question

Ian Roke avatar image
Ian Roke asked

Can the way this statement is written speed the query up or should I look at table design?

I have been given the following slow performing query which is taking a cut from 1.2 million rows to return about 51,000. It is taking about 56 seconds which is slow for a webpage.

SELECT *    
FROM vwPPEKBaseQuery, vwWeeklyRanges2Only
WHERE OM_Available BETWEEN WeekStart AND WeekEnd

Can I write the JOIN in a different way to speed things up or should I be looking at indexes and so on for the tables? Also I am surprised that sort of join still exists and still runs on SQL Server!

sql-server-2005t-sqljoins
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 answered

As long as it's an inner join it will work in 2008. The ANSI 89 outer join syntax is deprecated. Based on the tags you're running in this in '05, so it'll work.

As to whether or not changing the syntax will speed it up, it's hard to know without seeing the execution plans for the query. But, in general, I shy away from joining views to views where I can. SQL Server can do a very good job of resolving the view for a given query, but, depending on the view and the query, it might not bother to resolve the view at all and will merely run it as written, whether or not you're referencing all the tables & columns in the view or not. Clearly, that can lead to huge performance problems.

10 |1200

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

Ian Roke avatar image
Ian Roke answered

Just out of interest here is the query plan. I have managed to get it down to 17 seconds now.

Query plan

2 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 ·
Clearly, you want to find a way around that clustered scan.
0 Likes 0 ·
Ian Roke avatar image Ian Roke commented ·
That's what I thought. Thanks for the confirmation Grant.
0 Likes 0 ·
Jeff Moden avatar image
Jeff Moden answered

Ian... I see no join criteria between the two tables. What you have there is a full blown cross-join. There has to be something that both tables can relate to so you can join them without them being a cross-join.

2 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.

Ian Roke avatar image Ian Roke commented ·
Agreed. The reason is because I am trying to select records where the OM_Available date is between two dates in the vwWeeklyRanges2Only table. I suppose it would be much quicker if I used a subquery instead? I think I might try that for comparison.
0 Likes 0 ·
Jeff Moden avatar image Jeff Moden commented ·
Subquery or not, unless you have something to join on between the two views, you have a time consuming cross join. I also agree with Grant... joing two views together isn't usually a form of computational Nirana.
0 Likes 0 ·

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.