Mysql數據庫的索引和視圖詳解

Mysql數據庫的索引和視圖詳解

Mysql數據庫的索引和視圖詳解

索引的概念

數據庫的索引與書籍中的目錄類似
在一本書中,無需閱讀整本書,利用目錄就可以快速查找所需信息
書中的目錄是一個詞語列表,其中註明了包含各個詞的頁碼
數據庫索引
在數據庫中,索引數據庫程序無需對整個表進行掃描,就可以在其中找到所需數據
數據庫中的索引是某個表中一列或若干列的集合,以及物理標識這些值的數據頁的邏輯指針清單

索引的作用

設置了合適的索引之後,數據庫利用葛總快速的定位技術,能夠大大加快查詢速率
特別是當表很大時,或者查詢涉及到多個表時,使用索引可使查詢加快成千倍
可以降低數據庫的IO成本,並且索引還可以降低數據庫的排序成本
通過創建唯一索引保證數據表數據的唯一性
可以加快表與表之間的連接
在使用分組和排序時,可大大減少分組和排序時間

索引分類

普通索引
這是最基本的索引類型,而且它沒有唯一性的限制
唯一性索引
索引的列的所有值都只能出現一次,即必須唯一
主鍵
主鍵是一種唯一性索引,但它必須指定爲“PRIMARY KEY”
全文索引
全文索引可以在VARCHAR或者TEXT類型的列上創建

創建索引的原則依據

表的主鍵,外鍵必須有索引
數據量超過300行的表應該有索引
經常與其他表進行連接的表,在連接字段上應該建立索引
唯一性太差的字段不適合建立索引
更新太頻繁的字段不適合創建索引
經常出現在Where字句中的字段,特別是大表的字段,應該建立索引
索引應該建在選擇性高的字段上
索引應該建在小字段上,對於大的文本字段甚至超長字段,不要建索引

索引原理圖

Mysql數據庫的索引和視圖詳解

普通索引

普通索引結構語句

create index 索引名字 on tablename(列的列表

create index 索引名字 on tablename(列的列表)
[root@localhost ~]# mysql -u root -p #進入mysql數據庫
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;  #查看所有數據庫

mysql> use school; #創建school數據庫
Database changed
mysql> create table info (    #創建數據表
id int(4) not null primary key auto_increment, #int類型整型爲4,不能爲空,主鍵索引,數值自然增長
name varchar(10) not null,  #varchar字符串不能爲空
address varchar(50) default 'nanjing',  #字符串默認是nanjing
age int(3) not null); #int類型

Query OK, 0 rows affected (0.05 sec)

mysql> insert into info (name,address,age) values ('zhangsan','beijing',20),('lisi','shanghai',22);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * form info; #查看數據表
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'form info' at line 1
mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> desc info; #查看錶結構
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(4)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10) | NO   |     | NULL    |                |
| address | varchar(50) | YES  |     | nanjing |                |
| age     | int(3)      | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> create index index_age on info (age); #創建索引固定搭配,index_age索引作用在info表中的age列
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info; #查看數據表中的索引
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY   |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | index_age |            1 | age         | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> drop index index_age on info; #刪除數據表中的index_age的索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY  |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

唯一索引

唯一索引結構語句

create unique index 索引的名字 on tablename (列的列表)

mysql> create unique index unique_name on info (name); #創建唯一索引,create unique index固定搭配起個名字,作用在info的name列中
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY     |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | unique_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> alter table info add unique index index_name (name); #另一種方法alter table info add 唯一索引 索引名字,作用在name列名中
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

定義索引的三種方式

1.創建表的時候 直接定義
2.create index 索引名稱 on 表名 (列名1,列名2);列名可以是多個
3.alter table 表名 add index 索引名稱 (列名);

mysql> alter table info add unique index index_name (name); #另一種方法alter table info add 唯一索引 索引名字,作用在name列名中
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> create table user (
    -> 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_score (score));  #在創建表的時候可以直接定義索引
Query OK, 0 rows affected (0.05 sec)

mysql> desc user;
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | int(4)        | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)   | NO   |     | NULL    |                |
| score | decimal(10,0) | NO   | MUL | NULL    |                |
| hobby | int(2)        | NO   |     | 1       |                |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

做兩張表做索引查詢,兩張表合在一起查看

#要對應着列名填寫數據
mysql> insert into user (name,score,hobby) values ('test01',88,1),('stu01',99,2),('wangwu',77,3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from user;
+----+--------+-------+-------+
| id | name   | score | hobby |
+----+--------+-------+-------+
|  1 | test01 |    88 |     1 |
|  2 | stu01  |    99 |     2 |
|  3 | wangwu |    77 |     3 |
+----+--------+-------+-------+
3 rows in set (0.00 sec)

再創建一個表做與上一個表做相連查詢

mysql> create table hob (  
    -> id int (2) not null primary key,
    -> hob_name varchar(10) not null);
Query OK, 0 rows affected (0.04 sec)

mysql> desc hob;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(2)      | NO   | PRI | NULL    |       |
| hob_name | varchar(10) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into hob (id,hob_name) values (1,'看書'),(2,'運動'),(3,'跑步');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from hob;
+----+----------+
| id | hob_name |
+----+----------+
|  1 | 看書     |
|  2 | 運動     |
|  3 | 跑步     |
+----+----------+
3 rows in set (0.00 sec)

mysql> insert into user (name,score,hobby) values ('zhaoliu',66,2); #在user表中再插一行數據
Query OK, 1 row affected (0.00 sec)

mysql> select * from user inner join hob on user.hobby=hob.id; #把user這種表加入到hob表中,user的hobby對應hob裏面的id
+----+---------+-------+-------+----+----------+
| id | name    | score | hobby | id | hob_name |
+----+---------+-------+-------+----+----------+
|  1 | test01  |    88 |     1 |  1 | 看書     |
|  2 | stu01   |    99 |     2 |  2 | 運動     |
|  3 | wangwu  |    77 |     3 |  3 | 遊     |
|  4 | zhaoliu |    66 |     2 |  2 | 運動     |
+----+---------+-------+-------+----+----------+
4 rows in set (0.00 sec)

看這個表數據覺得不合理,現在需求只要名字和愛好

mysql> select user.name,hob.hob_name from user inner join hob on user.hobby=hob.id;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看書     |
| stu01   | 運動     |
| wangwu  | 跑步     |
| zhaoliu | 運動     |
+---------+----------+
4 rows in set (0.00 sec)

表名別名關聯查詢

mysql> select u.name,h.hob_name from user u inner join hob h on u.hobby=h.id;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看書     |
| stu01   | 運動     |
| wangwu  | 跑步     |
| zhaoliu | 運動     |
+---------+----------+
4 rows in set (0.00 sec)

創捷視圖

創建視圖結構語句

create view 視圖名 as
視圖建立一個映射,把結果呈現出來,真實的數據還在原有表中

mysql> create view view_user as select u.name,h.hob_name from user u inner join hob h on u.hobby=h.id;

Query OK, 0 rows affected (0.00 sec)
mysql> select * from view_user;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看書     |
| stu01   | 運動     |
| wangwu  | 跑步    |
| zhaoliu | 運動     |
+---------+----------+
4 rows in set (0.00 sec)

全文索引

給長的字段,文段做索引

mysql> create fulltext index full_addr on info (address);
Query OK, 0 rows affected, 1 warning (0.21 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | full_addr  |            1 | address     | NULL      |           2 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

組合索引

mysql> create index index_name_score on user (name,score);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from user;
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY          |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_score      |            1 | score       | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            1 | name        | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            2 | score       | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章