MySQL 优化 之 Copying to tmp table on disk
项目中遇到了慢查询问题
Sql语句
SELECT
sum(price) AS price,
`member_id`
FROM
`crm_upload`
GROUP BY
member_id
ORDER BY
price DESC
LIMIT 10;
Explain 之后的结果:
MariaDB [member]> explain SELECT sum(price) as price,`member_id` FROM `crm_upload` WHERE `status` = 1 AND `approved` = 1 AND `consume_time` > '2015-09-10' GROUP BY member_id ORDER BY price desc LIMIT 10;
+------+-------------+------------+------+---------------+------+---------+------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+------------+------+---------------+------+---------+------+--------+----------------------------------------------+
| 1 | SIMPLE | crm_upload | ALL | NULL | NULL | NULL | NULL | 310461 | Using where; Using temporary; Using filesort |
+------+-------------+------------+------+---------------+------+---------+------+--------+----------------------------------------------+
1 row in set (0.00 sec)
关于 Using temporary; 手册解释
To resolve the query, MySQL needs to create a temporary table to hold the result. This typically happens if the query contains GROUP BY and ORDER BY clauses that list columns differently.
大意是,需要一个临时表来暂存查询后的结果,经常会出现在Group By 或者 Order By 中
关于 Using filesort;手册解释
MySQL must do an extra pass to find out how to retrieve the rows in sorted order. The sort is done by going through all rows
according to the join type and storing the sort key and pointer to the row for all rows that match the WHEREclause. The keys then are sorted
and the rows are retrieved in sorted order.
大意是 Mysql 如果想要正常的查找出排序中的数据,需要做一个额外的传递。这个排序将根据join的类型遍历所有的数据,并且存储排序的key。找出匹配到的where条件的数据。
show profile
MariaDB [member]> show profile;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000037 |
| Waiting for query cache lock | 0.000008 |
| init | 0.000006 |
| checking query cache for query | 0.000103 |
| checking permissions | 0.000011 |
| Opening tables | 0.000029 |
| After opening tables | 0.000010 |
| System lock | 0.000008 |
| Table lock | 0.000007 |
| After opening tables | 0.000012 |
| Waiting for query cache lock | 0.000006 |
| After opening tables | 0.000041 |
| init | 0.000044 |
| optimizing | 0.000016 |
| statistics | 0.000244 |
| preparing | 0.000116 |
| executing | 0.000015 |
| Copying to tmp table | 0.000061 |
| Copying to tmp table | 0.138350 |
| converting HEAP to Aria | 0.003233 |
| Creating index | 0.000025 |
| Repair by sorting | 0.020695 |
| Saving state | 0.000040 |
| Creating index | 0.000005 |
| converting HEAP to Aria | 0.000070 |
| Copying to tmp table on disk | 4.040516 |
| Sorting result | 0.020373 |
| Sending data | 0.000046 |
| end | 0.000003 |
| removing tmp table | 0.000838 |
| end | 0.000013 |
| query end | 0.000008 |
| closing tables | 0.000010 |
| freeing items | 0.000006 |
| updating status | 0.000003 |
| Waiting for query cache lock | 0.000002 |
| updating status | 0.000715 |
| Waiting for query cache lock | 0.000015 |
| updating status | 0.000002 |
| storing result in query cache | 0.000014 |
| logging slow query | 0.000053 |
| cleaning up | 0.000017 |
+--------------------------------+----------+
可以看到
Copying to tmp table on disk 花费了大量的时间。
结果查找资料后 了解到 发现mysql可以通过变量tmp_table_size和max_heap_table_size来控制内存表大小上限,如果超过上限会将数据写到磁盘上,从而会有物理磁盘的读写操作,导致影响性能。
调整参数配置之后 就不会有这个问题了。
- To set max_heap_table_size to 64M do the following:
SET max_heap_table_size = 1024 * 1024 * 64;
- To set tmp_table_size to 32M do the following:
SET tmp_table_size = 1024 * 1024 * 32;
MySQL 优化 之 Copying to tmp table on disk的更多相关文章
- [MySQL优化2]不用SELECT * FROM table;
假设有一张employees表,它有8列:员工人数,姓氏,名字,分机,电子邮件,办公室代码,报告,职位等.如果要仅查看员工的名字,姓氏和职位,请使用以下查询:SELECT lastname, firs ...
- MySQL优化技巧之五(mysql查询性能优化)
对于高性能数据库操作,只靠设计最优的库表结构.建立最好的索引是不够的,还需要合理的设计查询.如果查询写得很糟糕,即使库表结构再合理.索引再合适,也无法实现高性能.查询优化.索引优化.库表结构优化需要齐 ...
- mysql优化一之查询优化
这一篇笔记的mysql优化是注重于查询优化,根据mysql的执行情况,判断mysql什么时候需要优化,关于数据库开始阶段的数据库逻辑.物理结构的设计结构优化不是本文重点,下次再谈 查看mysql语句的 ...
- [转]mysql优化——show processlist命令详解
本文转自:https://blog.csdn.net/sunqingzhong44/article/details/70570728 版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...
- mysql优化——show processlist命令详解
SHOW PROCESSLIST显示哪些线程正在运行 不在mysql提示符下使用时用mysql -uroot -e 'Show processlist' 或者 mysqladmin pro ...
- Mysql优化_内置profiling性能分析工具
如果要进行SQL的调优优化和排查,第一步是先让故障重现,但是这个并不是这一分钟有问题,下一秒就OK.一般的企业一般是DBA数据库工程师从监控里找到问题.DBA会告诉我们让我们来排查问题,那么可能很多种 ...
- Mysql优化实践(分页优化)
当你和别人都能实现一个某个功能,这时候区分你们能力的不是谁干活多少,而是谁能写出效率更高的代码.比如显示一个订单列表它不仅仅是写一条SELECT SQL那么简单,我们还需要很清楚的知道这条SQL他大概 ...
- 【mysql优化】大数据量分页优化
limit 翻页原理 limit offset,N, 当offset非常大时, 效率极低, 原因是mysql并不是跳过offset行,然后单取N行, 而是取offset+N行,返回放弃前offset行 ...
- Mysql优化系列之查询性能优化前篇2
接前一篇,这一篇主要总结下几个经常要用的命令 命令一:explain+sql mysql> explain select * from servers; +----+-------------+ ...
随机推荐
- layui前端框架实例(修复官网数据接口异常问题)
layui前端框架实例,官网的实例会提示数据接口异常,已修复. 主要是修改数据表格,做一个可以用的实例,可以选中,编辑,删除等. gitee地址:https://gitee.com/pingg2019 ...
- Luogu P1850换教室【期望dp】By cellur925
题目传送门 首先这个题我们一看它就是和概率期望有关,而大多数时候在OI中遇到他们时,都是与dp相关的. \(Vergil\)学长表示,作为\(NOIp2016\)的当事人,他们考前奶联赛一定不会考概率 ...
- pycharm 断点调试
转自; https://blog.csdn.net/chenggong2dm/article/details/9368641 PyCharm 作为IDE,断点调试是必须有的功能.否则,我们还真不如用纯 ...
- ARC 100
链接 https://arc100.contest.atcoder.jp/ C Linear Approximation 题解 把ai减去i后排序, 我们要的b就是排完序后的中位数 Code #inc ...
- 洛谷 P1892 团伙
P1892 团伙 并查集 #include<cstdio> int fa[2500];//fa[i]表示i的朋友所在集合,fa[i+n]表示i的敌人所在集合 bool boo[2500]; ...
- Secrets CodeForces - 333A
Secrets CodeForces - 333A 题意:这个世界上只有这样面值的硬币:1,3,9,27,81,...有一个商人,某一天遇到了一个顾客,他购买了价值n的商品,发现用自己的硬币无法付给商 ...
- spring mvc 通过拦截器记录请求数据和响应数据
spring mvc 能过拦截器记录请求数据记录有很多种方式,主要有以下三种: 1:过滤器 2:HandlerInterceptor拦截器 3:Aspect接口控制器 但是就我个人所知要记录返回的数据 ...
- datetime 模块详解
1.import datetime 常用方法: ttimedelta() 括号里默认为days,进行别的单位运算可以加上如hours = 1这样.除了进行减法运算,还可以进行加法运算. >> ...
- jQuery选择器之属性筛选选择器
在这么多属性选择器中[attr="value"]和[attr*="value"]是最实用的 [attr="value"]能帮我们定位不同类型 ...
- js dom node.children与node.childNodes区别
不同点:node.children不会取到节点下面的TextNode但是node.childNodes会取到 共同点:两者都是集合类数组,可以通过索引的方式取到值也可以用for循环遍历