Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

always use incorrect index to run the query by sql server Expand / Collapse
Author
Message
Posted Friday, March 05, 2010 11:23 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, June 22, 2012 6:44 AM
Points: 18, Visits: 71
One table has many fields. And in the table,it has a clustered index on a fieldA, and a nonclustered index on a fieldB. I have a query like :select * from table where fieldB in (select fieldBfrom othertable). But the sql server always use the Clustered index on fieldA, and it is very slow.

C.G
Post #878096
Posted Friday, March 05, 2010 11:28 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 32,903, Visits: 26,784
Colin-232036 (3/5/2010)
One table has many fields. And in the table,it has a clustered index on a fieldA, and a nonclustered index on a fieldB. I have a query like :select * from table where fieldB in (select fieldBfrom othertable). But the sql server always use the Clustered index on fieldA, and it is very slow.


The use of SELECT * pretty much dictates that you'll not use indexes effectively. It would probably be beneficial to use a WHERE EXISTS in this case, as well, but the SELECT * will continue to be a problem in most cases.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #878097
Posted Friday, March 05, 2010 11:43 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, April 19, 2012 10:25 PM
Points: 1,231, Visits: 3,483
Jeff Moden (3/5/2010)
Colin-232036 (3/5/2010)
One table has many fields. And in the table,it has a clustered index on a fieldA, and a nonclustered index on a fieldB. I have a query like :select * from table where fieldB in (select fieldBfrom othertable). But the sql server always use the Clustered index on fieldA, and it is very slow.


The use of SELECT * pretty much dictates that you'll not use indexes effectively. It would probably be beneficial to use a WHERE EXISTS in this case, as well, but the SELECT * will continue to be a problem in most cases.


Hi Jeff,

In this,
It’s better to use the Joins instead of the sub queries/Exists.
Post #878098
Posted Saturday, March 06, 2010 12:29 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:17 AM
Points: 2,213, Visits: 4,168
arun.sas (3/5/2010)

Hi Jeff,

In this,
It’s better to use the Joins instead of the sub queries/Exists.


The results might differ in both the cases.
Check the example given below..
declare @a table ( col char(1) )
declare @b table ( col char(1) )

insert @a
select 'a' union all
select 'b' union all
select 'c' union all
select 'd'

insert @b
select 'a' union all
select 'a' union all
select 'b' union all
select 'b' union all
select 'b' union all
select 'c'

select * from @a a inner join @b b on a.col = b.col
select * from @a a where exists( select * from @b b where a.col = b.col )




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #878099
Posted Saturday, March 06, 2010 12:58 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 18,733, Visits: 12,332
Coincidentally I just ran into an issue very similar to this today.

My query doesn't use a Select * and the query is SARGable. However, I noticed that it is trying to use the CI and a Key lookup on SQL 2005. The same query with same index structure on a second server does not require the Key Lookup, and uses the appropriate Index. However, if I remove the desired index, I get an RID lookup (note that it is not a key lookup since this terminology changed).

The difference is that the second server is patched and the first server is running 2005 RTM.

Thus, my recommendation is to check your patch level. Being properly patched in this scenario corrected the index usage problem I was seeing.

This recommendation is based on if you are not really using a select * but used that terminology simply to portray the issue.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server 2008


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #878106
Posted Saturday, March 06, 2010 1:22 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 37,682, Visits: 29,937
arun.sas (3/5/2010)
It’s better to use the Joins instead of the sub queries/Exists.


Sure about that?
http://sqlinthewild.co.za/index.php/2010/01/12/in-vs-inner-join/



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #878114
Posted Saturday, March 06, 2010 1:43 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, April 19, 2012 10:25 PM
Points: 1,231, Visits: 3,483
GilaMonster (3/6/2010)
arun.sas (3/5/2010)
It’s better to use the Joins instead of the sub queries/Exists.


Sure about that?
http://sqlinthewild.co.za/index.php/2010/01/12/in-vs-inner-join/

Hi,
I would thing yet it’s by join, but in the wild compares good result, but have the doubt says more than a condition to compare, what’s happened Gail?
Post #878122
Posted Saturday, March 06, 2010 6:17 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 37,682, Visits: 29,937
Sorry, don't understand what you're asking there. Clarify please.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #878146
Posted Saturday, March 06, 2010 10:17 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855, Visits: 9,374
arun.sas (3/5/2010)
Jeff Moden (3/5/2010)
Colin-232036 (3/5/2010)
One table has many fields. And in the table,it has a clustered index on a fieldA, and a nonclustered index on a fieldB. I have a query like :select * from table where fieldB in (select fieldBfrom othertable). But the sql server always use the Clustered index on fieldA, and it is very slow.


The use of SELECT * pretty much dictates that you'll not use indexes effectively. It would probably be beneficial to use a WHERE EXISTS in this case, as well, but the SELECT * will continue to be a problem in most cases.


Hi Jeff,

In this,
It’s better to use the Joins instead of the sub queries/Exists.

This is a wide-spread myth, encouraged by an optimizer weakness in SQL Server 2000. Please stop spreading it around.

Joins are not "better" than sub-queries. They are not even "in general" better than subqueries. They are "in general" about the same thing, but it always depends.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #878186
Posted Saturday, March 06, 2010 11:58 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 37,682, Visits: 29,937
RBarryYoung (3/6/2010)
Joins are not "better" than sub-queries. They are not even "in general" better than subqueries. They are "in general" about the same thing, but it always depends.


This being the reason that I'm writing the blog series (one of the posts linked above) on comparing in, exists and joins, and their negations. So far, in simple tests, IN and EXISTS identical, JOIN a little slower, though that may be a fluke (it was just because of a different join operator used) and I'm not willing to make a general conclusion based on that test.

Joins are most definitely not faster than IN.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #878199
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse