sql优化
慢查询:
检查慢查询是否开启
MariaDB [(none)]> show variables like 'slow_query_log';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| slow_query_log | OFF |
+----------------+-------+
1 row in set (0.00 sec)
慢查询日志存放的位置:
MariaDB [(none)]> show variables like 'slow_query_log_file';
+---------------------+--------------------+
| Variable_name | Value |
+---------------------+--------------------+
| slow_query_log_file | localhost-slow.log |
+---------------------+--------------------+
1 row in set (0.00 sec)
是否记录未使用索引的查询:
MariaDB [(none)]> show variables like 'log_queries_not_using_indexes';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF |
+-------------------------------+-------+
1 row in set (0.00 sec)
慢查询时间设置:此处10秒
MariaDB [(none)]> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
慢查询设置:
可以用 set global 上面相应参数进行修改(不用登录mysql)
如:
[root@localhost ~]# set global slow_query_log='OFF';
方法二:配置文件设置
修改配置文件my.cnf,在[mysqld]下的下方加入
[mysqld]slow_query_log = ONslow_query_log_file = /usr/local/mysql/data/slow.loglong_query_time = 1
重启MySQL服务
service mysqld restart
慢查询工具
[root@localhost ~]# mysqldumpslow -h
Option h requires an argument
ERROR: bad option
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
Parse and summarize the MySQL slow query log. Options are
--verbose verbose
--debug debug
--help write this text to standard output
-v verbose
-d debug
-s ORDER what to sort by (al, at, ar, ae, c, l, r, e, t), 'at' is default
al: average lock time
ar: average rows sent
at: average query time
aa: average rows affected
c: count
l: lock time
r: rows sent
t: query time
-r reverse the sort order (largest last instead of first)
-t NUM just show the top n queries
-a don't abstract all numbers to N and strings to 'S'
-n NUM abstract numbers with at least n digits within names
-g PATTERN grep: only consider stmts that include this string
-h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
default is '*', i.e. match all
-i NAME name of server instance (if using mysql.server startup script)
-l don't subtract lock time from total time
对具体的sql进行优化
MariaDB [test]> explain select * from users;
+------+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 1 | |
+------+-------------+-------+------+---------------+------+---------+------+------+-------+
说明:https://www.cnblogs.com/yycc/p/7338894.html
针对max进行优化:
对max作用的字段加索引:这样就会全表查询,而是直接通过索引找到最大的那条,速度快很多;
count(*)与count(字段)的值可能不一致,count(*)包含null,count(字段)不包含null
子查询中distinct去重复值
order by 能用主键尽量用主键
索引的建立上,将离散度高的字段放前面,不太恰当的例子,如:
假如有一个详情到街道的中国城市表,如果city_id放前面则查询影响的行会找出city_id对应所有的街道再在里面找town_id的,这样涉及的行数多;反之相对少一些
index(town_id_city_id) 好过index(city_id_town_id);
配置优化:
https://tools.percona.com/wizard
分片:
全局唯一id问题
auto_increment_offset要与分片个数相同
或用redis创建全局id
- 相关文章
- find_in_set在集合中查找
- sql查询自定义排序field函数
- sql显示表结构及索引
- sql修改表
- 添加、删除索引
- sql分组
- sql子查询
- sql多表更新
- sql建表同时插入数据
- sql多表删除
- 热门文章
- win7中将文件拷贝到虚拟机linux下
- phpexcel设置行高及列宽,背景颜色,
- rabbitmq无法启动
- intellij idea不显示git push按钮
- php7中使用mongodb的aggregate进行
- laravel页面静态化的方法
- centos7.4 64位下swoole安装及配置
- navicate连接mycat报1184错误
- curl设置超时不起作用(CURLOPT_TIM
- devops-jenkins容器为pending状态
- 好评文章
- phpexcel设置行高及列宽,背景颜色,
- php7中使用mongodb的aggregate进行
- intellij idea打开文件所在文件夹
- windows下使用MongoDB Compass Com
- win7中将文件拷贝到虚拟机linux下
- laravel 中悲观锁 & 乐观锁的使用
- 单点登录sso原理及php实现方式及de
- navicate连接mycat报1184错误
- rabbitmq无法启动
- laravel整合dingo/api方法步骤:jwt
- 我的项目
- 【github】www.github.com/hurong241
- 【码云】gitee.com/hu_rong/projects
- 【docker hub】hub.docker.com/repositories/hurong241
- 【packagist】packagist.org/users/hurong241/packages
- 站点信息
- 建站时间:2011年
- 文章数:623篇
- 浏览数:1303109