以下查询有效,但不是特别快:
select distinct controlid, (
select max(Orderno) From my_table
Where ControlID = #param1#
and a = 3 and b = 13 and c = 0 and d = 0
) colA, (
Select max(Orderno) From my_table
Where ControlID = #param1#
and a = 2
) colB, (
Select max(Orderno) From my_table
Where ControlID = #param1#
and a = 1 and b = 14 and e = 1
) colC, (
Select max(Orderno) From my_table
Where ControlID = #param1#
and a = 3 and b = 13 and e = 1 and c = 0 and d = 0
) colD, (
Select a From my_table
Where ControlID = #param1#
and Orderno = #param2#
) colE
from my_table
where controlid = #param1#
被查询的表有超过 300K 行,并且特定 controlid(在 distinct 子句之前)的行数从 1 到 61 不等。分析器说响应时间是 234/42 毫秒。表上没有索引。
显然,缺少索引是一个问题,但除此之外,任何人都可以推荐一种更有效的方式来编写这个查询吗?