Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cross joins slow?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

United Kingdom
28 Posts

Posted - 12/09/2013 :  06:09:34  Show Profile  Reply with Quote
The following script runs incredibly slowly..

I started by cross joining supplier_codes with product categories. I've then used a few nested queries in the select statement to return the total number of products those suppliers have in those categories.

The nested queries serve to determine which products belong to which supplier and which product categories those products belong to, both referencing the outer query.

Is there a quicker method to achieve this? I've heard joining tables is quicker than using nested queries but due to the nature of this query requiring the cross join, that doesn't feel like the intuitive strategy?

Any help is much appreciated.


Select distinct ord.supplier_code,
(select count(pasc.productcode) prodCount from productsBySupplier pasc where pasc.suppliercode = ord.supplier_code and pasc.idProdAttribute in
(select pa.idProdAttribute from productAttribute pa Where pa.idProductAttributeGroup in
(select pag.idProductAttributeGroup from productAttributeGroups pag where pag.attributeGroup = cat.category))) prodCount
From orders ord
Cross Join categories cat

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 12/09/2013 :  07:02:17  Show Profile  Reply with Quote

select m.*,prodCount 
Select distinct ord.supplier_code,
From orders ord
Cross Join categories cat
left join productAttributeGroups  pag 
on pag.attributeGroup = m.category
left join productAttribute pa 
on pa.idProductAttributeGroup = pag.idProductAttributeGroup
left join (select suppliercode,idProdAttribute,count(pasc.productcode) prodCount 
from productsBySupplier 
group by suppliercode,idProdAttribute 
 on pasc.suppliercode = m.supplier_code 
and pasc.idProdAttribute = pa.idProdAttribute 

SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000