自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

[[391872]]

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

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

 

  1. # 建表時(shí)指定索引 
  2. CREATE TABLE `t_index` ( 
  3.   `increment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵'
  4.   `col1` int(11) NOT NULL
  5.   `col2` varchar(20) NOT NULL
  6.   `col3` varchar(50) NOT NULL
  7.   `col4` int(11) NOT NULL
  8.  `col5` varchar(50) NOT NULL
  9.   PRIMARY KEY (`increment_id`), 
  10.   UNIQUE KEY `uk_col1` (`col1`), 
  11.   KEY `idx_col2` (`col2`) 
  12. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='測試索引'
  13.  
  14. # 創(chuàng)建索引(兩種方法) 
  15. # 普通索引 
  16. alter table `t_index` add index idx_col3 (col3);  
  17. create index idx_col3 on t_index(col3); 
  18. # 唯一索引 
  19. alter table `t_index` add unique index uk_col4 (col4); 
  20. create unique index uk_col4 on t_index(col4); 
  21. # 聯(lián)合索引 
  22. alter table `t_index` add index idx_col3_col4 (col3,col4); 
  23. create index idx_col3_col4 on t_index(col3,col4); 
  24. # 前綴索引 
  25. alter table `t_index` add index idx_col5 (col5(20));  
  26. create index idx_col5 on t_index(col5(20)); 
  27.  
  28. # 查看表索引 
  29. mysql> show index from t_index; 
  30. +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  31. Table   | Non_unique | Key_name | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
  32. +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  33. | t_index |          0 | PRIMARY  |            1 | increment_id | A         |           0 |     NULL | NULL   |      | BTREE      |         |               | 
  34. | t_index |          0 | uk_col1  |            1 | col1         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               | 
  35. | t_index |          1 | idx_col2 |            1 | col2         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               | 
  36. | t_index |          1 | idx_col3 |            1 | col3         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               | 
  37. +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 

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

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

 

  1. # 測試用戶的權(quán)限 
  2. mysql> show grants; 
  3. +-------------------------------------------------------------------------------------+ 
  4. | Grants for testuser@%                                                               | 
  5. +-------------------------------------------------------------------------------------+ 
  6. GRANT USAGE ON *.* TO 'testuser'@'%'                                                | 
  7. GRANT SELECTINSERTUPDATEDELETECREATEALTER ON `testdb`.* TO 'testuser'@'%' | 
  8. +-------------------------------------------------------------------------------------+ 
  9.  
  10. alter table 方式創(chuàng)建索引 
  11. mysql> alter table `t_index` add index idx_col2 (col2); 
  12. Query OK, 0 rows affected (0.05 sec) 
  13. Records: 0  Duplicates: 0  Warnings: 0 
  14.  
  15. create index 方式創(chuàng)建索引 
  16. mysql>  create index idx_col3 on t_index(col3); 
  17. ERROR 1142 (42000): INDEX command denied to user 'testuser'@'localhost' for table 't_index' 
  18.  
  19. create index 方式創(chuàng)建索引還需要index權(quán)限 賦予index權(quán)限后再執(zhí)行 
  20. mysql> create index idx_col3 on t_index(col3); 
  21. Query OK, 0 rows affected (0.04 sec) 
  22. Records: 0  Duplicates: 0  Warnings: 0 

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

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

索引的優(yōu)點(diǎ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ù)來考慮的,一個(gè)表中建議不要加過多索引。

責(zé)任編輯:華軒 來源: MySQL技術(shù)
相關(guān)推薦

2021-04-08 20:50:17

創(chuàng)建索引MySQL

2020-12-09 18:16:48

容器云開發(fā)CaaS

2022-10-26 07:21:15

網(wǎng)絡(luò)視頻開發(fā)

2018-03-16 10:36:56

SSD固態(tài)硬盤閃存

2019-11-25 21:46:12

數(shù)據(jù)湖云計(jì)算數(shù)據(jù)倉庫

2019-07-18 05:00:31

ARPIP網(wǎng)絡(luò)協(xié)議

2020-04-24 09:39:13

網(wǎng)絡(luò)攻擊惡意軟件網(wǎng)絡(luò)安全

2017-12-22 10:48:00

AI深度學(xué)習(xí)遷移學(xué)習(xí)

2016-01-29 16:02:06

虛擬化

2015-07-15 16:53:55

IP游戲基礎(chǔ)知識(shí)

2024-04-10 12:36:41

硬件代碼

2021-06-15 06:50:08

索引字段數(shù)據(jù)

2013-07-03 10:48:58

設(shè)計(jì)師iOS應(yīng)用iOS人機(jī)交互

2016-02-19 09:33:14

無線知識(shí)無線技術(shù)2016

2019-05-21 16:19:46

前端性能優(yōu)化圖片

2021-06-11 09:33:33

索引SQL語句

2021-10-25 14:55:38

Linux技巧命令

2013-03-20 17:58:41

虛擬內(nèi)存程序員

2021-04-27 22:27:19

手機(jī)安卓蘋果

2023-03-02 11:52:00

自定義自動(dòng)配置
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)