INF: Complexity of WHERE Clauses in SQL Server

ID Number: Q75404

1.10 1.11 4.20

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')

: :

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 reference words: 1.10 1.11 4.20