Saturday, November 29, 2014

Help on performance with dynamic query

First of all a "dynamic query" is not "a query" - it is a multitude of them. Some of them may be fast, others may be slow.


There is of course no way we can give specific suggestions without seeing the code, the table and index definitions etc.


We can only give the generic suggestions. As for the code, make sure that you are using parameterised SQL and you are not building a complete SQL string with parameters and all. If nothing else, this helps to make the code more readable and maintainable. It also protects you against SQL injection. And it also helps to prevent performance issue due to implicit conversion.


You will need to look at the query plan to see where the bottlenecks may be. You should look at the actual query plan. Note that the thickness of the arrows are more relevant than the percentages you see; the percentages are only estimates, and estimates are often off. Next step is to see if you can add indexes to alleviate the situation. You should also analyse if there are problems in the query, for instance indexed columns that are entangled in expression. If you are using views, make sure that you don't have views built on top of views etc. This can often result a table appearing multiple times in a query, when one would be enough.





Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

No comments:

Post a Comment