SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

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

Query:

Select distinct ord.supplier_code,
cat.category,
(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

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

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

select m.*,prodCount 
from
(
Select distinct ord.supplier_code,
cat.category
From orders ord
Cross Join categories cat
)m
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 
)pasc
 on pasc.suppliercode = m.supplier_code 
and pasc.idProdAttribute = pa.idProdAttribute 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.27 seconds. Powered By: Snitz Forums 2000