473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

strange optimizer

I tried this:

use northwind
go

SELECT OrderDate
FROM Orders WHERE OrderDate > '19950101'

see the query plan? ok

SELECT OrderDate, EmployeeId
FROM Orders WHERE OrderDate > '19950101'

see the query plan? what appened?

the only way to make an index seek instead of an index scan is to
force the
index usage ( with(index=orderdate) ), but I don't like this solution

also try this:

SELECT *
FROM Orders WHERE employeeId > 9

and

SELECT *
FROM Orders WHERE employeeId > 8

Can someone explain why this appens? and how can I overturn the
performance loss problem (well not in orders table, but in my table
there are 300K records and making a scan to retrieve 50 records is not
exactly what I want)

thanks to all
Jul 20 '05 #1
2 2079
See inline

Carlo Paccanoni wrote:

I tried this:

use northwind
go

SELECT OrderDate
FROM Orders WHERE OrderDate > '19950101'

see the query plan? ok
I see an index seek on a nonclustered index on OrderDate.

In this case, this probably the fastest access method, because the index
is smaller than the table, and can satisfy the entire query (it is a
covering index).
SELECT OrderDate, EmployeeId
FROM Orders WHERE OrderDate > '19950101'

see the query plan? what appened?
I see a clustered index scan.

Most likely, SQL-Server has estimated that it takes less time to scan
the entire table than any other strategy. One of these other strategies
would be to seek all relevant rows in the nonclustered index on
OrderDate, then for each relevant row, seek the clustered index (which
potentially is a page read for each row).

When I ran the query on my Northwind, the clustered index scan strategy
used 22 logical reads. The nonclustered index + bookmark lookup strategy
used 1740 logical reads.
the only way to make an index seek instead of an index scan is to
force the
index usage ( with(index=orderdate) ), but I don't like this solution

also try this:

SELECT *
FROM Orders WHERE employeeId > 9
I see a nonclustered index seek and a bookmark lookup for each
qualifying row.
and

SELECT *
FROM Orders WHERE employeeId > 8
I see a clustered index scan.
Can someone explain why this appens?
It all has to do with data distribution. SQL-Server has statistics on
each indexed column. When I ran "DBCC SHOW_STATISTICS (Orders,
EmployeeID)" on my Northwind, it showed that there were 277 rows
sampled, and that the values of EmployeeID range from 1 to 9. Because of
that, SQL-Server can estimate that rows with EmployeeID 9 will only be a
small portion of the table.

The second query will result in more rows. Because of this, the scale is
tipped over. It is no longer fastest to do NC Index Seeks + BM Lookups.
and how can I overturn the
performance loss problem (well not in orders table, but in my table
there are 300K records and making a scan to retrieve 50 records is not
exactly what I want)
SQL-Server will not scan the table if it has more than 50 pages (410Kb)
if it can accurately estimate that your query will only return 50 rows
(and you have an appropriate nonclustered index).

Make sure the statistics of the index are up to date.

Hope this helps,
Gert-Jan
thanks to all


--
(Please reply only to the newsgroup)
Jul 20 '05 #2
Thankyou Gert-Jan now it's more clear
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nick Mudge | last post by:
Hi, Does anybody know the performance difference between having your PHP code cached and just running your code with the zend optimizer? Is there much difference? Nick
9
by: hemal | last post by:
I came across a very strange situation at work. There is an order of magnitude difference in execution time for the following two queries (10 v/s ~130 msec): select count(*) from table_name...
3
by: Philip Yale | last post by:
I'm very puzzled by the choice of NC index being made by the optimizer in this example. I don't actually think it should use an NC index at all. I have: Table: CustomerStatus_T Single data...
10
by: Mark Barinstein | last post by:
Hello. W2K, db2 v7, fp11. Given: create table pays ( acode integer not null, packno smallint not null, sum decimal(15, 2) not null
3
by: Peter Arrenbrecht | last post by:
Hi all We ran into a very annoying optimizer problem recently. We had added a new key and self-join relation to a large production table. That key will be filled very rarely and having just...
9
by: Andrea | last post by:
Hi, I've read the former postings but was not able to solve my problem: I have a Summary Table (or MQT as you like it) and the query optimizer does not seem to use the summary table. I run...
2
by: Auday Alwash | last post by:
Hi, I have a really interesting one for you guys... SQL Server 2000 sp3 on Windows Server 2003 If I run this query: declare @find varchar(50)
5
by: Kevin | last post by:
Using a base table, a MQT table was created. With optimization - when querying the base table with calcuation that are already completed in the MQT - I would assume the optimizer would use the MQT...
2
by: boa sema | last post by:
Way back when, and at least in version 7 IIRC, the query optimizer gave up when the where clause in a statement contained more than 4 search conditions. Does anyone know if such a limitation...
3
by: aj | last post by:
DB2 LUW v8.2 FP 14 RHAS 2.1 Sorry if these are newbie questions. Optimizer stuff is black magic to me. For both of these, assume stats are current and an even distribution of data....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.