[英]Performance issue with mysql query
I am working in MYSQL for the first and I am having issues with the following query我第一次在 MYSQL 工作,但遇到以下查询问题
SELECT
t.id,
t.name,
t.description,
(
SELECT
GROUP_CONCAT( CONCAT( hs.name, '|', s.rate ) )
FROM
occupation_skill_rate s
INNER JOIN hard_skills hs ON s.hard_skill_id = hs.id
WHERE
s.occupation_id = t.id
ORDER BY
s.rate DESC LIMIT 15
) AS skills,
(
SELECT
GROUP_CONCAT( CONCAT( hs.name, '|', s.rate ) )
FROM
occupation_knowledge_rate s
INNER JOIN knowledge hs ON s.knowledge_id = hs.id
WHERE
s.occupation_id = t.id
ORDER BY
s.rate DESC LIMIT 15
) AS knowledge,
(
SELECT
GROUP_CONCAT( CONCAT( hs.name, '|', s.rate ) )
FROM
occupation_abilities_rate s
INNER JOIN ability hs ON s.ability_id = hs.id
WHERE
s.occupation_id = t.id
ORDER BY
s.rate DESC LIMIT 15
) AS knowledge
FROM
occupations t
The occupation table contains 1033 rows occupation_skill_rate contains 34160 rows and it takes more than 1 minute to execute this query. occupation 表包含 1033 行 occupation_skill_rate 包含 34160 行,执行此查询需要 1 分钟多的时间。 Please let me know if you need further clarification for helping me.
如果您需要进一步说明以帮助我,请告诉我。
Thanks for your help Ajai谢谢你的帮助阿杰
The occupation_%_rate
tables seem to be many-to-many, correct? occupation_%_rate
表似乎是多对多的,对吗? They need these indexes and no id:他们需要这些索引但没有 id:
PRIMARY KEY(occupation_id, xxx_id)
INDEX(xxx_id, occupation_id)
But, it seems like the ORDER BY
and LIMIT
when used with GROUP_CONCAT()
.但是,当与
GROUP_CONCAT()
一起使用时,它看起来像是ORDER BY
和LIMIT
。 Please describe what the query's intention is;请描述查询的意图是什么; we may be able to help in rewriting it.
我们也许可以帮助重写它。
GROUP_CONCAT
allows an ORDER BY
clause but not a LIMIT
. GROUP_CONCAT
允许ORDER BY
子句但不允许LIMIT
。 Can you do without the LIMITs
?你可以不用
LIMITs
吗?
Example例子
Instead of代替
( SELECT GROUP_CONCAT( CONCAT( hs.name, '|', s.rate ) )
FROM occupation_skill_rate s
INNER JOIN hard_skills hs ON s.hard_skill_id = hs.id
WHERE s.occupation_id = t.id
ORDER BY s.rate DESC
LIMIT 15
) AS skills;
Do做
( SELECT CONCAT( name, '|', rate ORDER BY rate DESC )
FROM (
SELECT hs.name, s.rate
FROM occupation_skill_rate s
INNER JOIN hard_skills hs
ON s.hard_skill_id = hs.id
AND s.occupation_id = t.id
ORDER BY s.rate DESC
LIMIT 15
) AS a
) AS skills
But I suspect t
is not visible that deeply nested.但我怀疑
t
嵌套得很深是不可见的。
If that is the case, rearrange things thus:如果是这种情况,请重新安排事情:
SELECT t.id, t.name, t.description, s3.skills, ...
FROM occupations AS t
JOIN (
SELECT s2.occupation_id,
CONCAT( s2.name, '|', s2.rate ORDER BY rate DESC )
AS skills
FROM (
SELECT hs.name, s1.rate, s1.occupation_id
FROM occupation_skill_rate s1
INNER JOIN hard_skills hs
ON s1.hard_skill_id = hs.id
ORDER BY s.rate DESC
LIMIT 15
) AS s2
GROUP BY s2.occupation_id
ORDER BY s2.rate DESC
) AS s3 ON s2.occupation_id = t.id
JOIN ...
JOIN ... ;
Another其他
There is also a way to build the long GROUP_CONCAT
, then chop to 15 items by using SUBSTRING_INDEX(...)
.还有一种构建长
GROUP_CONCAT
的方法,然后使用SUBSTRING_INDEX(...)
将其切成 15 个项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.