INF: Complexity of WHERE Clauses in SQL Server

ID: Q75404


The information in this article applies to:
  • Microsoft SQL Server for OS/2, version 4.2


SUMMARY

There is no simple limit on the complexity of a WHERE clause.

The terms in a WHERE clause such as


   WHERE  ((c1= '10') or (c1= '20') or (c1= '40'))
     AND  ((c2 = 'aa') or (c2 = 'bb') or (c2 = 'cc'))
     AND  (c3 = 'mm')
     AND  ((c4 = 'aaabbbccc') or (c4 = 'bbbcccdd'))
     AND  ((c5 = '11111') or (c5 = '22222')) 

are encoded into a tree structure consisting of AND nodes and OR nodes. The nodes are linked together to represent the logical structure of the predicate. In the above case, the AND nodes are peers at the top level and each AND node has a chain of OR nodes linked to it. If the ORs had ANDs inside them, the tree would be structured accordingly. If even those ANDs had ORs within them, the tree structure could still handle it.

   WHERE-->AND->OR->(c1='10')
            |    |
            |   OR->(c1='20')
            |    :
            |
           AND->OR->(c2='aa')
            |    |
            |   OR->(c2='bb')
            |    :
            |
           AND->OR->(c3='mm')
            :    : 


MORE INFORMATION

This illustrates that the only limit is the storage available for the linked lists. Each node has a pair of pointers (4 bytes each) and some status information (<10 bytes). Nodes that represent constants (such as 'aaabbbccc') require additional space for the constant. When the tree representing the entire SQL statement exceeds available storage in procedure cache, a message to that effect appears (#703).

Additional query words: dblib

Keywords : kbprg SSrvDB_Lib SSrvTrans SSrvServer
Version : 4.2
Platform : OS/2
Issue type :


Last Reviewed: March 11, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.