以下查詢工作,但沒有特別快:如何改進此查詢?
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