SQL Server / Hierarchy ID Performance -
take following scenario...
create table #parentitems ( itemid uniqueidentifier, hid hierarchyid, treeid uniqueidentifier, hidlevel hid.getlevel() ) create table #childitems ( itemid uniqueidentifier, hid hierarchyid, treeid uniqueidentifier, hidlevel hid.getlevel() ) create unique nonclustered index pind on #parentitems(treeid, hidlevel, hid) create unique nonclustered index cind on #childitems(treeid, hidlevel, hid) so 2 tables, parent table has bunch of ids/hids know ancestors of items in child tables. there multiple different tree structures in each, hence treeid (all hids same tree, share same treeid).
assume #parentitems , #childitems have 20,000+ rows in each.
the following query reasonable fast (for it's doing...) takes around second complete.
select distinct liparent.itemid parentid , lichild.itemid childid #parentitems liparent inner join #childitems lichild on lichild.hid.isdescendantof(liparent.hid)=1 however, ignores treeid relationship well. second add "where liparent.treeid = lichild.treeid" query (or add join) whole query jumps around 30 second execution time. seems simple change, , believe index in place... slows down massively.
anyone have ideas? in advance!
Comments
Post a Comment