以下查询工作,但没有特别快:如何改进此查询?
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行,并且行的特定控件ID(distinct子句前)计从1变化到61。分析器说响应时间是234/42毫秒。表格中没有索引。
很显然,缺乏索引是一个问题,但除此之外,任何人都可以推荐一种更有效的方式来编写这个查询吗?
可能是oracle,由于标签..? – Mithon 2013-03-06 19:27:39
为了在子查询没有找到任何内容时保持相同的结果,else子句应该在上面为null。此外,case表达式的默认值为null,所以'else null'不需要,但有时用于显示intent。 – 2013-03-06 19:34:47
你是绝对正确的。我会纠正我的尝试。 – alzaimar 2013-03-06 19:39:21