views:

178

answers:

2

I am trying to determine the performance of a piece of generated SQL that runs in SQL Server 2005.

It uses CROSS JOINS, but the conditionals tying the cross joined tables are in the where statement.

I previously thought that all cross joins that have where statements would first pull the full cartesian product and then apply the filter.

However, this following link on MSDN suggests differently.

http://msdn.microsoft.com/en-us/library/ms190690.aspx

It specifically states that if there is a conditional on a cross joined table, it will "behave" like an inner join. It goes on to show an example of the similar results of an inner join and a cross join w/ where conditional.

It does not state what the performance difference is though, only that they behave in a similar fashion.

+1  A: 

Probably the easiest thing to do is to display the execution plan for your statement and take a look at what it's doing. You can do it from management studio - there's some more info here:

http://www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx

Whisk
+1  A: 
The example given is correct, atleast in a basic query.

The query below will yield the same execution plan and execution times for both queries. The Sql Optimiser is typically free to move restrictions between where clauses and 'on' clauses during join operations, and i imagine the same is for cross joins.

    select companies.id, contacts.id
from 
companies
cross join contacts
where
contacts.companyid = companies.id


select companies.id, contacts.id
from 
companies
inner join contacts on contacts.companyid = companies.id
mrwayne
I've heard of an explain plan. Is that the same as an execution plan?
jetimms
funny, i've never heard of an execution plan! If you you are using SQL server management studio ,write your query, right click and then do 'show execution pkan' it breaks down the query into the logical processing components and shows you where the time / processing is used up. Typically i use it to determine when table scans are happening on query bottlenecks are appearing
mrwayne
I technically have SQL Server Manager, but I do everything in Advanced Query Tool (www.querytool.com). It allows something similar to an execution plan (or maybe exactly the same.) I just haven't fully understood what its for, but with your and Whish's recommendation I'll give it a shot. Thanks!
jetimms