473,287 Members | 3,286 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,287 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 8162
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.