實例介紹MySQL索引的使用

coldplay.xixi
發布: 2020-12-11 17:34:16
轉載
7805 人瀏覽過

mysql教學專欄實例講解MySQL索引的使用實例介紹MySQL索引的使用

#更多相關免費學習推薦:mysql教學(影片)

MySQL索引的使用實例

    • ##MySQL索引的使用實例
  • ##MySQL索引

二. 查詢分析器-explain

##三. 索引的基本使用

實例介紹MySQL索引的使用四.複合索引

五. 覆寫索引實例介紹MySQL索引的使用

實例介紹MySQL索引的使用
一. 慢查詢日誌

//查看是否开启慢查询日志
mysql> show variables like '%slow%';//临时开启慢查询日志
mysql> set global slow_query_log=ON;//查看是否开启慢查询日志
mysql> show variables like '%slow%';
登入後複製

實例介紹MySQL索引的使用

#
//查询超过多少时间就可以记录,上面是如果超过10秒就要记录
mysql> show variables like '%long%';//改成一秒,如果超过一秒就写到慢日志里面去(一般一秒是最好的)mysql> set long_query_time=1;//查看日记存储方式,默认FILE
mysql> show variables like '%log_output%';// 慢查询日志文件所在位置
mysql> show variables like '%datadir%';
登入後複製

//响应时间是3秒,超过了原先设定的一秒
mysql> select sleep(3);
登入後複製
## 我們去資料夾裡面查看時發現它已經被存入慢查詢日記裡面

這部分寫明瞭如何透過慢日誌找出比較慢

#的SQL,後面部分要說

為什麼慢,如何能更快一點。

二. 查詢分析器-explain

實例介紹MySQL索引的使用

作用

:透過這個可以知道查看sql慢在哪裡,需要朝那些方面優化實例介紹MySQL索引的使用

#列:我們建立一個

employee資料表

create table employee(
	id int not null auto_increment primary key,
	name varchar(30) comment '姓名',
	sex varchar(1) comment '性别',
	salary int comment '薪资(元)',
	dept varchar(30) comment '部门');insert into employee(name, sex, salary, dept) values('张三', '男', 5500, '部门A');insert into employee(name, sex, salary, dept) values('李洁', '女', 4500, '部门C');insert into employee(name, sex, salary, dept) values('李小梅', '女', 4200, '部门A');insert into employee(name, sex, salary, dept) values('欧阳辉', '男', 7500, '部门C');insert into employee(name, sex, salary, dept) values('李芳', '女', 8500, '部门A');insert into employee(name, sex, salary, dept) values('张江', '男', 6800, '部门A');insert into employee(name, sex, salary, dept) values('李四', '男', 12000, '部门B');insert into employee(name, sex, salary, dept) values('王五', '男', 3500, '部门B');insert into employee(name, sex, salary, dept) values('马小龙', '男', 6000, '部门A');insert into employee(name, sex, salary, dept) values('龙五', '男', 8000, '部门B');insert into employee(name, sex, salary, dept) values('冯小芳', '女', 10000, '部门C');insert into employee(name, sex, salary, dept) values('马小花', '女', 4000, '部门B');insert into employee(name, sex, salary, dept) values('柳峰', '男', 8800, '部门A');
登入後複製
實例介紹MySQL索引的使用

//通过explain解读他,后面加一个\G便于阅读
mysql> explain select * from employee where name='柳峰'\G;//扫描快捷
mysql> explain select * from employee where id=13\G;
登入後複製

實例介紹MySQL索引的使用

效果:如下圖,可以看之前為什麼那麼慢,需要四秒回應時間

實例介紹MySQL索引的使用

三.索引的基本使用

mysql> show index from employee\G;//主键会默认建一个id索引
登入後複製
實例介紹MySQL索引的使用

實例介紹MySQL索引的使用建立索引效率提升
//查询分析
mysql> explain select * from employee where name='柳峰';//创建普通索引
mysql> create index idx_name on employee(name);
登入後複製

//删除
mysql> drop index idx_name on employee;
登入後複製
實例介紹MySQL索引的使用

老師事列:

實例介紹MySQL索引的使用

如過用like檢索,效率還是不變,所以要看你怎麼用

實例介紹MySQL索引的使用

四.複合索引

//查的时候可以看到一个主键索引
mysql> show index from employee\G;
登入後複製

實例介紹MySQL索引的使用

#目前是all全域掃描

select * from employee where name ='柳峰';//查询分析
explain select * from employee where name ='柳峰'\G;
登入後複製

實例介紹MySQL索引的使用

建立索引

//创建索引
create index idx_name_salary_dept on employee(name,salary,dept);//查询分析
explain select * from employee where name ='柳峰'\G;
登入後複製

實例介紹MySQL索引的使用

驗證有name就能索引

// name和salary
mysql> explain select * from employee where name ='柳峰' and salary=8800\G;//name和dept
mysql> explain select * from employee where name ='柳峰' and dept='部门A'\G;
登入後複製

##沒有name就不能使用索引

mysql> explain select * from employee where  salary=8800;mysql> explain select * from employee where  dept='部门A';
登入後複製
#五. 覆蓋索引

依照上面步驟,我們可以看到四個索引,第一個是實例介紹MySQL索引的使用主鍵索引
,後面是

複合索引

name_salary_dept

實例介紹MySQL索引的使用

mysql> show index from employee;
登入後複製

如何觸發

實例介紹MySQL索引的使用我們用id作為查詢數據

mysql> select * from employee;mysql> select * from employee where id =11;
登入後複製

實例介紹MySQL索引的使用

###只查id###
mysql> explain select id from employee  employee where id=11\G;mysql> explain select id from employee\G;
登入後複製
##########
//查name,salary
mysql> explain select name,salary from employee;//查name,salary,dept
mysql> explain select name,salary,dept from employee;//因为没有sxe条件,所以只能做全部扫描type为null
mysql> explain select name,sex,salary,dept from employee;
登入後複製
###########

以上是實例介紹MySQL索引的使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!