【发布时间】:2018-03-24 19:19:40
【问题描述】:
我正在使用以下查询:
SELECT *,
GROUP_CONCAT(distinct t_name order by t_id separator ', ') 'Topics',
GROUP_CONCAT(distinct ch_title order by ch_id separator ', ') 'Chapters',
GROUP_CONCAT(distinct cs_code order by cs_id separator ', ') 'ContentStandards',
GROUP_CONCAT(distinct c_name order by c_id separator ', ') 'Categories',
GROUP_CONCAT(distinct s_name order by s_id separator ', ') 'SchoolSubjects'
FROM (
SELECT r.res_id, r.res_status, r.res_type, r.res_category, r.res_title, r.res_filename, r.res_source, r.res_gradelevel_from, r.res_gradelevel_to, r.res_tags, r.res_description, r.res_onserver, r.res_favoriteaccts, r.res_CreatedOn, c_id, s_id, t_id, ch_id, cs_id,
CONCAT("<a class=""ReportLink"" href=""page.asp?category=",c_name,""" title=""",c_name,""">",c_name,"</a>") as c_name,
CONCAT("<a class=""ReportLink"" href=""page.asp?subject=",s_name,""" title=""",s_name,""">",s_name,"</a>") as s_name,
CONCAT("<a class=""ReportLink"" href=""page.asp?topic=",t_name,""" title=""",t_name,""">",t_name,"</a>") as t_name,
CONCAT("<a class=""ReportLink"" href=""page.asp?cs=",cs_code,""" title=""",cs_code,""">",cs_code,"</a>") as cs_code,
CONCAT("<a class=""ReportLink"" href=""page.asp?chapter=",ch_title,""" title=""",ch_title,""">",ch_code,": ",ch_title,"</a>") as ch_title
FROM hre_resources r
LEFT JOIN topic_to_resource ttr ON ttr.rt_resourceid = r.res_id
LEFT JOIN topics t on t.t_id = ttr.rt_topicid
LEFT JOIN topic_to_cs tcs on tcs.cst_topicid = t.t_id
LEFT JOIN categories c ON c_id = r.res_category
LEFT JOIN topic_to_framework ttc on ttc.ft_topicid = t.t_id
LEFT JOIN ca_hss_frameworkchapters ch ON ch.ch_id = ttc.ft_frameworkid
LEFT JOIN ca_hss_contentstandards cs ON cs.cs_id = tcs.cst_contentstandardid
LEFT JOIN schoolsubjects_to_resource str ON str.sr_resid = r.res_id
LEFT JOIN schoolsubjects s ON s.s_id = str.sr_sid) links
WHERE RES_Status = 'Active'
GROUP BY RES_ID
ORDER BY RES_Title ASC, RES_Source DESC
此查询从我们的 mySQL DB 中返回 64 条格式完美的记录,但执行此操作需要 100 多秒。
查询分析器将主要违规者显示为:
+-------------------------------+-----------+
| Sending data | 34.530026 |
+-------------------------------+-----------+
| Creating sort index | 24.205878 |
| Storing result in query cache | 56.739204 |
+-------------------------------+-----------+
EXPLAIN 结果如下:
+----+-------------+------------+--------+---------------+---------------+---------+------------------------------------+------+---------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------------+---------+------------------------------------+------+---------------------------------------------------------------------+
| 1 | PRIMARY | <derived2> | ref | <auto_key0> | <auto_key0> | 77 | const | 10 | Using index condition; Using where; Using temporary; Using filesort |
| 2 | DERIVED | r | ALL | NULL | NULL | NULL | NULL | 64 | NULL |
| 2 | DERIVED | ttr | ref | RT_ResourceID | RT_ResourceID | 4 | uhri_hre.r.RES_ID | 3 | Using where |
| 2 | DERIVED | t | eq_ref | PRIMARY | PRIMARY | 4 | uhri_hre.ttr.RT_TopicID | 1 | Using where |
| 2 | DERIVED | tcs | ref | CST_TopicID | CST_TopicID | 4 | uhri_hre.t.T_ID | 9 | Using where |
| 2 | DERIVED | c | eq_ref | PRIMARY | PRIMARY | 4 | uhri_hre.r.RES_Category | 1 | Using where |
| 2 | DERIVED | ttc | ref | FT_TopicID | FT_TopicID | 4 | uhri_hre.t.T_ID | 21 | Using where |
| 2 | DERIVED | ch | eq_ref | PRIMARY | PRIMARY | 4 | uhri_hre.ttc.FT_FrameworkID | 1 | Using where |
| 2 | DERIVED | cs | eq_ref | PRIMARY | PRIMARY | 4 | uhri_hre.tcs.CST_ContentStandardID | 1 | Using where |
| 2 | DERIVED | str | ref | SR_RESID | SR_RESID | 4 | uhri_hre.r.RES_ID | 1 | Using where |
| 2 | DERIVED | s | eq_ref | PRIMARY | PRIMARY | 4 | uhri_hre.str.SR_SID | 1 | Using where |
+----+-------------+------------+--------+---------------+---------------+---------+------------------------------------+------+---------------------------------------------------------------------+
这些表具有以下索引。未列出的表在 ID 字段(主键)上有索引:
HRE_Resources:
+--------------+-------+--------+--------+--------------+-------------+-----------+------+---------+
| Keyname | Type | Unique | Packed | Column | Cardinality | Collation | Null | Comment |
+--------------+-------+--------+--------+--------------+-------------+-----------+------+---------+
| PRIMARY | BTREE | Yes | No | RES_ID | 64 | A | | |
| RES_Type | BTREE | No | No | RES_Type | 4 | A | | |
| RES_Category | BTREE | No | No | RES_Category | 10 | A | | |
| RES_Status | BTREE | No | No | RES_Status | 4 | A | | |
| RES_Language | BTREE | No | No | RES_Language | 8 | A | | |
| RES_Source | BTREE | No | No | RES_Source | 21 | A | | |
+--------------+-------+--------+--------+--------------+-------------+-----------+------+---------+
Topics:
+---------+-------+--------+--------+--------+-------------+-----------+------+---------+
| Keyname | Type | Unique | Packed | Column | Cardinality | Collation | Null | Comment |
+---------+-------+--------+--------+--------+-------------+-----------+------+---------+
| PRIMARY | BTREE | Yes | No | T_ID | 39 | A | | |
| T_Name | BTREE | No | No | T_Name | 39 | A | | |
+---------+-------+--------+--------+--------+-------------+-----------+------+---------+
Topic_to_CS:
+-----------------------+-------+--------+--------+-----------------------+-------------+-----------+------+---------+
| Keyname | Type | Unique | Packed | Column | Cardinality | Collation | Null | Comment |
+-----------------------+-------+--------+--------+-----------------------+-------------+-----------+------+---------+
| PRIMARY | BTREE | Yes | No | CST_ID | 685 | A | | |
| CST_ContentStandardID | BTREE | No | No | CST_ContentStandardID | 228 | A | | |
| CST_TopicID | BTREE | No | No | CST_TopicID | 76 | A | | |
+-----------------------+-------+--------+--------+-----------------------+-------------+-----------+------+---------+
Topic_to_Framework:
+----------------+-------+--------+--------+----------------+-------------+-----------+------+---------+
| Keyname | Type | Unique | Packed | Column | Cardinality | Collation | Null | Comment |
+----------------+-------+--------+--------+----------------+-------------+-----------+------+---------+
| PRIMARY | BTREE | Yes | No | FT_ID | 471 | A | | |
| FT_TopicID | BTREE | No | No | FT_TopicID | 22 | A | | |
| FT_FrameworkID | BTREE | No | No | FT_FrameworkID | 235 | A | | |
+----------------+-------+--------+--------+----------------+-------------+-----------+------+---------+
Topic_to_Resource:
+---------------+-------+--------+--------+---------------+-------------+-----------+------+---------+
| Keyname | Type | Unique | Packed | Column | Cardinality | Collation | Null | Comment |
+---------------+-------+--------+--------+---------------+-------------+-----------+------+---------+
| PRIMARY | BTREE | Yes | No | RT_ID | 387 | A | | |
| RT_ResourceID | BTREE | No | No | RT_ResourceID | 129 | A | | |
| RT_TopicID | BTREE | No | No | RT_TopicID | 77 | A | | |
+---------------+-------+--------+--------+---------------+-------------+-----------+------+---------+
Schoolsubject_to_Resource:
+----------+-------+--------+--------+----------+-------------+-----------+------+---------+
| Keyname | Type | Unique | Packed | Column | Cardinality | Collation | Null | Comment |
+----------+-------+--------+--------+----------+-------------+-----------+------+---------+
| PRIMARY | BTREE | Yes | No | SR_ID | 228 | A | | |
| SR_SID | BTREE | No | No | SR_SID | 25 | A | | |
| SR_RESID | BTREE | No | No | SR_RESID | 228 | A | | |
+----------+-------+--------+--------+----------+-------------+-----------+------+---------+
Categories:
+---------+-------+--------+--------+--------+-------------+-----------+------+---------+
| Keyname | Type | Unique | Packed | Column | Cardinality | Collation | Null | Comment |
+---------+-------+--------+--------+--------+-------------+-----------+------+---------+
| PRIMARY | BTREE | Yes | No | C_ID | 9 | A | | |
| C_Name | BTREE | No | No | C_Name | 9 | A | | |
+---------+-------+--------+--------+--------+-------------+-----------+------+---------+
其中一些索引是使用其他查询中使用的更复杂的 WHERE 创建的。
我已经在数据库上运行了与上述查询无关的其他目的的复杂查询,并且名义上执行。因此,我认为这不是服务器资源问题。
任何人都可以在上面的查询中看到任何可能导致响应如此缓慢的内容吗?
非常感谢!
PS。根据以下请求从服务器添加一些信息...
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
+------------------------------+---------+
+-------------------------+--------+
| Variable_name | Value |
+-------------------------+--------+
| Qcache_free_blocks | 9 |
| Qcache_free_memory | 22736 |
| Qcache_hits | 121910 |
| Qcache_inserts | 9296 |
| Qcache_lowmem_prunes | 8213 |
| Qcache_not_cached | 29475 |
| Qcache_queries_in_cache | 103 |
| Qcache_total_blocks | 295 |
+-------------------------+--------+
【问题讨论】:
-
你检查过子查询的结果数吗?我的猜测是你有几个不相关的一对多关系,它们相互乘以各自的结果......而你的 GROUP_CONCAT 中的 DISTINCT 隐藏了这一点。
-
...如果是这种情况,通常的做法是将这些连接分解为最小的子查询,然后将它们连接在一起以获得相同的最终结果格式。
-
我同意。不要试图在一个查询中完成所有操作。这会产生巨大的笛卡尔积,然后你必须使用 DISTINCT 减少它。相反,只需运行几个查询,这些查询单独起来更简单。
-
另外,我建议您不要在 SQL 查询中格式化 HTML。只需取回数据,并使用应用程序代码以 HTML 形式呈现数据值。
-
我还注意到您的查询花费了 50% 的时间来更新查询缓存。我建议您禁用查询缓存。由于这种事情,MySQL 8.0 中删除了查询缓存功能。
标签: mysql performance join indexing group-concat