Hi
Recently I have faced with a strange problem. Developers of one of my customers came with a query like this:
SELECT *
FROM packages
WHERE packages.item_number = 427909 OR 427909 = 0
go
"packages" is a table, there is an index on item_number column and "packages.item_number = 427909" is a very much selective condition. However, adding of an apparently false condition "427909 = 0" causes the optimizer not to see the first condition at all, which results in an expensive table scan. The behavior is observed in both version 15.7 SP51 (via set option show_lio_costing) and 12.5.3 (dbcc traceon(302)). Removing of the apparently false condition solves the problem. My questions:
1. Is it an expected behavior? Why the optimizer stops seeing the first condition?
2. Is there any workaround that doesn't involve changing of a query? Developers will change the query, of course, but releasing of a new application version may take some time.
To illustrate the problem better, I pasted outputs of "set showplan on" and "set option show_lio_costing on" below.
Thanks in advance
Leonid Gvirtz
Beginning selection of qualifying indexes for table 'packages',
Estimating selectivity of index 'packages.idx_packages_item', indid 3
scan selectivity 1, filter selectivity 1
56533 rows, 330 pages
Data Row Cluster Ratio 0.2954172
Index Page Cluster Ratio 0.7847222
Data Page Cluster Ratio 0.2359819
using index prefetch (size 16K I/O)
Large IO selected: The number of leaf pages qualified is > MIN_PREFETCH pages
in index cache 'default data cache' (cacheid 0) with LRU replacement
using table prefetch (size 16K I/O)
Large IO selected: The number of leaf pages qualified is > MIN_PREFETCH pages
in data cache 'default data cache' (cacheid 0) with LRU replacement
Data Page LIO for 'idx_packages_item' on table 'packages' = 41797
Estimating selectivity for table 'packages'
Table scan cost is 56533 rows, 6651 pages,
The table (Datarows) has 56533 rows, 6651 pages,
Data Page Cluster Ratio 0.9943289
Search argument selectivity is 0.330035.
using table prefetch (size 16K I/O)
Large IO selected: The number of leaf pages qualified is > MIN_PREFETCH pages
in data cache 'default data cache' (cacheid 0) with LRU replacement
The Cost Summary for best global plan:
FINAL PLAN ( total cost = 52661.9 ):
lio=6976 pio=870 cpu=169599 tempdb=0
QUERY PLAN FOR STATEMENT 1 (at line 1).
Optimized using Serial Mode
STEP 1
The type of query is SELECT.
2 operator(s) under root
|ROOT:EMIT Operator (VA = 2)
|
| |RESTRICT Operator (VA = 1)(0)(0)(0)(7)(0)
| |
| | |SCAN Operator (VA = 0)
| | | FROM TABLE
| | | packages
| | | Table Scan.
| | | Forward Scan.
| | | Positioning at start of table.
| | | Using I/O Size 16 Kbytes for data pages.
| | | With LRU Buffer Replacement Strategy for data pages.