偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

[[391872]]

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

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

 

  1. # 建表時指定索引 
  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)建索引所需權限

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

 

  1. # 測試用戶的權限 
  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權限 賦予index權限后再執(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 權限,使用 create index 方式創(chuàng)建索引需要 index 權限。

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

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

責任編輯:華軒 來源: MySQL技術
相關推薦

2021-04-08 20:50:17

創(chuàng)建索引MySQL

2022-10-26 07:21:15

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

2020-12-09 18:16:48

容器云開發(fā)CaaS

2020-04-24 09:39:13

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

2017-12-22 10:48:00

AI深度學習遷移學習

2018-03-16 10:36:56

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

2019-11-25 21:46:12

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

2019-07-18 05:00:31

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

2016-01-29 16:02:06

虛擬化

2015-07-15 16:53:55

IP游戲基礎知識

2024-04-10 12:36:41

硬件代碼

2021-06-15 06:50:08

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

2016-02-19 09:33:14

無線知識無線技術2016

2013-07-03 10:48:58

設計師iOS應用iOS人機交互

2019-05-21 16:19:46

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

2021-10-25 14:55:38

Linux技巧命令

2013-03-20 17:58:41

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

2021-06-11 09:33:33

索引SQL語句

2015-10-26 09:19:28

PHP經(jīng)驗

2021-04-27 22:27:19

手機安卓蘋果
點贊
收藏

51CTO技術棧公眾號