1. 程式人生 > >程序媛計劃——mysql索引

程序媛計劃——mysql索引

ted style -- msg weight blog alter type es2017

定義:   索引是一種單獨的、物理的對數據庫表中一列或多列的值進行排序的一種存儲結構 #為字段創建索引
#在表中的字段中創建索引
mysql> create index ind_score on score(name); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 #查看索引 mysql> show index from score; +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| score | 0 | PRIMARY | 1 | id | A | 4 | NULL | NULL | | BTREE | | | | score | 1 | ind_score | 1 | name | A | 4 | NULL | NULL | | BTREE | | |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 2 rows in set (0.00 sec)

#也可以在創建表的時候創建索引

技術分享

#刪除索引

mysql> drop index ind_score on score;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

#用alter創建/刪除索引

mysql> alter table score add index ind_score(name);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table score drop index ind_score;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

#創建索引值唯一的索引(好處是數據很多時能通過唯一索引快速查詢到數據)

mysql> create unique index uni_index on score(score);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

程序媛計劃——mysql索引