PRB: Using WHERE with OR or IN Can Slow Down Performance

Last reviewed: April 25, 1997
Article ID: Q68470

The information in this article applies to:

  - Microsoft SQL Server version 4.2 for OS/2
  - Microsoft SQL Server version 4.2

SYMPTOMS

It takes a long time to execute a query that uses a WHERE clause with multiple IN or OR options. However, if the same query is broken up into shorter separate queries, and the OR or IN options are not used, the performance improves dramatically.

CAUSE

Before SQL Server executes a query, it first attempts to optimize the query. In the optimization step, it looks at what, if any, indexes exist on a table, and decides whether those indexes will be useful in the execution of the query. In addition, it examines the conditions in the WHERE clause to estimate how many rows will be affected by the query.

It is sometimes beneficial to avoid using the OR and IN options in a WHERE clause. In certain situations, the query may not be able to be optimized as well as it could be when the OR or IN option is used.

WORKAROUND

In these instances, better performance can be obtained by breaking the query into separate, smaller queries. This is not to say, however, that the OR and IN options should never be used. There are situations where using an OR or IN option will give better performance than using separate queries.


Additional query words: Transact-SQL dblib Windows NT
Keywords : kbprg SSrvDB_Lib SSrvServer SSrvTran_SQL SSrvWinNT
Version : 4.2 | 4.2
Platform : OS/2 WINDOWS


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.