|
|
|
|
|
by i_s
1524 days ago
|
|
At my work, we often parse and rewrite a query before handing it off to SQL Server, because there are a lot of cases where Microsoft misses obvious optimizations. Sometimes there are also optimizations we can do because of things we know at compile time, but don't fit in the type system of SQL. The impact varies all the way from just shaving off 10% of the execution time, to changing some queries from timing out in a web request to executing in a few hundred milliseconds. A few examples: - Inlining scalar function calls (less impactful now with Sql Server 2019) - Removing joins from a query when we know it won't impact the number of records - Deepening where conditions against derived tables - Killing "branches" of union queries when they can be determined to not matter statically Yes, we could write the queries that way in the first place, but it would make them harder to compose, more verbose, and harder for the programmer to communicate intent. Not to mention, often the user can impact what the query will be, making it less feasible. |
|
.
- Inlining scalar function calls (less impactful now with Sql Server 2019)
write them as table valued funcs and it'll inline them for you (ugly but it works and is easy)
.
- Removing joins from a query when we know it won't impact the number of records
That's just tree pruning. It does that
.
- Deepening where conditions against derived tables
If I understand you that's predicate pushdown. MSSQL does it well.
.
- Killing "branches" of union queries when they can be determined to not matter statically
Not sure what you mean. Can you give an example?