开发者

Need to optimize a nested select statement

I've got the following SQL:

SELECT customfieldvalue.ISSUE
FROM   customfieldvalue
WHERE  customfieldvalue.STRINGVALUE 
IN (SELECT customfieldvalue.STRINGVALUE
    FROM   customfieldvalue
    WHERE  customfieldvalue.CUSTOMFIELD = "10670"
    GROUP  BY customfieldvalue.STRINGVALUE
    HAVING COUNT(*) > 1);

The inner nested select returns 3265 rows in 1.5secs on MySQL 5.0.77 when run on its own. The customfieldvalue table contains 2286831 rows.

I want to return all values of the ISSUE co开发者_如何学Pythonlumn where the STRINGISSUE column value is not exclusive to that row and the CUSTOMFIELD column contains "10670".

When I try and run the query above, MySQL seems to be stuck. I've left it run for up to a minute, but I'm pretty sure the problem is my query.


Try something along these lines:

    SELECT cfv1.ISSUE
    COUNT(cfv2.STRINGVALUE) as indicator

    FROM   customfieldvalue cfv1

    INNER JOIN customfieldvalue cfv2
    ON cfv1.STRINGVALUE = cfv2.STRINGVALUE AND cfv2.CUSTOMFIELD = "10670"

    GROUP BY cfv1.ISSUE
    HAVING indicator > 1

This probably doesn't work on copy&paste as I haven't verified it, but in MySQL JOINs are often much much faster than subqueries, even orders of magnitude.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜