question

JAF avatar image
JAF asked

Joining a master table took a long time to execute

Having a transaction header table, transaction details table and a master table. In details table there are multiple records for a header record. And each detail row refers a record from master table. Query 1 SELECT 1 FROM HeaderTable INNER JOIN DetailTable ON HeaderTable.HeaderID = DetailTable.HeaderID This will return around 4000 records in 40 ms. Query 2 SELECT 1 FROM DetailTable INNER JOIN MasterTable ON DetailTable.MasterRefID = MasterTable.MasterID This will return around 4000 records in 16 ms. Query 3 SELECT 1 FROM HeaderTable INNER JOIN DetailTable ON HeaderTable.HeaderID = DetailTable.HeaderID INNER JOIN MasterTable ON DetailTable.MasterRefID = MasterTable.MasterID This will return around 4000 records in 1400 ms. Why this last query took long time to execute? Note: All the tables has required indices also.
tables
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JAF avatar image JAF commented ·
Hello all, is there any other way to find out the reason for delay?
0 Likes 0 ·
Kev Riley avatar image
Kev Riley answered
So the common table between the first 2 queries is DetailTable. In each of the first 2 queries, there may be a different index being used to access DetailTable. In the third query, only one of these will be chosen initially, and whilst it may be a good index for one of the joins, may not be a good index for the other join, resulting in lookups. So maybe you should look at the execution plan to see if there is a different indexing strategy to employ.
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
Yeah, really hard to say without seeing the full structure as well as the data distribution, or, the execution plan.
0 Likes 0 ·
JAF avatar image JAF commented ·
Thank you all for your immediate reply. I had checked the SQL plan for all the 3 queries. Same indices were used. There is no difference.
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
The same index? In all 3 plans? Could you post the 3 execution plans as xml files here.
0 Likes 0 ·
Scot Hauder avatar image Scot Hauder commented ·
Is the third query using parallelism and the first two are not?
0 Likes 0 ·
Grant Fritchey avatar image
Grant Fritchey answered
The same indexes cannot be used the same way in all three queries. All three queries will be different because they all refer to different tables and have different join criteria. You're seriously missing a level of understanding on what the execution plans are telling you. If you can't post the .sqlplan files for us to look at, then I'd suggest you at least get a [copy of this book][1] on execution plans and read it. [1]: https://www.simple-talk.com/books/sql-books/sql-server-execution-plans,-second-edition,-by-grant-fritchey/
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

virtualjosh avatar image
virtualjosh answered
Why are you surprised it took longer to process if the last query uses 3 tables and the others just 2?. The execution plans can absolutely not be equal simply because of this fact. The answers to your question lies in the execution plan for the third query. So, look for clues there. Also, I suggest you start with the biggest table in the FROM clause.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.