一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Mysql - MYSQL創建索引,這些知識應該了解

MYSQL創建索引,這些知識應該了解

2021-04-08 23:29MySQL技術 Mysql

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

MYSQL創建索引,這些知識應該了解

 1.創建索引方法

 

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

  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. # 創建索引(兩種方法) 
  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. # 聯合索引 
  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.創建索引所需權限

 

如果你用的不是 root 賬號,那創建索引就要考慮權限問題了,是不是需要 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 方式創建索引 
  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 方式創建索引 
  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 方式創建索引還需要index權限 賦予index權限后再執行 
  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 方式創建索引需要 alter 權限,使用 create index 方式創建索引需要 index 權限。

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

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

原文地址:https://mp.weixin.qq.com/s?__biz=MzI2NTA3OTY2Nw==&mid=2647636131&idx=1&sn=3e421df081e97c544c102e21b3a30ea9&chksm=f299c6bfc5ee4fa98b31e6620975332213ae239d3919ccddb1100b07831a08c4ac6a4919eb88&mpshare=1&

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品原创巨作无遮挡 | 日本高清中文字幕一区二区三区 | 好男人好资源在线观看免费 | 日韩欧美在线看 | 成人国产一区 | 欧美日韩精品免费一区二区三区 | 91精品综合国产在线观看 | 男人j放进女人的p免费看视频 | 国内体内she精视频免费 | 国产精品日韩欧美在线 | 四虎在线视频免费观看视频 | 亚洲波多野结衣日韩在线 | 女黑人尺寸bbb | 扒开老师挠尿口到崩溃刑罚 | 日产免费自线一二区 | 国产福利一区二区在线精品 | 国产精品第1页在线播放 | 麻豆性视频 | 国产一二三区视频 | 日本强不卡在线观看 | 日本三级做a全过程在线观看 | 日韩一区二区三区不卡视频 | 暖暖 免费 高清 日本 在线 | 亚洲 欧美 偷自乱 图片 | 日韩精品免费一级视频 | 动漫美女胸被狂揉扒开吃奶动态图 | 精品无人区麻豆乱码无限制 | 99资源站| 久久中文字幕无线观看 | 操破苍穹h| 青青草国产一区二区三区 | 成人综合网站 | 天天操天天干天天做 | 国产高清在线看 | 91热这里只有精品 | 日韩精选在线 | 免费观看www视频 | 大胸被c出奶水嗷嗷叫 | 四虎影视国产精品婷婷 | 邪恶肉肉全彩色无遮琉璃神社 | 我的年轻漂亮继坶三级 |