473,386 Members | 1,726 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,386 software developers and data experts.

SQLSever not using index

Hello,

We are having a very strange problem. We have a table with about 5
million rows in it. The problem is with one of the non clustered
indexes. I have noticed that sometimes in query analyzer, when doing
an execution plan, the optimizer is NOT doing an index seek, or a
bookmark lookup when the query should. It sometimes will do a full
clustered index scan on the primary key, which takes much longer. For
example:

select * from MyTable where fk_value = 1001201

the optimizer WILL NOT do an index seek or bookmark lookup and for this
query:

select * from MyTable where fk_value = 1001222

it WILL do an index seek on the non clustered index. The only
difference is the values specified for fk_value.

I have done a update statistics MyTable with full scan and it still
will not use the non clustered index for some queries. I have also
tried:

select * from MyTable (INDEX=IX_FK_VALUE) where fk_value = 1001201

to force the optimizer to use this index, but it still DOES NOT use
this index. Its wierd because for some values it will use the index,
and some it will not. Not sure what is going on

Any help would be greatly appreciated.

TIA

Jul 23 '05 #1
6 8175
Stu
Are there any other tables in your query that you have removed for
simplicity in posting? It may have something to do with the amount of
data that matches from the other table. Just shooting in the dark.

One thing you should do, however, is get rid of the SELECT *. You'll
have better luck at hitting a covering index if you limit your query to
only the data that you need to return.

It's a start, anyway. :)

Stu

Jul 23 '05 #2
talf, have you tried looking at the statistics using DBCC SHOWCONTIG
and SHOW_STATISTICS? Perhaps they are incorrect or stale and require
updating? By any change does the non-index using version of the plan
show that the query is being ran parallell? (Though the index hint
should have disabled that).

HTH -- Mark D Powell --

Jul 23 '05 #3
just did a show statstics and 1001201 has:

RANGE ROWS: 159514.0
EQ ROWS: 154588.0
DISTINCT RANGE ROWS: 1
AVG RANGE ROWS: 159514.0

and 1001222, the one that works has:

RANGE ROWS: 7173.0
EQ ROWS: 10589.0
DISTINCT RANGE ROWS: 9
AVG RANGE ROWS: 797.0

I also noticed that this is happening on our test server as well. I
found an fk_value with a large number of rows and did an execution
plan, and it is using the clustered scan, not a bookmark or seek w/ the
correct index.

Jul 23 '05 #4
No. The query is that simple.

I don't understand why the column list in the select would make a
difference on which index is used. It seems like the where clause
would be the determining factor. At any rate, I tried your suggestion
and this is what I found:

When I select only the PK of the table ie:

select id from cps_common_draft where fk_value = 1001201

it DOES do an index seek on the non clustered index. However, if I add
one of the address fields, it goes back to a full clustered scan:

select id, address_1 from cps_common_draft where fk_value = 1001201

Hmmm

Jul 23 '05 #5
No. The query is that simple.

I don't understand why the column list in the select would make a
difference on which index is used. It seems like the where clause
would be the determining factor. At any rate, I tried your suggestion
and this is what I found:

When I select only the PK of the table ie:

select id from cps_common_draft where fk_value = 1001201

it DOES do an index seek on the non clustered index. However, if I add
one of the address fields, it goes back to a full clustered scan:

select id, address_1 from cps_common_draft where fk_value = 1001201

Hmmm

Jul 23 '05 #6
(ta*****@ncpsolutions.com) writes:
I don't understand why the column list in the select would make a
difference on which index is used. It seems like the where clause
would be the determining factor. At any rate, I tried your suggestion
and this is what I found:

When I select only the PK of the table ie:

select id from cps_common_draft where fk_value = 1001201

it DOES do an index seek on the non clustered index. However, if I add
one of the address fields, it goes back to a full clustered scan:

select id, address_1 from cps_common_draft where fk_value = 1001201


Yes, this is expected. When you do "SELECT id", the query can
be evaluated from the index alone. No need to access the data pages.
I assume here that id is in the clustered index, and recall that
non-clustered indexes as row locatotr uses the keys of the clustered
index.

When you add a column which is not in the index, the index seek must be
combined with a bookmark lookup. Now, for a narrow selection, index
seek + bookmark lookup is a good thing. But for a wide selection,
this can be a disaster.

The FK value you have problems with appear 159514 times according
to the statistics in your other posting. This means 159514 bookmark
lookups, or 159514 pages access. Your table has five million rows.
Permit me to assume a row size of 50: 50 * 5000000 / 8192 = 30518
pages to scan the table, if it's completely unfragmented. As you
can see, the clustered index scan is a lot cheaper in this case.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7

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

Similar topics

6
by: Chris Foster | last post by:
I am trying to implement a very fast queue using SQL Server. The queue table will contain tens of millions of records. The problem I have is the more records completed, the the slower it gets....
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
8
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE(...
7
by: ad | last post by:
I found that there is reporting service bundle with SQLSever 2005 express. Can VS2005 express can use the reporting service in SQLSever 2005 express?
5
by: Bas Scheffers | last post by:
Hi, I have a table with about 100K rows, on which I have created a btree index of the type table_name(int, int, int, timestamp). At first postgres was using it for my AND query on all four...
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
1
by: Steffen Stellwag | last post by:
Truely is often better to scan a table in full passing by an index , but if you can force the optimizer to use an index via a hint for testing and comparing the results. But the index in the...
5
by: ryuchi311 | last post by:
In C++ using arrays. I need to create a C Program that will ask for five integers input from the user, then store these in an array. Then I need to find the lowest and highest integers inside that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.