国产精品一级片-操日本美女-成人在线免费看-久久久精品中文字幕麻豆发布-国产电影av-五月综合久久-色伊人网-亚洲精品**不卡在线播he-亚洲一区精品视频-亚洲在线视频免费观看-在线能看的av-篠田优在线观看-视频免费在线-国产一区欧美二区-精久久久久久久

網站開發 APP開發 小程序開發 SEO優化 公司新聞

sql優化的小技巧

2018-05-18 09:51:05
1846

  在日常的sql查詢中為了提高查詢效率,常常會對查詢語句進行sql優化,下面總結的一些方法,有需要的可以參考。

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

  2.應避免在 where 子句中對字段進行 null 值的判斷,否則將使引擎放棄索引而進行全表掃描,如:

  select id from t where num is null

  可以在num字段上設置默認值,確保表中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 name like 'abc%'

?