亚欧色一区w666天堂,色情一区二区三区免费看,少妇特黄A片一区二区三区,亚洲人成网站999久久久综合,国产av熟女一区二区三区

  • 發布文章
  • 消息中心
點贊
收藏
評論
分享
原創

sql使用簡單心得

2023-02-24 03:13:27
18
0

1.select * from  

* 一定要(yao)要(yao)慎用,最好是只返回你需(xu)要(yao)的列,否則效(xiao)率會(hui)很低


2.游標要慎用

之前在實際項目中,因為內存不(bu)足的原因,使用游標從數(shu)據庫中讀數(shu)據,

游(you)標實際上(shang)就是分批放(fang)入(ru)內(nei)存,實際上(shang)減小(xiao)了內(nei)存的占用,但效率會(hui)變低


3.索引建立在需要查詢較為頻繁的列

要(yao)建立在(zai)經(jing)常做查詢的列上(shang)


4.防止索引失效是很關(guan)鍵的

  (1) 最左前綴(zhui)法(fa)則:比如create index 了三(san)個索引,進(jin)行查(cha)詢(xun)匹配時,要(yao)按順序走索引

       ALTER  TABLE  `table_name`  ADD  INDEX index_name (  `column1`,  `column2`,  `column3`  )

        查詢時符合(he)最(zui)左前綴:

            select id from tablename where column1 = “spx”;      

            select id from tablename where column1 = “spx” and column2= "spx"

            select id from tablename where column1 = “spx” and column2 ="spx" and column3="spx"

        違背(bei)最(zui)左前(qian)綴,索引(yin)全部失效:

            select id from tablename where column2 = “spx”  and column3="spx"

        符合最(zui)左(zuo)前綴,只有(you)左(zuo)列索(suo)引(yin)生效(xiao)(跳躍了某一(yi)索(suo)引(yin)列):

            select id from tablename where column1 = “spx”  and column3="spx"

   (2) 索引(yin)列進行運算操作,索引(yin)將失效

   (3)使(shi)用(yong)or 時如(ru)果or之(zhi)前是索(suo)引列,or之(zhi)后不是那么索(suo)引失效,所(suo)以限制(zhi)條(tiao)件字段沒有索(suo)引就少用(yong)or;

     解決方(fang)案(an):可以使用(yong)union 或者(zhe)union all 效果更(geng)好,但是(shi)盡量要使用(yong) union all ,

        因為前者要加(jia)結(jie)果(guo)集合并后再(zai)進行過濾操作,增(zeng)大cpu運算,但是union all 前提是倆個(ge)結(jie)果(guo)集沒有重復數(shu)據(ju)

        column1 --索引(yin)列   column 2 -- 不(bu)是索引(yin)列

        索引失效:select id from tablename where conlumn1 ="spx"  or conlumn2="spx";

        解(jie)決(jue): select id from tablename   where conlumn1=“spx”

           union all 

           select id from tablename  where conlumn2="spx";

   (4)模糊查詢時(shi),以%開(kai)頭,索引失(shi)效,盡量都寫(xie)成尾部(bu)模糊匹配

   (5) is NULL,is not NULL 有時索(suo)引失效

   (6) 索(suo)引列數據類型(xing)不匹配,比如 column1 的(de)類型(xing)是varchar

      eg: select id from  tablename where  column1 = 1;   沒(mei)有加引(yin)號(hao),會自動轉(zhuan)類型導致索引(yin)失(shi)效 


5.復合索引大多數情況下效率高于單列索引

因為多條件聯合(he)查詢時,mysql優(you)化(hua)器會評估哪個條件的索(suo)引(yin)的效率高(gao),會去選(xuan)擇(ze)最(zui)佳(jia)的索(suo)引(yin)

6.EXPLAIN的使用

要經常使(shi)用 EXPLAIN  來(lai)查看(kan) sql 的(de)執行計劃(hua),這算一個很(hen)好的(de)習慣,比較sql的(de)性(xing)能(neng),查看(kan)該語(yu)句是否使(shi)用了索(suo)引等等,真的(de)很(hen)重(zhong)要!!


7. in 和 exists , not in 和 not exists 小結

  select *from tablename1 where id in (select id from tablename2)

  上面的sql等于下面這個

  select *from tablename1 where exists(select id tablename2 where tablename2.id = tablename1.id)

  根據驅動的(de)(de)順序可知(zhi),in先執行(xing)子查詢,in適合(he)于(yu)外表大而內表小的(de)(de)情況,exists先對外表做loop循環,所(suo)以適合(he)于(yu)外表小而內表大的(de)(de)情況

  not in 內外表(biao)都進行全表(biao)掃描,而 not exists 得子(zi)查詢依然能用到表(biao)上(shang)的索引,所以一(yi)定要使用 not exists

0條評論
0 / 1000
嘎嘎嘎嘎
15文(wen)章(zhang)數
0粉絲(si)數
嘎嘎嘎嘎
15 文章(zhang) | 0 粉絲
嘎嘎嘎嘎
15文章數
0粉絲數
嘎嘎嘎嘎
15 文章 | 0 粉絲(si)
原(yuan)創(chuang)

sql使用簡單心得

2023-02-24 03:13:27
18
0

1.select * from  

* 一定要(yao)要(yao)慎用,最好是只返(fan)回(hui)你(ni)需要(yao)的列,否則效率會很(hen)低


2.游標要慎用

之前在(zai)實際(ji)項(xiang)目(mu)中,因(yin)(yin)為內存不(bu)足的原因(yin)(yin),使用游標從數(shu)據(ju)庫中讀數(shu)據(ju),

游(you)標實際(ji)上就是(shi)分批放入內(nei)存,實際(ji)上減小了(le)內(nei)存的占用(yong),但效率會變(bian)低(di)


3.索引建立在需要查詢較為頻繁的列

要建(jian)立(li)在(zai)經常(chang)做(zuo)查詢的(de)列(lie)上(shang)


4.防止索引失效是(shi)很關鍵(jian)的

  (1) 最左前(qian)綴法(fa)則:比如create index 了(le)三(san)個索(suo)引,進行查詢(xun)匹配時(shi),要按順序走索(suo)引

       ALTER  TABLE  `table_name`  ADD  INDEX index_name (  `column1`,  `column2`,  `column3`  )

        查詢時符合最左前綴:

            select id from tablename where column1 = “spx”;      

            select id from tablename where column1 = “spx” and column2= "spx"

            select id from tablename where column1 = “spx” and column2 ="spx" and column3="spx"

        違(wei)背最左前綴(zhui),索引全部失效:

            select id from tablename where column2 = “spx”  and column3="spx"

        符(fu)合最左前(qian)綴,只有左列(lie)(lie)索引生效(跳(tiao)躍了(le)某一索引列(lie)(lie)):

            select id from tablename where column1 = “spx”  and column3="spx"

   (2) 索引列進(jin)行運(yun)算操作,索引將失(shi)效

   (3)使用or 時(shi)如果or之前是索引列,or之后不是那么索引失(shi)效,所(suo)以限制條(tiao)件字(zi)段沒有索引就少用or;

     解決方案:可以使用(yong)union 或者union all 效果更好,但是盡量(liang)要使用(yong) union all ,

        因(yin)為前者要加結果(guo)集合并后(hou)再進(jin)行過濾操作,增大cpu運(yun)算,但(dan)是union all 前提(ti)是倆個結果(guo)集沒有(you)重復(fu)數(shu)據

        column1 --索引列(lie)(lie)   column 2 -- 不是索引列(lie)(lie)

        索引(yin)失效(xiao):select id from tablename where conlumn1 ="spx"  or conlumn2="spx";

        解決: select id from tablename   where conlumn1=“spx”

           union all 

           select id from tablename  where conlumn2="spx";

   (4)模(mo)糊查詢時,以%開頭,索引失效,盡量都寫成尾部模(mo)糊匹(pi)配(pei)

   (5) is NULL,is not NULL 有時(shi)索引失效(xiao)

   (6) 索引列數據類型不匹配,比如 column1 的(de)類型是varchar

      eg: select id from  tablename where  column1 = 1;   沒有加引號(hao),會自(zi)動轉類型(xing)導致索引失效 


5.復合索引大多數情況下效率高于單列索引

因為多條(tiao)件(jian)聯(lian)合查詢時,mysql優化器會(hui)評估(gu)哪個(ge)條(tiao)件(jian)的索(suo)引(yin)的效(xiao)率高(gao),會(hui)去選擇最佳的索(suo)引(yin)

6.EXPLAIN的使用

要經(jing)常使用 EXPLAIN  來查(cha)看 sql 的(de)執行計劃(hua),這算一個很好的(de)習慣,比較sql的(de)性(xing)能,查(cha)看該語句是否(fou)使用了索(suo)引等等,真的(de)很重要!!


7. in 和 exists , not in 和 not exists 小結

  select *from tablename1 where id in (select id from tablename2)

  上面的sql等于(yu)下(xia)面這個(ge)

  select *from tablename1 where exists(select id tablename2 where tablename2.id = tablename1.id)

  根(gen)據驅(qu)動的順序可知,in先(xian)(xian)執行(xing)子查詢(xun),in適(shi)合于外表(biao)大(da)而內(nei)表(biao)小(xiao)的情況(kuang),exists先(xian)(xian)對外表(biao)做(zuo)loop循環,所以適(shi)合于外表(biao)小(xiao)而內(nei)表(biao)大(da)的情況(kuang)

  not in 內(nei)外表(biao)都進(jin)行(xing)全表(biao)掃描,而 not exists 得(de)子查詢依然能用到表(biao)上的索引,所以一(yi)定要使用 not exists

文章來自個人專欄
文章(zhang) | 訂閱
0條評論
0 / 1000
請輸入你的評論
1
0