有两张表,一张是empl有545405条记录,第二张是pam有1466320条记录。任务是根据aID找到pID的计数。因此,为了完成这项任务,我编写了以下查询。
Select pa.aID, count(pa.pID) from
empl join pam pa
ON empl.pID = pa.pID
Group by pa.aID
pam的索引如下:
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
实际执行计划显示索引扫描:
我能理解的是,估计数据大小为15 MB,这导致了问题。
有没有办法针对大量数据调整这个复杂的计数查询?
编辑:
带有 empl 过滤器的查询:
Select pa.aID, count(pa.pID) from
empl join pam pa
ON empl.pID = pa.pID
where
empl.del = 0 AND
empl.pub = 1 AND
empl.sID = 2 AND
empl.md = 0
Group by pa.aID
结构中没有什么花哨的,只使用了基本的数据类型 int、bit、varchar 和 datatime。empl有65列, pam有4列