久久久久久国产精品免费免,久久成人免费观看全部免费,久久久久久久久久免免费精品

網(wǎng)站開發(fā) APP開發(fā) 小程序開發(fā) SEO優(yōu)化 公司新聞

sql優(yōu)化的小技巧

2018-05-18 09:51:05
1104

  在日常的sql查詢中為了提高查詢效率,常常會(huì)對(duì)查詢語句進(jìn)行sql優(yōu)化,下面總結(jié)的一些方法,有需要的可以參考。

  1.對(duì)查詢進(jìn)行優(yōu)化的事項(xiàng),應(yīng)盡量避免全表掃描,首先應(yīng)考慮在 where 及 order by 涉及的列上建立索引。
 

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

  select id from t where num is null

  可以在num字段上設(shè)置默認(rèn)值,確保表中num字段列沒有null值,然后這樣查詢:

  select id from t where num=0

  3.應(yīng)避免在 where 子句中用!=或<>操作符,否則將使引擎放棄索引而進(jìn)行全表掃描。

  4.應(yīng)避免在 where 子句中使用 or 來連接條件,否則將導(dǎo)致引擎放棄索引而進(jìn)行全表掃描,如:

  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 也要慎用,會(huì)導(dǎo)致全表掃描,如:

  select id from t where num in(1,2,3)

  對(duì)于連續(xù)的數(shù)值,能用 between 就不用 in 了:

  select id from t where num between 1 and 3

  6.這種查詢也將導(dǎo)致全表掃描:

  select id from t where name like '%abc%'

  7.應(yīng)避免在 where 子句中對(duì)字段進(jìn)行表達(dá)式操作,這將導(dǎo)致引擎放棄索引而進(jìn)行全表掃描。如:

  select id from t where num/2=100

  應(yīng)改為:

  select id from t where num=100*2

  8.應(yīng)避免在where子句中對(duì)字段進(jìn)行函數(shù)操作,這將導(dǎo)致引擎放棄索引而進(jìn)行全表掃描。如:

  select id from t where substring(name,1,3)='abc'--name以abc開頭的id

  應(yīng)改為:

  select id from t where name like 'abc%'

?
<rp id="cxbmw"></rp>

<sup id="cxbmw"></sup>
  • <rp id="cxbmw"><form id="cxbmw"><nav id="cxbmw"></nav></form></rp>
    <big id="cxbmw"><delect id="cxbmw"><font id="cxbmw"></font></delect></big>
    <rp id="cxbmw"></rp>
    主站蜘蛛池模板: 北票市| 高雄市| 杭锦旗| 沿河| 加查县| 赤峰市| 高唐县| 冷水江市| 筠连县| 文登市| 城市| 庄浪县| 宣威市| 逊克县| 辽宁省| 沙雅县| 广东省| 安泽县| 双牌县| 湟中县| 松阳县| 长沙县| 博乐市| 台北县| 兴仁县| 兴海县| 定日县| 阜南县| 松桃| 眉山市| 句容市| 绥芬河市| 枣庄市| 伊通| 西乌珠穆沁旗| 化德县| 乐昌市| 绵竹市| 舞阳县| 敦煌市| 德昌县|