项目中遇到了慢查询问题

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. 9-26模拟赛 By cellur925

    1.计数 (count.cpp/c/pas)时间限制:1s内存限制:256MB[问题描述]给出 m 个数 a[1],a[2],…,a[m]求 1~n 中有多少数不是 a[1],a[2],…,a[m]的 ...

  2. 调试的时候禁止chrome缓存图片

    https://blog.csdn.net/yiifaa/article/details/54290047 https://blog.csdn.net/xinghuo0007/article/deta ...

  3. pip 国内安装镜像源

    转自: https://blog.csdn.net/xuezhangjun0121/article/details/81664260 pip国内的一些镜像   阿里云 http://mirrors.a ...

  4. Vant入门

    Vant是什么 简介 Vant是有赞开源的一套基于Vue2.0的Mobile组件库.通过Vant,可以快速搭建出风格统一的页面,提升开发效率.目前已有近50个组件,这些组件被广泛使用于有赞的各个移动端 ...

  5. Mybatis-Configuration-详解

    Configuration MyBatis的初始化会执行SqlSessionFactoryBuilder的中build()方法,build方法又会调用XMLConfigBuilder()的内部pars ...

  6. (027)[技术资料]业余制作Windows图标

    这几天一直在纠结一件事,想给软件制作一个简单的图标,以前(2014-10-4 11:00)制作的是下面这个,多重ICO,最大尺寸256,无压缩(windows允许图标尺寸在大于256时按PNG方式进行 ...

  7. C# 部分命名规则

    接触C#开发已经四个月,整理下C#中的命名规则: 一:变量的命名规则(和Java相似) 1.变量名由字母.数字.下划线组成 2.变量名开头只能以字母.下划线开头,不能以数字开头 3.区分大小写 4.命 ...

  8. [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列

    \(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...

  9. poj1862 Stripies

    思路: 简单贪心. 实现: #include <iostream> #include <cstdio> #include <algorithm> #include ...

  10. php中session实现机制

    一.默认机制,用磁盘文件来实现PHP会话.php.ini配置:session.save_handler = files 1.session_start() A. session_start()是ses ...