May 29, 2007 at 7:38 am
I have 2 tables A, B in SQL 2000. Table A contains 1,323,540 rows and table B contains 1,902,932 rows. The Primary Key of Table A is used as foreigh in Key in Table B. Indexes on both fields in both tables are created (PK in A, Non-Clustered in B).
I am simply joing tables A and B on foreign key and applying a filter in where criteria on table A. The query is executed very slow. I checked the EXECUTION PLAN and it shows that it is scanning the table B. I think it should seek index created on table B but it is not doing this.
Please help why it is behaving like this.
May 29, 2007 at 8:01 am
Could be a number of things. How often do you update your statistics and/or reindex your tables? Use DBCC SHOWCONTIG to find out how fragmented your indexes are.
John
May 29, 2007 at 8:21 am
Posting the query might help us guide you towards ensuring the indexes you have are appropriate.''
--------------------
Colt 45 - the original point and click interface
May 31, 2007 at 7:15 am
Yes, please post the query... chances are, the criteria in the WHERE clause is what's keeping the index from being used...
--Jeff Moden
Change is inevitable... Change for the better is not.
June 1, 2007 at 6:57 am
Without seeing anything I can pretty confidently state that it is either old statistics (and you are asking for data at the 'tail' of the set) or more likely you are simply asking for more than 10-20% of the total data in the set in which case the optimizer won't use an index anyway since index usage will be more costly than table scans.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
June 1, 2007 at 5:33 pm
or more likely you are simply asking for more than 10-20% of the total data in the set in which case the optimizer won't use an index anyway since index usage will be more costly than table scans |
Heh... we've been though this... that's just not true in all cases especially if a covering index is involved...
--Jeff Moden
Change is inevitable... Change for the better is not.
June 1, 2007 at 8:01 pm
Another possibility, if the previous excellent suggestions don't fix the problem, is having different data types in the joined fields. This requires a conversion function which makes indexes useless. The fields in a foreign key relationship would have to be compatible, but if there are additional fields in the join this is a possibility.
June 1, 2007 at 9:30 pm
Good of you to remember our last discourse Jeff!! HEHEHE
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
June 4, 2007 at 4:42 am
Try moving the filter condition on table a from the WHERE clause to the ON clause. This generally speeds up most queries, as the WHERE clause is not applied until all the records in the tables are merged.
Regards, Peter
June 4, 2007 at 6:55 am
Do you have any examples of this? I'd be interested in seeing them.
June 4, 2007 at 8:50 am
I take it back. With this simple example it doesn't seem make any difference when I checked further. It is more useful when there are more than two tables joined together.
What I was suggesting was changing:
Select * from tableb b inner join tablea a on b.fkey = a.pkey where a.somefield = 'somevalue'
to:
Select * from tableb b inner join tablea a on b.fkey = a.pkey and a.somefield = 'somevalue'
June 4, 2007 at 9:52 pm
This query may useful
Select a.* from a
inner join b
on (a.id = b.id and b.age >30)
where a.sex = 'M'
Bye
June 4, 2007 at 10:23 pm
???
--------------------
Colt 45 - the original point and click interface
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply