MYSQL之索引演算法分類
阿新 • • 發佈:2020-07-17
1.什麼是索引
1.索引就好比一本書的目錄,它能讓你更快的找到自己想要的內容。
2.讓獲取的資料更有目的性,從而提高資料庫檢索資料的效能。
2.索引的種類
1.BTREE: B+樹索引(Btree,B+tree,B*tree)
2.HASH:HASH索引(memery儲存引擎支援)
3.FULLTEXT:全文索引(myisam儲存引擎支援)
4.RTREE:R樹索引
3.索引根據演算法分類
索引是建立在資料庫欄位上面的
當where條件後面接的內容有索引的時候,會提高速度
1.主鍵索引(聚集索引)
# 建立表的時候建立主鍵索引 mysql> create table test(id int not null auto_increment primary key comment '學號'); Query OK, 0 rows affected (0.04 sec) mysql> create table test1(id int not null auto_increment,primary key(id)); Query OK, 0 rows affected (0.04 sec) # 檢視索引命令 mysql> show index from test; # 已經有表時新增主鍵索引 mysql> alter table student add primary key pri_id(id);
2.唯一鍵索引
# 建立表的時候建立唯一鍵索引 mysql> create table test2(id int not null auto_increment unique key comment '學號'); Query OK, 0 rows affected (0.04 sec) # 已經有表時新增唯一鍵索引 mysql> alter table student add unique key uni_key(name); # 注意:建立唯一建索引或主鍵索引的列不能有重複資料 判斷一列能否做唯一建索引 1.查詢資料總量 mysql> select count(name) from city; 2.去重檢視該列資料總量 mysql> select count(distinct(name)) from city; # 以上兩個值相等則可以設定唯一建索引
例:
# 1.檢視列的總資料量 mysql> select count(name) from country; +-------------+ | count(name) | +-------------+ | 239 | +-------------+ 1 row in set (0.00 sec) # 2.檢視去重後資料量 mysql> select count(distinct(name)) from country; +-----------------------+ | count(distinct(name)) | +-----------------------+ | 239 | +-----------------------+ 1 row in set (0.00 sec) # 3.建立唯一建索引 mysql> alter table country add unique key uni_key(name); Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0
3.普通索引(輔助索引)
mysql> alter table city add index inx_name(name);
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index index_District on city(District);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
4.全文索引
mysql> create table txt(id int,bookname varchar(12),wenzhang text,fulltext(wenzhang));
Query OK, 0 rows affected (0.20 sec)
mysql> select * from txt where match(wenzhang) against('查詢的內容');
#例項
mysql> create table text(id int,bookname varchar(12) charset utf8,wenzhang text charset utf8,fulltext(wenzhang));
Query OK, 0 rows affected (0.21 sec)
mysql> insert into text values(1,'紅樓夢','上回書說到張飛長阪坡三打白骨精救出宋江');
Query OK, 1 row affected (0.01 sec)
mysql> select * from text;
+------+-----------+-----------------------------------------------------------+
| id | bookname | wenzhang |
+------+-----------+-----------------------------------------------------------+
| 1 | 紅樓夢 | 上回書說到張飛長阪坡三打白骨精救出宋江 |
+------+-----------+-----------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from text where match(wenzhang) against('上回書說到張飛長阪坡三打白骨精救出宋江');
+------+-----------+-----------------------------------------------------------+
| id | bookname | wenzhang |
+------+-----------+-----------------------------------------------------------+
| 1 | 紅樓夢 | 上回書說到張飛長阪坡三打白骨精救出宋江 |
+------+-----------+-----------------------------------------------------------+
5.檢視索引
方式一:
mysql> show index from city;
#方式二:
mysql> desc city;
+-----+
| Key |
+-----+
| PRI | #主鍵索引
| MUL | #普通索引
| UNI | #唯一鍵索引
| MUL |
+-----+
6.刪除索引
mysql> alter table city drop index index_District;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
# 刪除普通索引
mysql> alter table city drop priary key;
# 刪除主鍵索引