sql server - Complex SQL where clause: many 'and' and 'or' conditions -


I generate a query where such clauses for MS SQL Server with the complex:

  (t1.Column1 = SomeValue1 and t1.Column2 = SomeValue2) or (t1.Column1 = SomeValue3 and t1.Column2 = SomeValue4) or (t1.Column1 = SomeValue5 and t1.Column2 = SomeValue6) ... (50 or more the situation like this)  

hierarchy list with real depth of 3 levels to me and I want to retrieve all three levels of objects with a query. So T1 Column 1 here is 1 parent, T1. Column 2 is the second parent. How many circumstances do I have my WHERE clauses, because many things have been expanded to the second level in the list. In the worst case can be 1000 or more.

I wonder if this query could affect performance. If so, are there any way to optimize this (table-value parameters, etc.)?

I have done some inquiry with query plans and query time compilation. Both look fine

I go out on a limb and I'm going to answer. This answer will not be correct, because in fact your "right" answer is not given the level of your expansion. However, to try to even keep some possible suggestions (assuming that you need 1000 section in your query):

  • query inserted after consistent and death. It will probably take some time to compile, but once compiled, the cache should do its job, as long as you keep changing it (even if you just change around prices). Use bind variables if the values ​​need to change.

  • Index - Make sure both columns are indexed. Both of them are available and always ask both of you, so you should be right.

This is a warning - I have a good way to ask it is a database that I'm not saying it just to help the above two points.


Comments