- 索引的概念
- 資料庫建立索引的原則
- 查看索引
- 語法格式
- 示例
- 顯示資訊蠶食描述
- 普通索引
- 概述
- 創建普通索引
- 創建方式
- 示例
- 洗掉索引
- 洗掉索引的方式
- 示例
- 唯一索引
- 概述
- 創建唯一索引
- 語法格式
- 示例
- 主鍵索引
- 概述
- 創建主鍵索引
- 全文索引
- 概述
- 創建全文索引
- 創建方法
- 示例
- 組合索引
- 概述
- 創建組合索引
- 創建方式
索引的概念
- 是一個排序的串列,存盤著索引值和這個值所對應的物理地址
- 無須對整個表進行掃描,通過物理地址就可以找到所需資料
- 是表中一列或者若干列值排序的方法
- 需要額外的磁盤空間
資料庫建立索引的原則
- 確定針對該表的操作是大量的查詢操作還是大量的增刪改操作;
- 嘗試建立索引來幫助特定的查詢,檢查自己的sql陳述句,為那些頻繁在where子句中出現的欄位建立索引;
- 嘗試建立復合索引來進一步提高系統性能,修改復合索引將消耗更長時間,同時復合索引也占磁盤空間;
- 對于小型的表,建立索引可能會影響性能;
- 應該避免對具有較少值的欄位進行索引;
- 避免選擇大型資料型別的列作為索引,
查看索引
語法格式
SHOW INDEX FROM 表名;
SHOW KEYS FROM表名 ;
示例
mysql> create table grade(
-> 學號 int(16) not null,
-> 姓名 char(16) not null,
-> 班級 char(16) not null,
-> 成績 int(3) not null,
-> primary key(學號));
Query OK, 0 rows affected (0.01 sec)
mysql> show keys from grade; //第一種查看方式
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.01 sec)
mysql> show index from grade; //第二種查看方式
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
mysql> show index from grade \G; //末尾加上\G表示豎向查看
*************************** 1. row ***************************
Table: grade
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: 學號
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
顯示資訊蠶食描述
| 引數 | 描述 |
|---|---|
| Table | 表的名稱 |
| Non_unique | 索引值得唯一性,0表示唯一,1表示不唯一 |
| Key_name | 索引的名稱 |
| Seq_in_index | 索引的序列號,從1開始 |
| Column_name | 列的名稱 |
普通索引
概述
- 最基本的索引型別,沒有唯─性之類的限制
- 創建普通索引的方式
創建普通索引
創建方式
1. 創建表時創建索引
2. CREATE INDEX 索引名 ON 表名 (列名);
3. ALTER TABLE 表名 ADD INDEX 索引名 (列名);
示例
mysql> create table ltp(
-> id int(4) not null primary key auto_increment,
-> name varchar(10) not null,
-> score decimal not null,
-> hobby int(2) not null default '1',
-> index index_scrore (score));
Query OK, 0 rows affected (0.01 sec)
mysql> show keys from ltp;
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tp | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| tp | 1 | index_scrore | 1 | score | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
mysql> create index name on grade(姓名); //使用create方式新增索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show keys from grade;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 1 | name | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> alter table grade add index 姓名(姓名); //使用alter方式新建索引
Query OK, 0 rows affected, 1 warning (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show keys from grade; //查看到新增了一條姓名索引
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 1 | name | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 1 | 姓名 | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
洗掉索引
洗掉索引的方式
DROP INDEX 索引名 ON 表名;
ALTERTABLE 表名 DROP INDEX 索引名;
示例
mysql> drop index name on grade; //洗掉name索引
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from grade; //查看,name索引已經被洗掉
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 1 | 姓名 | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> alter table grade drop index 姓名; //洗掉姓名索引
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show keys from grade; //查看,洗掉成功
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
唯一索引
概述
- “普通索引”基本相同
- 與普通索引的區別是索引列的所有值只能出現一次,即必須唯一
- 創建唯一索引的方式
創建唯一索引
語法格式
1.創建表時創建索引
2.CREATE UNIQUE INDEX 索引名 ON 表名(列名);
3.ALTER TABLE 表名 ADD UNIQUE 索引名(列名);
示例
mysql> create table lllx ( //創建表的方式創建
-> id int(4) not null primary key auto_increment,
-> name varchar(10) not null,
-> score decimal not null,
-> hobby int(2) not null default '1',
-> unique index index_scrore (score));
Query OK, 0 rows affected (0.01 sec)
mysql> show keys from lllx;
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| lllx | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| lllx | 0 | index_scrore | 1 | score | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> create unique index name on grade(姓名); //新建name索引
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show keys from grade; //查看索引,新建成功
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 0 | name | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> alter table grade drop index name; //洗掉name索引
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table grade add unique 姓名(姓名); //新建姓名索引
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from grade; //查看,新建成功
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 0 | 姓名 | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> alter table grade drop index 姓名; //洗掉姓名索引
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
主鍵索引
概述
- 是一種特殊的唯一索引,指定為“PRIMARY KEY",
- 一個表只能有一個主鍵,不允許有空值
- 創建表時必須創建,創建后不能洗掉
創建主鍵索引
mysql> create table test( //創建表的方式創建索引
-> id int(10) not null auto_increment,
-> title char(255) not null,
-> primary key (`id`));
Query OK, 0 rows affected (0.01 sec)
mysql> show keys from test;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
全文索引
概述
- MySQL從3.23.23版開始支持全文索引和全文檢索
- 索引型別為FULLTEXT
- 可以在CHAR、VARCHAR或者TEXT型別的列上創建
創建全文索引
創建方法
在創建表時創建索引
CREATE FULLTEXT INDEX 索引名 ON 表名(列名);
ALTER TABLE 表名 ADD FULLTEXT 索引名(列名);
示例
mysql> create table article ( 新建方式創建索引
-> 標題 char(48) not null,
-> 目錄 varchar(255) default null,
-> 正文 varchar(8096) not null,
-> primary key (標題),
-> fulltext (正文));
Query OK, 0 rows affected (0.34 sec)
mysql> show keys from article;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| article | 0 | PRIMARY | 1 | 標題 | A | 0 | NULL | NULL | | BTREE | | |
| article | 1 | 正文 | 1 | 正文 | NULL | 0 | NULL | NULL | | FULLTEXT | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> alter table article add fulltext page(目錄); //alter方式新創建page索引
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show keys from article; //查看,創建成功
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| article | 0 | PRIMARY | 1 | 標題 | A | 0 | NULL | NULL | | BTREE | | |
| article | 1 | 正文 | 1 | 正文 | NULL | 0 | NULL | NULL | | FULLTEXT | | |
| article | 1 | page | 1 | 目錄 | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
mysql> drop index page on article; //洗掉page索引
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create fulltext index mulu on article(目錄); //使用create方式創建mulu索引
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from article; 查看,創建成功
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| article | 0 | PRIMARY | 1 | 標題 | A | 0 | NULL | NULL | | BTREE | | |
| article | 1 | 正文 | 1 | 正文 | NULL | 0 | NULL | NULL | | FULLTEXT | | |
| article | 1 | mulu | 1 | 目錄 | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
組合索引
概述
- 可以是單列上創建的索引,也可以是在多列上創建的索引
- 最左原則,從左往右依次執行
- 創建組合索引的方式
創建組合索引
創建方式
1.創建表時創建索引
2.CREATE UNIQUE INDEX 索引名 ON 表名(列名1,列名2,……);
3.ALTER TABLE 表名 ADD UNIQUE 索引名(列名1,列名2,……);
mysql> create table users ( //創建表的方式創建
-> name char(9),
-> age int(3),
-> sex tinyint(1),
-> index user (name,age,sex));
Query OK, 0 rows affected (0.00 sec)
mysql> show keys from user;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user | 1 | user | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| user | 1 | user | 2 | age | A | 0 | NULL | NULL | YES | BTREE | | |
| user | 1 | user | 3 | sex | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.01 sec)
mysql> create unique index student on grade(學號,姓名,成績); //給學號,姓名,成績這幾列創建索引,名為student
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from grade; //查看,創建成功
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 0 | student | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 0 | student | 2 | 姓名 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 0 | student | 3 | 成績 | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)
mysql> drop index student on grade; //洗掉student索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table grade add fulltext 學生(姓名,班級); //給姓名,班級兩列創建全文索引,索引名為學生
Query OK, 0 rows affected, 1 warning (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show keys from grade; 查看,創建成功
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| grade | 0 | PRIMARY | 1 | 學號 | A | 0 | NULL | NULL | | BTREE | | |
| grade | 1 | 學生 | 1 | 姓名 | NULL | 0 | NULL | NULL | | FULLTEXT | | |
| grade | 1 | 學生 | 2 | 班級 | NULL | 0 | NULL | NULL | | FULLTEXT | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/26908.html
標籤:其他
上一篇:MySQL中的列轉行
