Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
skiabox
Posting Yak Master
169 Posts |
Posted - 2009-03-27 : 04:04:22
|
| I am running this queryWITH relations AS ( SELECT T1.id, T1.parentid FROM fullcases T1 WHERE T1.id = {0} UNION ALL SELECT T1.id, T1.parentid FROM fullcases T1 INNER JOIN relations TH ON TH.id = T1.parentid ) SELECT * FROM fullcases WHERE id = {0} UNION SELECT * FROM fullcases WHERE id in (SELECT DISTINCT id from relations)";.....but it takes too long to finish.Any ideas on how to make it more efficient?Thank you very much. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-03-27 : 04:19:00
|
My experience with using IN (...) or also NOT IN (...) in SQL Server 2005 says: don't use it or only for few data...WITH relationsAS(SELECT T1.id,T1.parentidFROMfullcases T1WHERET1.id = {0}UNION ALLSELECT T1.id,T1.parentidFROMfullcases T1INNER JOIN relations TH ON TH.id = T1.parentid)SELECT * FROM fullcases WHERE id = {0}UNIONSELECT f.* FROM fullcases fJOIN relations r on r.id = f.idWebfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-03-27 : 04:42:16
|
[code];WITH RelationsAS ( SELECT ID, ParentID FROM FullCases WHERE ID = {0} UNION ALL SELECT fc.ID, fc.ParentID FROM FullCases AS fc INNER JOIN Relations AS r ON r.ID = fc.ParentID)SELECT fc.*FROM Relations AS rINNER JOIN FullCases AS fc ON fc.ID = r.ID[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|