English 中文(简体)
Index with Leftouter join there is always Index scan in sql server 2005
原标题:

I have query joining several tables, the last table is joined with LEFT JOIN. The last table has more then million rows and execution plan shows table scan on it. I have indexed columns on which the join is made. It is always use index scan but If I replace LEFT JOIN with INNER JOIN, index seek is used used and execution takes few seconds but with LEFT JOIN there is a table scan , so the execution takes several minutes. Does using outer joins turn off indexes? Missed I something? What is the reason for such behavior? Here is the Query

Select * 
FROM

     Subjects                  s
    INNER join       Question  q ON q.SubjectID   = s.SubjectID
    INNER JOIN       Answer    c ON a.QestionID   = q.QuestionID
    Left outer JOIN  Cell      c ON c.Question ID = q.QuestionID

Where S.SubjectID =15

There is cluster index on SubjectID in "Subject" table. and there is non-cluster index on questionID in other tables.

Solution: I try it in other way and now I am index seek on Cell table. Here is the modified query:

Select * 
FROM

     Subjects                  s
    INNER join       Question  q ON q.SubjectID   = s.SubjectID
    INNER JOIN       Answer    c ON a.QestionID   = q.QuestionID
    Left outer JOIN  Cell      c ON c.Question ID = q.QuestionID
                                              AND C.QuestionID > 0
                                              AND C.CellKey > 0

Where S.SubjectID =15

This way I did high selectivity on Cell table. :)

问题回答

I just tried to simulate the same issue, however there is no table scan instead it was using the clustered index of Cell, at the same time you could try to force the index, you can check the syntax here and the issues you may face when forcing an index here. Hope this helps.





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签