sql - Is there a way to tuneup a complex count query against heavy data? -
there 2 tables 1 empl having 545405 records , second pam having 1466320 records. task find count of pid according aid. acomplish task write following query.
select pa.aid, count(pa.pid) empl join pam pa on empl.pid = pa.pid group pa.aid
the indexes pam following:
ix_pam_unique nonclustered, unique, unique key located on primary pid, aid ix_pam_aid nonclustered located on primary aid pk_paid clustered, unique, primary key located on primary paid
the actual execution plan shows index scan:
what can understand there estimated data size of 15 mb causing problem.
is there way tuneup complex count query against heavy data?
edit:
the query empl filters:
select pa.aid, count(pa.pid) empl join pam pa on empl.pid = pa.pid empl.del = 0 , empl.pub = 1 , empl.sid = 2 , empl.md = 0 group pa.aid
and there nothing fancy in structures, basic datatypes int, bit, varchar , datatime used. there 65 columns in empl , 4 columns in pam
possible helpful -
select pa.* empl join ( select pa.aid , cnt = count(pa.pid) pam pa group pa.aid ) pa on empl.pid = pa.pid
or -
select pa.aid, count(pa.pid) pam pa exists( select 1 empl empl.pid = pa.pid ) group pa.aid
or -
select pa.aid , cnt = count(pa.pid) pam pa group pa.aid
Comments
Post a Comment