寫SQL時盡量不要對欄位進行運算操作

2022-03-07 08:46:38 字數 741 閱讀 8887

有時我們在做專案時,需要在sql文裡面對某個字段經過運算後再與乙個常量比較:那麼這將會對執行效能產生很大的影響:

請看下面sql的執行效率:

select * from iroomtypeprice where

amount/30< 1000(11

秒)當改寫成下面的sql語句時效率明顯提高了:

select * from iroomtypeprice where

amount< 1000*30

(<1秒)

語句1因為要對沒沒條記錄的字段amount做乙個/的運算,所以必須進行全表掃瞄,表的合適索引不會起作用;而語句二就會用到表上的合適索引。因此效率明顯提高。

另外,在where後面盡量少用substring等函式,這樣也可以提高執行效率,如:

select * from iroomtypeprice where

substring(card_no,1,4)='5378'(13

秒)改成

select * from iroomtypeprice where

card_no like '5378%'(<1秒)

select * from iroomtypeprice wher e

convert(char(10),date,112)='19991201'(10

秒)select * from iroomtypeprice where date= '1999/12/01'

(< 1

秒)

ibatis中寫SQL語句時使用in遇到的問題描述

update chunqiu3.order head t set t.status flag where t.id in orderids 傳遞的id為2488877,2488878,2488879。但是資料卻沒有任何的修改。因為ibatis缺省會把 中間的變數作為字串來處理。這樣,就會出現這樣的s...

mybatis中寫sql語句時需要轉義的字元

mybatis配置檔案,sql語句中含有轉義字元 錯誤語句 date sub curdate interval 3 day date a.create date 錯誤資訊 caused by org.xml.sax.saxparseexception linenumber 8 columnnumbe...

mybatis中寫sql語句時需要轉義的字元

mybatis配置檔案,sql語句中含有轉義字元 錯誤語句 select from table base where flag topic 錯誤資訊 caused by org.xml.sax.saxparseexception linenumber 8 columnnumber 54 the en...