<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 索引的使用 ## 1.準備環境 ```sql create table `tb_seller` ( `sellerid` varchar (100), `name` varchar (100), `nickname` varchar (50), `password` varchar (60), `status` varchar (1), `address` varchar (100), `createtime` datetime, primary key(`sellerid`) )engine=innodb default charset=utf8mb; insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('alibaba','阿里巴巴','阿里小店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('baidu','百度科技有限公司','百度小店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('huawei','華為科技有限公司','華為小店','e10adc3949ba59abbe56e057f20f883e','0','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('itcast','傳智播客教育科技有限公司','傳智播客','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('itheima','黑馬程序員','黑馬程序員','e10adc3949ba59abbe56e057f20f883e','0','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('luoji','羅技科技有限公司','羅技小店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('oppo','OPPO科技有限公司','OPPO官方旗艦店','e10adc3949ba59abbe56e057f20f883e','0','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('ourpalm','掌趣科技股份有限公司','掌趣小店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('qiandu','千度科技','千度小店','e10adc3949ba59abbe56e057f20f883e','2','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('sina','新浪科技有限公司','新浪官方旗艦店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('xiaomi','小米科技','小米官方旗艦店','e10adc3949ba59abbe56e057f20f883e','1','西安市','2088-01-01 12:00:00'); insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`) values('yijia','宜家家居','宜家家居旗艦店','e10adc3949ba59abbe56e057f20f883e','1','北京市','2088-01-01 12:00:00'); create index idx_seller_name_sta_addr on tb_seller(name,status,address); #創建聯合索引name status address ``` ## 2.避免索引失效 ### 1). 全值匹配 ,對索引中所有列都指定具體值。 ~~~ mysql> explain select * from tb_seller where name='' and status='1' and address=''\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: tb_seller partitions: NULL type: ref possible_keys: idx_seller_name_sta_addr key: idx_seller_name_sta_addr key_len: 612 ref: const,const,const rows: 1 filtered: 100.00 Extra: NULL 1 row in set, 1 warning (0.00 sec) ERROR: No query specified ~~~ ### 1). 全值匹配 ,對索引中所有列都指定具體值。 改情況下,索引生效,執行效率高。 ```sql explain select * from tb_seller where name='小米科技' and status='1' and address='北京市'\G; ``` ![](https://img.kancloud.cn/b0/0b/b00b6e3f84b5053240b190703ec8424f_2642x258.png) ### 2). 最左前綴法則 如果索引了多列,要遵守最左前綴法則。指的是查詢從索引的最左前列開始,并且不跳過索引中的列。 **2.1匹配最左前綴法則,走索引:** ![](https://img.kancloud.cn/c1/fe/c1fe06c974593ae18e334fdedcc438f7_2530x686.png) 索引全部覆蓋時,索引長度比較長。 **2.2違法最左前綴法則 , 索引失效:** ~~~ explain select * from tb_seller where status='0' and address='北京市'; ~~~ ![](https://img.kancloud.cn/1d/20/1d200526414f77feceff5e0e867654d7_2560x234.png) **2.3如果符合最左法則,但是出現跳躍某一列,只有最左列索引生效::** ~~~ explain select * from tb_seller where name = '小米科技' and address='北京市'; ~~~ ![](https://img.kancloud.cn/e2/9e/e29ee2a5e0fc29e3aa3e5c8c5888dcba_2762x230.png) **2.4 范圍查詢右邊的列,不能使用索引 。** ~~~ explain select * from tb_seller where status = '0' and address='北京市'; ~~~ ![](https://img.kancloud.cn/27/75/27752509fa13f8987fa67beefa7a1d8f_2344x228.png) **2.5 不要在索引列上進行運算操作, 索引將失效。** ~~~ explain select * from tb_seller where substring(name,3,2) = '科技'; ~~~ ![](https://img.kancloud.cn/95/7c/957c7eb2d863435b7bee9eb68bb9ccf3_2256x240.png) **2.6 字符串不加單引號,造成索引失效。** ~~~ mysql> explain select * from tb_seller where name = '小米科技' and status =0; ~~~ 由于,在查詢是,沒有對字符串加單引號,MySQL的查詢優化器,會自動的進行類型轉換,造成索引失效。 ![](https://img.kancloud.cn/db/26/db26a09494c5cc52542482e9b0fff492_2696x232.png) 此時只命中name一個索引,索引長度為單獨命中name時的長度、 **2.6 盡量使用覆蓋索引,避免select ** ~~~ mysql> explain select * from tb_seller where name = '小米科技' and status='0' and address='北京市'; ~~~ ![](https://img.kancloud.cn/d4/69/d46933d2901ee37c7fe5886881cf1e66_2780x234.png) 如果查詢列,超出索引列,也會降低性能。 ~~~ mysql> explain select name,nickname from tb_seller where name = '小米科技' and status='0' and address='北京市'; ~~~ ![](https://img.kancloud.cn/20/90/2090f3e2fcf9e987fc096eddfb6d68e8_2672x248.png) ``` TIP : using index :使用覆蓋索引的時候就會出現,完全使用索引 using where:在查找使用索引的情況下,需要回表去查詢所需的數據 using index condition:查找使用了索引,但是需要回表查詢數據 using index ; using where:查找使用了索引,但是需要的數據都在索引列中能找到,所以不需要回表查詢數據 ``` **2.7 用or分割開的條件, 如果or前的條件中的列有索引,而后面的列中沒有索引,那么涉及的索引都不會被用到。** 示例,name字段是索引列 , 而nikename不是索引列,中間是or進行連接是不走索引的 : ```sql explain select * from tb_seller where name='黑馬程序員' or nickname = '張三'; ``` ![](https://img.kancloud.cn/73/bd/73bd3c088811ceb988c88a0be9e4ded8_2554x250.png) **2.8 以%開頭的Like模糊查詢,索引失效。** 如果僅僅是尾部模糊匹配,索引不會失效。如果是頭部模糊匹配,索引失效。 ~~~ mysql> explain select * from tb_seller where name like '%黑馬程序員'; ~~~ ![](https://img.kancloud.cn/14/41/144105648f8b74a2032a26b4518e77f1_2248x452.png) **2.9 如果MySQL評估使用索引比全表更慢,則不使用索引。** address 是有索引的,但是查詢時并不會走索引 address 的內容單一且全部都是重復的,mysql 判定走索引比全表更慢,所以查詢不能使用索引。 ![](https://img.kancloud.cn/da/6b/da6bbf9a0f58042dba0cbe522c593ae4_2880x1358.png) 其實這個字段不適合加索引,我們這只做測試做。 **2.10). is NULL , is NOT NULL 有時索引失效** 將其中一個name 修改成NUll ,測試mysql索引優化選擇器。 ![](https://img.kancloud.cn/c6/29/c62916dfdfcdd90abcb64bd8255c0ef6_2736x1216.png) 可以看到,并不是is NULL , is NOT NULL完全不走索引,Mysql 內部會自動判斷是否會要使用索引,mysql 判定走索引比全表更慢,就不會使用索引。 **2.11). in 走索引, not in 索引失效** ![](https://img.kancloud.cn/e0/8a/e08acb31de648ce9b9bd6e2a1a511841_2736x436.png) 盡量使用復合索引,而少使用單列索引 。 創建復合索引 ``` create index idx_name_sta_address on tb_seller(name, status, address); 就相當于創建了三個索引 : name name + status name + status + address ``` 創建單列索引 ``` create index idx_seller_name on tb_seller(name); create index idx_seller_status on tb_seller(status); create index idx_seller_address on tb_seller(address); ``` 數據庫會選擇一個最優的索引(辨識度最高索引)來使用,并不會使用全部索引 。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看