2012-07-04

[DB]一些常見的SQL語法效能建議

1.對查詢進行優化,應儘量避免全表掃描,首先應考慮在 where 及 order by 涉及的列上建立索引。

2.應儘量避免在 where 子句中對欄位進行 null 值判斷,否則將導致引擎放棄使用索引而進行全表掃描,如:
select id from t where num is null
--可以在num上設置預設值0,確保表中num列沒有null值,然後這樣查詢:
select id from t where num=0

3.應儘量避免在 where 子句中使用!=或<>操作符,否則將使引擎放棄使用索引而進行全表掃描。

4.應儘量避免在 where 子句中使用 or 來連接條件,否則將導致引擎放棄使用索引而進行全表掃描,如:
select id from t where num=10 or num=20
--可以這樣查詢:
select id from t where num=10
union all
select id from t where num=20

5.in 和 not in 也要慎用,否則會導致全表掃描,如:
select id from t where num in (1,2,3)
--對於連續的數值,能用 between 就不要用 in :
select id from t where num between 1 and 3

6.下面的查詢也將導致全表掃描:
select id from t where name like 'abc'
若要提高效率,可以考慮全文檢索。

7.應儘量避免在 where 子句中對欄位進行運算式操作,這將導致引擎放棄使用索引而進行全表掃描。如:
select id from t where num/2=100
--應改為:
select id from t where num=100*2

8.應儘量避免在 where 子句中對欄位進行函數操作,這將導致引擎放棄使用索引而進行全表掃描。如:
select id from t where substring(name,1,3)='abc' --name以abc開頭的id
select id from t where datediff(day,createdate,'2005-11-30')=0--‘2005-11-30’生成的id
--應改為:
select id from t where name like 'abc'
select id from t where createdate>='2005-11-30' and createdate<'2005-12-1'

9.不要在 where 子句中的“=”左邊進行函數、算術運算或其他運算式運算,否則系統將可能無法正確使用索引。

10.在使用索引欄位作為條件時,如果該索引是複合索引,那麼必須使用到該索引中的第一個欄位作為條件時才能保證系統使用該索引,否則該索引將不會被使用,並且應盡可能的讓欄位順序與索引順序相一致。

11.很多時候用 exists 代替 in 是一個好的選擇
select num from a where num in(select num from b)
--用下面的語句替換:
select num from a where exists(select 1 from b where num=a.num)

12.並不是所有索引對查詢都有效,SQL是根據表中資料來進行查詢優化的,當索引列有大量資料重複時,SQL查詢可能不會去利用索引,如一表中有欄位sex,male、female幾乎各一半,那麼即使在sex上建了索引也對查詢效率起不了作用。

13.索引並不是越多越好,索引固然可以提高相應的 select 的效率,但同時也降低了 insert 及 update 的效率,因為 insert 或 update 時有可能會重建索引,所以怎樣建索引需要慎重考慮,視具體情況而定。一個表的索引數最好不要超過6個,若太多則應考慮一些不常使用到的列上建的索引是否有必要。

14.應盡可能的避免更新 clustered 索引資料列,因為 clustered 索引資料列的順序就是表記錄的物理存儲順序,一旦該列值改變將導致整個表記錄的順序的調整,會耗費相當大的資源。若應用系統需要頻繁更新 clustered 索引資料列,那麼需要考慮是否應將該索引建為 clustered 索引。

15.儘量使用數字型欄位,若只含數值資訊的欄位儘量不要設計為字元型,這會降低查詢和連接的性能,並會增加存儲開銷。這是因為引擎在處理查詢和連接時會逐個比較字串中每一個字元,而對於數字型而言只需要比較一次就夠了。

16.盡可能的使用 varchar/nvarchar 代替 char/nchar ,因為首先變長欄位儲存空間小,可以節省儲存空間,其次對於查詢來說,在一個相對較小的欄位內搜索效率顯然要高些。

17.盡量不要用 select * from t ,用具體的欄位列表代替“*”,不要返回用不到的任何欄位。

18.HAVING 條件是在抓出所需資料後才做的過濾,跟一開始就寫在 WHERE 條件中是很像的:
select subject, count(subject) from tblA GROUP BY subject HAVING subject != 'Tim' AND subject != 'Lin'
--應改為:
select subject, count(subject) from tblA WHERE subject != 'Tim' AND subject != 'Lin' GROUP BY subject

19.減少子查詢數量,也能提升效能:
select name from tblA where salary = (select MAX(salary) FROM tblB) and age = (select MAX(age) FROM tblB)
--應改為:
select name from tblA where (salary, age) = (select MAX (salary), MAX (age) from tblB)


來源:Avan_Lau的專欄

沒有留言:

張貼留言