mysql 查询慢sql 例子_sql慢查询及优化方法

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值