索引在MySQL中叫做"键",是存储引擎用于快速找到记录的一种数据结构。索引对于良好的性能非常关键。
当表中数据量比较大,索引对于性能的影响比较重要。
索引优化是对查询性能优化最有效的手段,能够将查询性能提高好几个数量级。
索引分类:1、普通索引 2、唯一索引(unique) 3、全文索引(fulltext)
4、单列索引 5、多列索引 6、空间索引
创建索引:
1、 创建表时候创建索引:create table 表名(字段1 类型 约束,字段2 类型 约束,索引修饰符 index 索引名(字段));
create table t2(id int.name varchar(50),index id_index(id));
2、表已经存在创建索引
create 索引修饰符 index 索引名 on 表名(字段);
create unqiue index id_index on t2(id);
3、表已经存在创建索引
alter table 表名 add 索引修饰符 index 索引名 (字段);
alter table t2 add fulltext index name_index(name);
管理索引:
1、show create table 表名\G
删除索引:
1、drop index 索引名 on 表名;
准备实验环境:
准备表t1(id int,name varchar(50));里面存储20万条数据
create table t2(id int,name varchar(50));
创建一个存储过程自动存储20万条数据
\d
Query OK, 0 rows affected (0.01 sec)
mysql> \d ;
mysql> call autoinsert(); 执行自动插入数据的存储过程
Query OK, 1 row affected (25.67 sec)
不建立索引前查询数据:
mysql> select * from t2 where id=198768;
±-------±-------+
| id | name |
±-------±-------+
| 198768 | gz2003 |
±-------±-------+
1 row in set (0.04 sec) 花费时间0.04秒
对t2表创建普通索引
mysql> create index id_index on t2(id);
Query OK, 0 rows affected (0.21 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table t2\G 查看索引 可以看到id_index这个索引名称
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE t2 (
id int(11) DEFAULT NULL,
name varchar(50) DEFAULT NULL,
KEY id_index (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)mysql> select * from t2 where id=198768; 再次查询id=198768 时间为0 性能提高
±-------±-------+
| id | name |
±-------±-------+
| 198768 | gz2003 |
±-------±-------+
1 row in set (0.00 sec)
视图:视图是数据库另一种虚拟的表,其内容由查询定义。和真实的表一样,视图包含一系列带有名称的列和行数据,但是视图不在数据库中以存储的数据形式存在。对视图进行查询没有任何限制,通过他们进行数据修改限制也比较少。
视图存在原因:1、安全,视图可以隐藏一些数据,不能被入侵的人查看到
2、可以使复杂的查询简单化
对于基本表操作,都可以对视图进行操作,并且操作结果也会反馈在基本表上。
创建视图语句: create view 视图名称 as select 语句;
查看视图: 1、show tables; 查看所有表
2、show table status;查看所有表的详细信息
3、show table status from mysql like ‘user_info’;查看来自mysql数据库,user_info表的详细信息
4、show table status from mysql;查看来自数据库mysql所有表详细信息
5、show create view uer_info\G 查看视图user_info创建过程
6、desc user_info;查看视图的表结构
修改视图: alter view 视图名 as select 语句;
删除视图: drop view 视图名。
通过试图操作基本表: 通过对视图数据的更新删除插入都会表现在基本表上
案例:用之前员工表emp1和部门表dep1创建一个员工信息表,包含员工id,姓名,年龄,部门名称视图表user_info
创建视图表:
mysql> create view user_info as select emp_id,emp_name,age,dept_name from enp1 inner join dep1 on emp1.dept_id=dep1.dept_id;
查看试图内容
mysql> select * from user_info;
±-------±---------±-----±----------+
| emp_id | emp_name | age | dept_name |
±-------±---------±-----±----------+
| 1 | tianyun | 19 | hr |
| 2 | tom | 26 | it |
| 3 | jack | 30 | it |
| 4 | alice | 24 | sale |
| 5 | robin | 40 | hr |
±-------±---------±-----±----------+
5 rows in set (0.00 sec)
查看视图创建过程:
mysql> show create view user_info\G
*************************** 1. row ***************************
View: user_info
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER VIEW user_info AS select emp1.emp_id AS emp_id,emp1.emp_name AS emp_name,emp1.age AS age,dep1.dept_name AS dept_name from (emp1 join dep1 on((emp1.dept_id = dep1.dept_id)))
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
修改视图数据,将hr部门人年龄都修改为50
mysql> update user_info set age=50 where dept_name=‘hr’;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
验证基础表数据是否发生变化,hr部门id为200,可以发现他们年龄都变为50
mysql> select * from emp1;
±-------±---------±-----±--------+
| emp_id | emp_name | age | dept_id |
±-------±---------±-----±--------+
| 1 | tianyun | 50 | 200 |
| 2 | tom | 26 | 201 |
| 3 | jack | 30 | 201 |
| 4 | alice | 24 | 202 |
| 5 | robin | 50 | 200 |
| 6 | natasha | 28 | 204 |
±-------±---------±-----±--------+
6 rows in set (0.00 sec)
对单个表能做的操作都可以对视图进行操作进行验证,各位小伙伴自己验证一下