FIX: Optimizer May Choose Inefficient Query Plan When Using NOT LIKE with an Empty String
This article was previously published under Q247125
This article has been archived. It is offered "as is" and will no longer be updated.
BUG #: 56552 (SQLBUG_70)
SQL Server may incorrectly estimate the number of rows returned by a query or subquery using the NOT LIKE comparison operator with the Empty String. This behavior can subsequently cause poor performance on SELECT statements due to inefficient query plans being chosen.
Instead of using the NOT LIKE comparison operator for selecting rows that do not have Empty String values for a column, use the following query:
Note that '_' matches any single character, and '%' matches any string of zero or more characters.
SELECT * FROM orders WHERE shipaddress LIKE '_%'
Microsoft has confirmed this to be a problem in SQL Server 7.0. This problem has been corrected in U.S. Service Pack 2 for Microsoft SQL Server 7.0. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
254561 INF: How to Obtain Service Pack 2 for Microsoft SQL Server 7.0 and Microsoft Data Engine (MSDE) 1.0For more information, contact your primary support provider.
The behavior reported can be observed by running the following query with the Northwind database:
This query returns 830 rows; however, the generated Estimated Execution Plan only estimates a single row returned for the NOT LIKE filter. To display the Estimated Execution Plan for a query, you can select the Display Estimated Execution Plan (CTRL+L) option from the Query Analyzer's toolbar.
SELECT * FROM orders WHERE shipaddress NOT LIKE ''
sqlserver LIKE Empty String query T-SQL
Article ID: 247125 - Last Review: 10/22/2013 03:14:17 - Revision: 2.1
Microsoft SQL Server 7.0 Standard Edition
- kbnosurvey kbarchive kbbug kbfix KB247125