<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          創(chuàng)建索引,這些知識(shí)應(yīng)該了解

          共 7727字,需瀏覽 16分鐘

           ·

          2021-04-08 10:33

          前言: 


          在 MySQL 中,基本上每個(gè)表都會(huì)有索引,有時(shí)候也需要根據(jù)不同的業(yè)務(wù)場(chǎng)景添加不同的索引。索引的建立對(duì)于數(shù)據(jù)庫(kù)高效運(yùn)行是很重要的,本篇文章將介紹下創(chuàng)建索引相關(guān)知識(shí)及注意事項(xiàng)。


            1.創(chuàng)建索引方法


          創(chuàng)建索引可以在建表時(shí)指定,也可以建表后使用 alter table 或 create index 語(yǔ)句創(chuàng)建索引。下面展示下幾種常見(jiàn)的創(chuàng)建索引場(chǎng)景。


          # 建表時(shí)指定索引
          CREATE TABLE `t_index` (
            `increment_id` int(11NOT NULL AUTO_INCREMENT COMMENT '自增主鍵',
            `col1` int(11NOT NULL,
            `col2` varchar(20NOT NULL,
            `col3` varchar(50NOT NULL,
            `col4` int(11NOT NULL,
           `col5` varchar(50NOT NULL,
            PRIMARY KEY (`increment_id`),
            UNIQUE KEY `uk_col1` (`col1`),
            KEY `idx_col2` (`col2`)
          ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='測(cè)試索引';

          # 創(chuàng)建索引(兩種方法)
          # 普通索引
          alter table `t_index` add index idx_col3 (col3); 
          create index idx_col3 on t_index(col3);
          # 唯一索引
          alter table `t_index` add unique index uk_col4 (col4);
          create unique index uk_col4 on t_index(col4);
          # 聯(lián)合索引
          alter table `t_index` add index idx_col3_col4 (col3,col4);
          create index idx_col3_col4 on t_index(col3,col4);
          # 前綴索引
          alter table `t_index` add index idx_col5 (col5(20)); 
          create index idx_col5 on t_index(col5(20));

          # 查看表索引
          mysql> show index from t_index;
          +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
          | Table   | Non_unique | Key_name | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
          +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
          | t_index |          0 | PRIMARY  |            1 | increment_id | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
          | t_index |          0 | uk_col1  |            1 | col1         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
          | t_index |          1 | idx_col2 |            1 | col2         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
          | t_index |          1 | idx_col3 |            1 | col3         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
          +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+


            2.創(chuàng)建索引所需權(quán)限


          如果你用的不是 root 賬號(hào),那創(chuàng)建索引就要考慮權(quán)限問(wèn)題了,是不是需要 create、alter 權(quán)限就行了呢?下面我們來(lái)具體看下。


          # 測(cè)試用戶的權(quán)限
          mysql> show grants;
          +-------------------------------------------------------------------------------------+
          | Grants for testuser@%                                                               |
          +-------------------------------------------------------------------------------------+
          GRANT USAGE ON *.* TO 'testuser'@'%'                                                |
          GRANT SELECTINSERTUPDATEDELETECREATEALTER ON `testdb`.* TO 'testuser'@'%' |
          +-------------------------------------------------------------------------------------+

          # alter table 方式創(chuàng)建索引
          mysql> alter table `t_index` add index idx_col2 (col2);
          Query OK, 0 rows affected (0.05 sec)
          Records: 0  Duplicates: 0  Warnings: 0

          # create index 方式創(chuàng)建索引
          mysql>  create index idx_col3 on t_index(col3);
          ERROR 1142 (42000): INDEX command denied to user 'testuser'@'localhost' for table 't_index'

          # create index 方式創(chuàng)建索引還需要index權(quán)限 賦予index權(quán)限后再執(zhí)行
          mysql> create index idx_col3 on t_index(col3);
          Query OK, 0 rows affected (0.04 sec)
          Records: 0  Duplicates: 0  Warnings: 0


          從上面測(cè)試可以看出,使用 alter table 方式創(chuàng)建索引需要 alter 權(quán)限,使用 create index 方式創(chuàng)建索引需要 index 權(quán)限。


          另外說(shuō)明下,刪除索引也是可以使用 alter table `tb_name` drop index xxx 和 drop index xxx on tb_name 兩種方式,分別需要 alter 和 index 權(quán)限。


          索引的優(yōu)點(diǎn)顯而易見(jiàn)是可以加速查詢,但創(chuàng)建索引也是有代價(jià)的。首先每建立一個(gè)索引都要為它建立一棵B+樹,會(huì)占用額外的存儲(chǔ)空間;其次當(dāng)對(duì)表中的數(shù)據(jù)進(jìn)行增加、刪除、修改時(shí),索引也需要?jiǎng)討B(tài)的維護(hù),降低了數(shù)據(jù)的維護(hù)速度。所以我們創(chuàng)建索引時(shí)還是需要根據(jù)業(yè)務(wù)來(lái)考慮的,一個(gè)表中建議不要加過(guò)多索引。

          推薦閱讀


          (點(diǎn)擊標(biāo)題可跳轉(zhuǎn)閱讀)

          MySQL鎖等待與死鎖問(wèn)題分析

          MySQL字段默認(rèn)值設(shè)置詳解

          超實(shí)用的索引知識(shí)介紹

          - End -

          動(dòng)動(dòng)手指轉(zhuǎn)發(fā)、在看
          是對(duì)我最大的鼓勵(lì)


          瀏覽 45
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产操逼视频暴操特操 | 精品日产乱码久久久 | 黄色一级片欧美 | 91五月婷婷| 成人黄色电影在线观看 |