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

Query Performance - Advice needed on Joining same table multiple times Expand / Collapse
Author
Message
Posted Tuesday, October 02, 2012 3:18 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 15, 2013 10:51 AM
Points: 22, Visits: 84
We have a product table which some of the products are kits which include other products. So i have 2 tables

Item table
(id)
(title)

and
Groups Table
(gleaderid)
(itemid)
(grank)

So to get all the items in a kit i have to a statement like

Select i.id,
'description' = 'Kit contains' + childitem1.title + childitem2.title + etc
from imitem i
inner JOIN groups ON groups.gLeaderID = I.ID AND groups.grank = 1
left JOIN groups groups2 ON groups2.gLeaderID = I.ID AND groups2.grank = 2
left JOIN groups groups3 ON groups3.gLeaderID = I.ID AND groups3.grank = 3
left JOIN groups groups4 ON groups4.gLeaderID = I.ID AND groups4.grank = 4
left JOIN groups groups5 ON groups5.gLeaderID = I.ID AND groups5.grank = 5
left JOIN groups groups6 ON groups6.gLeaderID = I.ID AND groups6.grank = 6
left JOIN groups groups7 ON groups7.gLeaderID = I.ID AND groups7.grank = 7
left JOIN groups groups8 ON groups8.gLeaderID = I.ID AND groups8.grank = 8
left JOIN groups groups9 ON groups9.gLeaderID = I.ID AND groups9.grank = 9
left JOIN groups groups10 ON groups10.gLeaderID = I.ID AND groups10.grank = 10
left JOIN groups groups11 ON groups11.gLeaderID = I.ID AND groups10.grank = 11
left JOIN groups groups12 ON groups12.gLeaderID = I.ID AND groups10.grank = 12
left JOIN groups groups13 ON groups13.gLeaderID = I.ID AND groups10.grank = 13
left JOIN groups groups14 ON groups14.gLeaderID = I.ID AND groups10.grank = 14
left JOIN groups groups15 ON groups15.gLeaderID = I.ID AND groups10.grank = 15

inner JOIN IMItem childItem1 ON childItem1.id = groups.itemid
left JOIN IMItem childItem2 ON childItem2.id = groups2.itemid
left JOIN IMItem childItem3 ON childItem3.id = groups3.itemid
left JOIN IMItem childItem4 ON childItem4.id = groups4.itemid
left JOIN IMItem childItem5 ON childItem5.id = groups5.itemid
left JOIN IMItem childItem6 ON childItem6.id = groups6.itemid
left JOIN IMItem childItem7 ON childItem7.id = groups7.itemid
left JOIN IMItem childItem8 ON childItem8.id = groups8.itemid
left JOIN IMItem childItem9 ON childItem9.id = groups9.itemid
left JOIN IMItem childItem10 ON childItem10.id = groups10.itemid
left JOIN IMItem childItem11 ON childItem10.id = groups11.itemid
left JOIN IMItem childItem12 ON childItem10.id = groups12.itemid


This takes forever to run. Would maybe using some sort of function make this run faster you think? Looking for any advice

Post #1367304
Posted Tuesday, October 02, 2012 4:25 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, May 02, 2013 3:40 PM
Points: 522, Visits: 374
Any reason why you couldn't just turn this into separate queries and union them together? You could just PIVOT them together if you need it all in one flat row.
Post #1367325
Posted Tuesday, October 02, 2012 4:58 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:50 PM
Points: 1,308, Visits: 3,899
It's really hard to give a decent answer without any sample data.

How about you provide some sample data and expected results and you will definitely get a decent answer


MM




Post #1367331
Posted Tuesday, October 02, 2012 5:22 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 15, 2013 10:51 AM
Points: 22, Visits: 84
Groups Table
gid         gleaderid          itemid      grank 
46768 23014 36501 1
46769 23014 35405 2
46771 23014 44787 4
46772 23014 30026 5
50127 23014 18537 3
222140 23014 27258 6
222141 23014 47619 7


Item Table
id        title
23014 Canon 430EX II Kit
18537 SLR System Camera Case
27258 10 Free Prints
30026 Lens Cleaning Kit
35405 Tripod
36501 430EX II Flash
44787 Batteries & Rapid Charger
47619 Digital Photo Magazine



Query and result
Select i.id,
'description' = 'Kit contains' + childitem1.title + childitem2.title + etc
from imitem i
inner JOIN groups ON groups.gLeaderID = I.ID AND groups.grank = 1
left JOIN groups groups2 ON groups2.gLeaderID = I.ID AND groups2.grank = 2
left JOIN groups groups3 ON groups3.gLeaderID = I.ID AND groups3.grank = 3
left JOIN groups groups4 ON groups4.gLeaderID = I.ID AND groups4.grank = 4
left JOIN groups groups5 ON groups5.gLeaderID = I.ID AND groups5.grank = 5
left JOIN groups groups6 ON groups6.gLeaderID = I.ID AND groups6.grank = 6
left JOIN groups groups7 ON groups7.gLeaderID = I.ID AND groups7.grank = 7


inner JOIN IMItem childItem1 ON childItem1.id = groups.itemid
left JOIN IMItem childItem2 ON childItem2.id = groups2.itemid
left JOIN IMItem childItem3 ON childItem3.id = groups3.itemid
left JOIN IMItem childItem4 ON childItem4.id = groups4.itemid
left JOIN IMItem childItem5 ON childItem5.id = groups5.itemid
left JOIN IMItem childItem6 ON childItem6.id = groups6.itemid
left JOIN IMItem childItem7 ON childItem7.id = groups7.itemid



Result
id	description
23014 Kit contains Canon Speedlite 430EX II Flash & Tripod & Camera Case & Batteries & Rapid Charger & Lens Cleaning Kit & Free Prints






Post #1367338
Posted Tuesday, October 02, 2012 5:27 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, May 02, 2013 3:40 PM
Points: 522, Visits: 374
I'm sorry, but this is just bad design and you should push back to the developer to have them either list the included items in the package, or if they really desire, concatenate it from multiple rows, themselves.
Post #1367342
Posted Tuesday, October 02, 2012 6:10 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:50 PM
Points: 1,308, Visits: 3,899
I have turned your sample data into something that people can use - take note that you should do this , not us...

Things to note:

1. I used two CTE expressions to provide the sample data - just because I don't like bothering with tables for a small sample.
2. Your expected result contains data that is not present in the sample, so I best-guessed what you want.
3. I used a comma in the FOR XML PATH('') part of the query, then used a REPLACE to turn commas into Ampersands, just because ampersands get converted to & amp ; by the XML processor.

;with groups (gid,gleaderid,itemid,grank )
as
(
SELECT 46768,23014,36501,1 UNION ALL
SELECT 46769,23014,35405,2 UNION ALL
SELECT 46771,23014,44787,4 UNION ALL
SELECT 46772,23014,30026,5 UNION ALL
SELECT 50127,23014,18537,3 UNION ALL
SELECT 222140,23014,27258,6 UNION ALL
SELECT 222141,23014,47619,7
),items(id,title)
as
(
SELECT 23014,'Canon 430EX II Kit' UNION ALL
SELECT 18537,'SLR System Camera Case' UNION ALL
SELECT 27258,'10 Free Prints' UNION ALL
SELECT 30026,'Lens Cleaning Kit' UNION ALL
SELECT 35405,'Tripod' UNION ALL
SELECT 36501,'430EX II Flash' UNION ALL
SELECT 44787,'Batteries & Rapid Charger' UNION ALL
SELECT 47619,'Digital Photo Magazine'
)
select id,'Kit contains '+ i_outer.title + REPLACE(titles,',','&') as description
from items as i_outer
outer apply (
select ' , '+i_inner.title
from groups as g_inner
join items as i_inner
on i_inner.id = g_inner.itemid
where g_inner.gleaderid = i_outer.id
order by g_inner.grank
for xml path('')
) as ex(titles)
where id = 23014



MM




Post #1367354
Posted Wednesday, October 03, 2012 7:06 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 15, 2013 10:51 AM
Points: 22, Visits: 84
Thanks for doing that. I am going to try building a function to see if it is faster that way. Right now this query takes about 20 minutes to run using all the joins
Post #1367648
Posted Wednesday, October 03, 2012 7:21 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:50 PM
Points: 1,308, Visits: 3,899
Michael T2 (10/3/2012)
Thanks for doing that. I am going to try building a function to see if it is faster that way. Right now this query takes about 20 minutes to run using all the joins


Did you try the solution I posted - it should outperform your multi-join solution?


MM




Post #1367660
Posted Wednesday, October 03, 2012 7:29 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 15, 2013 10:51 AM
Points: 22, Visits: 84
oh sorry, i thought you just cleaned up my post so others could use it. I will try it now
Post #1367670
Posted Wednesday, October 03, 2012 9:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 15, 2013 10:51 AM
Points: 22, Visits: 84
Wow you are amazing. Thank you. Went from over 20 minutes to 34 Seconds.
Post #1367786
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse