数据库
首页 > 数据库> > mysql简单聚合子查询结果很慢

mysql简单聚合子查询结果很慢

作者:互联网

我正在尝试根据购买的商品数量“捆绑”所有客户,并显示每个箱柜的数量.我试图看看有多少人(account_id)购买了一件商品,购买了两件商品,一直购买了九件商品,然后购买了十件商品.

这是我正在使用的查询 – 为了它的价值,我希望查询对销售进行全表扫描以生成结果,但整个过程需要永远!

我来自Oracle背景,我像在Oracle中一样编写查询.

 select  thecnt
      ,  count(*) 
   from  (select  count(*)
               ,  case when count(*) >= 10 then 'tenormore' else cast(count(*) as char) end thecnt
            from  sales
           where  created >= SUBDATE( CURRENT_DATE(), INTERVAL 60 DAY )
        group by  account_id) sub
group by  thecnt
order by  thecnt;

在处理子查询时,mysql中有任何陷阱吗?

解释计划

+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+----------+-----------------------------------------------------------+
| id | select_type | table             | type  | possible_keys | key     | key_len | ref  |     rows    | filtered | Extra                                                     |
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+----------+-----------------------------------------------------------+
|  1 | PRIMARY     | <derived2>        | ALL   | NULL          | NULL    | NULL    | NULL | 2143248 |   100.00 | Using temporary; Using filesort                           |
|  2 | DERIVED     | sales             | range | created       | created | 8       | NULL | 2012492 |   100.00 | Using where; Using index; Using temporary; Using filesort |
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+----------+-----------------------------------------------------------+
2 rows in set, 1 warning (1 hour 4 min 6.14 sec)


mysql> describe sales;
+-----------------+---------------------+------+-----+---------+-------+
| Field           | Type                | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| account_id      | char(36)            | NO   | PRI | NULL    |       |
| created         | datetime            | NO   | MUL | NULL    |       |
| histogram_value | bigint(20) unsigned | NO   | PRI | NULL    |       |
+-----------------+---------------------+------+-----+---------+-------+

解决方法:

我没有看到您的查询有任何特别的错误.查询速度慢的原因是因为它需要使用临时表和filesort.严重加速此查询的唯一方法是修改MySQL设置以分配更多内存,以避免将磁盘用于这些进程. Here’s a spot on article covering the pertinent settings.

编辑:执行此操作后,您还可以通过指定要计数的精确列而不是COUNT(*)以及其他一些小调整来节省内存,正如其他一些人所提到的那样.您希望获得尽可能小的数据集以充分利用您的记忆.但我认为除非你分配更多内存,否则整个问题不会消失.

标签:mysql,sql,database-performance
来源: https://codeday.me/bug/20190705/1383724.html