mysql教學專欄實例講解MySQL索引的使用
#更多相關免費學習推薦:mysql教學(影片)
MySQL索引的使用實例
- ##MySQL索引的使用實例
##MySQL索引
二. 查詢分析器-explain
##三. 索引的基本使用四.複合索引
五. 覆寫索引
一. 慢查詢日誌
//查看是否开启慢查询日志 mysql> show variables like '%slow%';//临时开启慢查询日志 mysql> set global slow_query_log=ON;//查看是否开启慢查询日志 mysql> show variables like '%slow%';
//查询超过多少时间就可以记录,上面是如果超过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:透過這個可以知道查看sql慢在哪裡,需要朝那些方面優化
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');

//通过explain解读他,后面加一个\G便于阅读
mysql> explain select * from employee where name='柳峰'\G;//扫描快捷
mysql> explain select * from employee where id=13\G;
mysql> show index from employee\G;//主键会默认建一个id索引


//查询分析 mysql> explain select * from employee where name='柳峰';//创建普通索引 mysql> create index idx_name on employee(name);
//删除 mysql> drop index idx_name on employee;

老師事列:
//查的时候可以看到一个主键索引 mysql> show index from employee\G;
select * from employee where name ='柳峰';//查询分析 explain select * from employee where name ='柳峰'\G;
//创建索引 create index idx_name_salary_dept on employee(name,salary,dept);//查询分析 explain select * from employee where name ='柳峰'\G;
// 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> show index from employee;如何觸發
我們用id作為查詢數據
mysql> select * from employee;mysql> select * from employee where id =11;
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中文網其他相關文章!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。