sql server - Additional fields to results of INTERSECT -
i have query looks match 6 fields between 2 tables , return matches. query uses inner joins. i've been testing intersect replace this. thus,
select field1, field2, field3,...,field6 tablea intersect select field1, field2, field3,...,field6 tableb
however, want add tablea.id , tableb.id results , can't quite written query won't take forever (relative original query). thoughts?
update: regretfully, added comment performance when first concern how write query. can see, believe main issue bad , incorrect query structure haven't been able improve. attempt:
select a.id, b.id tablea inner join ( select field1, field2, field3,...,field6 tablea intersect select field1, field2, field3,...,field6 tableb ) dupes on ( a.field1 = dupes.field1 , ... a.field6 = dupes.field6 ) inner join tableb b ( b.field1 = dupes.field1 , ... b.field6 = dupes.field6 )
a simple inner join
on 6 fields return records both tables having common field values:
select a.id, b.id, a.field1, a.field2, a.field3, a.field4, a.field5, a.field6 tablea inner join tableb b on a.field1 = b.field1 , a.field2 = b.field2 , a.field3 = b.field3 , a.field4 = b.field4 , a.field5 = b.field5 , a.field6 = b.field6
to optimize above query need set indices on columns used in on
clause of inner join
.
Comments
Post a Comment