INF: Complexity of WHERE Clauses in SQL Server

Last reviewed: April 25, 1997
Article ID: Q75404

The information in this article applies to:

  - Microsoft SQL Server version 4.2 for OS/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 SSrvServer SSrvTrans
Version : 4.2
Platform : OS/2


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 25, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.