簡體   English   中英

oracle查詢的查詢優化

[英]Query optimization of oracle query

我對查詢優化的了解不多,我需要一些幫助。 這是我們項目的示例代碼,它是長期運行的 oracle 查詢,用於從 2 個不同的表(student、student_info)中提取數據。

有沒有可能優化這個? where子句“and”操作在這里如何工作?

它在執行 AND 子句時是否保持任何順序? 下面的查詢如何通過刪除b.student_id in ('a123','b123','c123')行的代碼使前后不同。

我們沒有在該表列上添加索引的權限。

我們如何在不創建索引的情況下提高性能。

select a.student_id
       max(decode(a.marks_limit, 99.99,100,null )) as max_marks,
       b.student_city_code "NYC",
from student a, 
     student_info b
where a.student_id=b.student_id
  and a.student_id in ('a123','b123','c123')
  and b.student_id in ('a123','b123','c123')
  and  b.adress_modified > TO_TIMESTAMP('2003/12/13 10:13:18', 'YYYY/MM/DD HH:MI:SS')
  group by a.student_id, b.student_city_code;

你可以:

select a.student_id
    max(decode(a.marks_limit, 99.99,100,null )) as max_marks,
    b.student_city_code "NYC",
from student a
join student_info b
  on a.student_id=b.student_id     -- explicit join
where a.student_id in ('a123','b123','c123') -- removing duplicate condition
  and b.adress_modified>TO_TIMESTAMP('2003/12/13 10:13:18','YYYY/MM/DD HH:MI:SS')
group by a.student_id, b.student_city_code;

並添加索引:

CREATE INDEX id1 ON student(student_id);
CREATE INDEX id2 ON student_info(student_id);
CREATE INDEX id3 ON student_info(adress_modified);

首先,使用正確、明確、正確的JOIN語法以及合理的表別名和case表達式編寫查詢。

修復后,我希望是這樣的:

select s.student_id
       max(s.marks_limit) as max_marks,  -- I have no idea what the decode() is supposed to be doing
       si.student_city_code
from student s join
     student_info si
     on s.student_id = si.student_id
where s.student_id in ('a123', 'b123', 'c123')
 and   si.adress_modified > TIMESTAMP '2003-12-13T10:13:18'HH:MI:SS')
group by s.student_id, si.student_city_code;

我將從student(student_id)student_info(student_id address_modified, student_city_code)上的索引開始。

只是一些建議。

你已經有 a.student_id=b.student_id 所以 ('a123,'b123','c123') 中的條件 b.student_id 只是一個有用的重復你應該也使用顯式連接符號而不是基於 where 子句的舊隱式符號

   select a.student_id
    max(decode(a.marks_limit, 99.99,100,null )) as max_marks,
    b.student_city_code "NYC",
    from student a
    INNER JOIN  student_info b ON a.student_id= b.student_id
    WHERE 
    and a.student_id in ('a123','b123','c123')
    and  b.adress_modified > TO_TIMESTAMP('2003/12/13 10:13:18', 'YYYY/MM/DD HH:MI:SS')
    group by a.student_id, b.student_city_code

為了獲得更好的性能,您應該檢查表上的復合索引

 student_info ( adress_modified, student_id )

或者

student_info ( adress_modified, student_id, student_city_code )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM