mysql - 如何优化此 sql 查询(内连接)

标签 mysql optimization query-optimization

有没有办法优化这个查询。执行时间超过 40 秒。我尝试了很多方法,比如创建索引。这帮助不大。仍然查询时间太长。

 SELECT b.id,
   Count(DISTINCT a.id, c.chapters_id) AS pct,
   Count(DISTINCT e.id)                 AS eCount,
   Count(DISTINCT f.id)                 AS fCount,
   Count(DISTINCT d2.id)                 AS d2Count,
   b.NAME,
   e.NAME,
   e.address2,
   f.NAME,
   d2.is_active
FROM   tableA a
       INNER JOIN tableB b
               ON a.modules_id = b.id
       INNER JOIN tableC c
               ON a.modules_id = c.modules_id
       INNER JOIN tableD d1
               ON d1.id = b.store_users_id 
       INNER JOIN tableD d2
               ON d2.id = a.store_users_id
       INNER JOIN tableE e
               ON e.id = d2.stores_id
       INNER JOIN tableF f
               ON e.city = f.id
WHERE  b.type IN( 1, 2, 4 )
       AND b.organizations_id = 156
       AND b.is_enable = true
GROUP  BY b.NAME,b.id

解释

   +----+-------------+-------+--------+-----------------------------------------------------------------------------------------------------+-----------------------------+---------+--------------------------+------+---------------------------------------+
| id | select_type | table | type   | possible_keys                                                                                       | key                         | key_len | ref                      | rows | Extra                                 |
+----+-------------+-------+--------+-----------------------------------------------------------------------------------------------------+-----------------------------+---------+--------------------------+------+---------------------------------------+
|  1 | SIMPLE      | b     | range  | PRIMARY,fk_modules_organizations1,fk_modules_store_users1,sort_modules_name,wh_modules_type,new_idx | new_idx                     | 9       | NULL                     |  160 | Using index condition; Using filesort |
|  1 | SIMPLE      | c     | ref    | fk_module_chapters_modules1                                                                         | fk_module_chapters_modules1 | 4       |         b.id             |    1 | NULL                                  |
|  1 | SIMPLE      | d1    | eq_ref | PRIMARY                                                                                             | PRIMARY                     | 4       |         b.store_users_id |    1 | Using index                           |
|  1 | SIMPLE      | a     | ref    | store_users_id_UNIQUE,fk_user_modules_store_users1,fk_user_modules_modules1                         | fk_user_modules_modules1    | 4       |         b.id             |  149 | NULL                                  |
|  1 | SIMPLE      | d2    | eq_ref | PRIMARY,fk_store_users_stores1                                                                      | PRIMARY                     | 4       |         a.store_users_id |    1 | NULL                                  |
|  1 | SIMPLE      | e     | eq_ref | PRIMARY,Stores-Cities                                                                               | PRIMARY                     | 4       |        d2.stores_id      |    1 | Using where                           |
|  1 | SIMPLE      | f     | eq_ref | PRIMARY                                                                                             | PRIMARY                     | 4       |         e.city           |    1 | NULL                                  |
+----+-------------+-------+--------+-----------------------------------------------------------------------------------------------------+-----------------------------+---------+--------------------------+------+---------------------------------------+

最佳答案

这可能对一些人有帮助:

INDEX(organizations_id, is_enable, type) -- on b

既然你说COUNT(DISTINCT...) , 它闻起来像 JOINs正在爆炸行数,那么你就是DISTINCTifying他们回到他们应该在的地方。一种查看方式是执行 SELECT COUNT(*) FROM ... JOIN ...没有最后GROUP BY .如果这是一个比任何表格大小都大得多的数字,那么您就会遇到这个经典问题。

有时解决方案是更换一个 JOIN通过

SELECT  ...,
        ( SELECT COUNT(*) FROM x ... )  AS x_ct,  -- instead of `JOIN x`
        ... 

关于mysql - 如何优化此 sql 查询(内连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32975609/

相关文章:

php - 文本类型的输入不会在 mysql 表中更新

php - 如何从克隆的 laravel 项目生成迁移(表?)到本地数据库?

php - 根据用户完成的搜索计算搜索条件记录(MYSQL PHP)

c# - 适用于 Mac 的 Mysql 连接器 .net

mysql - 查看对表执行的约束 - SQL

Python SciPy : optimization issue fmin_cobyla : one constraint is not respected

python - 使用 Tensorflow/PyTorch 加速自定义函数的最小化

javascript - 如何运行相同的javascript函数

sql - 使用 GROUP BY/HAVING 重构子查询?

mysql - 如何计算对一个数据库表进行简单选择查询的成本?