项目中遇到了慢查询问题

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的更多相关文章

  1. [MySQL优化2]不用SELECT * FROM table;

    假设有一张employees表,它有8列:员工人数,姓氏,名字,分机,电子邮件,办公室代码,报告,职位等.如果要仅查看员工的名字,姓氏和职位,请使用以下查询:SELECT lastname, firs ...

  2. MySQL优化技巧之五(mysql查询性能优化)

    对于高性能数据库操作,只靠设计最优的库表结构.建立最好的索引是不够的,还需要合理的设计查询.如果查询写得很糟糕,即使库表结构再合理.索引再合适,也无法实现高性能.查询优化.索引优化.库表结构优化需要齐 ...

  3. mysql优化一之查询优化

    这一篇笔记的mysql优化是注重于查询优化,根据mysql的执行情况,判断mysql什么时候需要优化,关于数据库开始阶段的数据库逻辑.物理结构的设计结构优化不是本文重点,下次再谈 查看mysql语句的 ...

  4. [转]mysql优化——show processlist命令详解

    本文转自:https://blog.csdn.net/sunqingzhong44/article/details/70570728 版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...

  5. mysql优化——show processlist命令详解

    SHOW PROCESSLIST显示哪些线程正在运行 不在mysql提示符下使用时用mysql -uroot  -e 'Show  processlist'   或者   mysqladmin pro ...

  6. Mysql优化_内置profiling性能分析工具

    如果要进行SQL的调优优化和排查,第一步是先让故障重现,但是这个并不是这一分钟有问题,下一秒就OK.一般的企业一般是DBA数据库工程师从监控里找到问题.DBA会告诉我们让我们来排查问题,那么可能很多种 ...

  7. Mysql优化实践(分页优化)

    当你和别人都能实现一个某个功能,这时候区分你们能力的不是谁干活多少,而是谁能写出效率更高的代码.比如显示一个订单列表它不仅仅是写一条SELECT SQL那么简单,我们还需要很清楚的知道这条SQL他大概 ...

  8. 【mysql优化】大数据量分页优化

    limit 翻页原理 limit offset,N, 当offset非常大时, 效率极低, 原因是mysql并不是跳过offset行,然后单取N行, 而是取offset+N行,返回放弃前offset行 ...

  9. Mysql优化系列之查询性能优化前篇2

    接前一篇,这一篇主要总结下几个经常要用的命令 命令一:explain+sql mysql> explain select * from servers; +----+-------------+ ...

随机推荐

  1. 工作中常用css样式总结

    一.HTML隐藏文本输入框 有三种方法: 1.<input type="hidden" value=""> 这是对任何元素都起作用的: 2.< ...

  2. pycharm 整段缩进

    转自 https://blog.csdn.net/zoulonglong/article/details/79869787 在使用pycharm时,经常会需要多行代码同时缩进.左移,pycharm提供 ...

  3. visual studio各版本下载

    软件包括以下几种: cn_visual_studio_2010_ultimate_x86_dvd_532347.part1.rar cn_visual_studio_2010_ultimate_x86 ...

  4. 多线程 线程间通信 wait,notify

    1. 方法wait锁释放,notify()锁不释放

  5. SpringMVC中Freemarker获取项目根目录

    https://blog.csdn.net/whatlookingfor/article/details/51538995 在SpringMVC框架中使用Freemarker试图时,要获取根路径的方式 ...

  6. 逆序数 HDOJ 4911 Inversion

    题目传送门 题意:可以交换两个相邻的数字顺序k次,问最后逆序对最少有多少 分析:根据逆序数的定理如果逆序数大于0,那么必定存在1<=i<n使得i和i+1交换后逆序数减1假设原逆序数为cnt ...

  7. PHP常见的输出语句 echo()、print()、print_r()、var_dump()、pinrtf()之间的区别

    echo().print().print_r().var_dump().pinrtf()之间的区别 1.print() :简单输出语句,可以输出字符串,变量.表达式等单项的值!(不能输出数组.对象等复 ...

  8. D. Tavas and Malekas DFS模拟 + kmp + hash || kmp + hash

    http://codeforces.com/contest/535/problem/D 如果真的要把m个串覆盖上一个串上面,是可以得,不会超时. 要注意到一点,全部覆盖后再判断时候合法,和边放边判断, ...

  9. 使用css3 制作switch开关

    使用css3来实现switch开关的效果: html代码: <!--switch开关--><div class="switch-btn"> <inpu ...

  10. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...