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»»

CROSS JOIN vs INNER JOIN performance issue. Expand / Collapse
Author
Message
Posted Friday, May 17, 2013 4:33 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, April 13, 2014 11:53 PM
Points: 714, Visits: 516
Dear All,

I have a query which was using CROSS JOIN (with relations) earlier and now we modified it with INNER JOIN.

As per Prod server report, CROSS JOIN was performing faster but as per my theoretical knowledge, INNER JOIN should perform faster.

I have attached Queries, IO Stats and Execution plan for your reference.

Any guidance will be highly appreciated.


  Post Attachments 
CrossJoin.doc (6 views, 330.50 KB)
Post #1453923
Posted Friday, May 17, 2013 5:52 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:41 AM
Points: 6,748, Visits: 12,843
T.Ashish (5/17/2013)
Dear All,

I have a query which was using CROSS JOIN (with relations) earlier and now we modified it with INNER JOIN.

As per Prod server report, CROSS JOIN was performing faster but as per my theoretical knowledge, INNER JOIN should perform faster.

I have attached Queries, IO Stats and Execution plan for your reference.

Any guidance will be highly appreciated.


It's much easier for folks to look at your problem if you post execution plans as .sqlplan attachments, and put queries directly into your post with the appropriate tags.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1453949
Posted Friday, May 17, 2013 6:15 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, April 13, 2014 11:53 PM
Points: 714, Visits: 516
Query A

SELECT
e.es_id,
e.site_id,
e.bu_id,
e.comp_code,
e.submit_time,
e.status,
e.currency,
j.lt_id
FROM
dbo.exp_sheet e(nolock)
INNER JOIN dbo.work w(nolock) on e.work_id = w.work_id
INNER JOIN dbo.job_post j(nolock) on w.job_post_id = j.job_post_id
WHERE
w.work_type = 1 AND e.status IN (0,1,5,31,97)

Query B


SELECT
e.es_id,
e.site_id,
e.bu_id,
e.comp_code,
e.submit_time,
e.status,
e.currency,
j.lt_id
FROM
dbo.exp_sheet e(nolock) cross JOIN dbo.work w(nolock) cross JOIN dbo.job_post j(nolock)
WHERE
e.work_id = w.work_id
AND w.work_type = 1
AND w.job_post_id = j.job_post_id
AND e.status IN (0,1,5,31,97)

Query C


SELECT
e.es_id,
e.site_id,
e.bu_id,
e.comp_code,
e.submit_time,
e.status,
e.currency,
j.lt_id
FROM
dbo.exp_sheet e(nolock),
dbo.work w(nolock),
dbo.job_post j(nolock)
WHERE
e.work_id = w.work_id
AND w.work_type = 1
AND w.job_post_id = j.job_post_id
AND e.status IN (0,1,5,31,97)


  Post Attachments 
Query.sqlplan (6 views, 153.81 KB)
Post #1453956
Posted Friday, May 17, 2013 6:35 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:41 AM
Points: 6,748, Visits: 12,843
The queries are logically equivalent, they generate the same result set. The plans are identical. What was the question again?
Think of your query as a description of the result set that you want returned, rather than an instruction to SQL Server to do stuff in a particular way.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1453962
Posted Friday, May 17, 2013 7:00 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:42 PM
Points: 41,516, Visits: 34,431
More nolocks... I do hope your users are happy with potentially incorrect data. Nolock is NOT a go-faster switch.

See - http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx



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 #1453984
Posted Friday, May 17, 2013 7:11 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, April 13, 2014 11:53 PM
Points: 714, Visits: 516
Though I know the drawbacks of NOLOCK, it can't be avoided in our case. So, excuse me for that.

And regarding this post, I know that logically all 3 queries are same, but I'm little confused about how SQL Server is handling the JOINS in these 3 queries!!!

And if it doesn't matters then why I should not use Query 2 or 3 everywhere!!!
Post #1453994
Posted Friday, May 17, 2013 7:22 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:42 PM
Points: 41,516, Visits: 34,431
T.Ashish (5/17/2013)
Though I know the drawbacks of NOLOCK, it can't be avoided in our case.


Really? So you absolutely must allow potentially incorrect data in results because ....

And regarding this post, I know that logically all 3 queries are same, but I'm little confused about how SQL Server is handling the JOINS in these 3 queries!!!


They're logically the same, so SQL's handling the joins identically. The fact that some are joined in the FROM and others joined in the WHERE doesn't change the query's form nor the way SQL executes them.

And if it doesn't matters then why I should not use Query 2 or 3 everywhere!!!


You can if you wish. They're harder to read, easier to make mistakes with and impossible to convert to outer joins, but if you wish you can use them



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 #1453998
Posted Friday, May 17, 2013 7:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:41 AM
Points: 6,748, Visits: 12,843
GilaMonster (5/17/2013)
T.Ashish (5/17/2013)
Though I know the drawbacks of NOLOCK, it can't be avoided in our case.


Really? So you absolutely must allow potentially incorrect data in results because ....

And regarding this post, I know that logically all 3 queries are same, but I'm little confused about how SQL Server is handling the JOINS in these 3 queries!!!


They're logically the same, so SQL's handling the joins identically. The fact that some are joined in the FROM and others joined in the WHERE doesn't change the query's form nor the way SQL executes them.

And if it doesn't matters then why I should not use Query 2 or 3 everywhere!!!


You can if you wish. They're harder to read, easier to make mistakes with and impossible to convert to outer joins, but if you wish you can use them


In addition to these points, the type of join used in a query implies intent. Use a CROSS JOIN instead of an INNER JOIN and the next developer who looks at the code will be scratching their head wondering what on earth you were trying to achieve.

The coding styles you see on ssc aren't accidental - they're based on tens of thousands of hours of experience from thousands of coders, and are well worth a little investment of your time.

Why do you have silly unnecessary spaces surrounding your object delimiters?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1454003
Posted Friday, May 17, 2013 8:17 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Sunday, April 13, 2014 11:53 PM
Points: 714, Visits: 516
Gail, Chris
Thanks a ton.

I will go with queru 1, even if there is no performance gain.

One last qerry you may like to help on:

If I use query 2/3, then optimizer must be converting it in Inner Join before execution. If I'm right then is there any way I can find out the diferrence between Query I wrote and Query sql executed!!
Post #1454030
Posted Friday, May 17, 2013 8:22 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 4:42 PM
Points: 41,516, Visits: 34,431
T.Ashish (5/17/2013)
If I use query 2/3, then optimizer must be converting it in Inner Join before execution. If I'm right then is there any way I can find out the diferrence between Query I wrote and Query sql executed!!


No, it's not converting it to an inner join, it's already an inner join, just a different way of writing it. Since the queries are logically identical (and the parser and optimiser know that), they are parsed into the same internal format and optimised to the same execution plan.



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 #1454033
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse