如何提高这个Mysql Query的性能?

时间:2017-05-20 07:26:56

标签: mysql database

我根据大表格的要求编写了以下查询。 如果我给出限制10或1000,则两者的加载时间相同。 请帮助提高此查询性能。

SELECT
    col1,
    col2,
    (SELECT MAX(tcol1) FROM table2 WHERE REPLACE(tcol1,' s',' ')=col1 AND tcol2 = col2) AS max,
    (SELECT COUNT(1) FROM table2 WHERE REPLACE(tcol1,' s',' ')=col1 AND tcol2 = col2) AS count
FROM 
    table1
ORDER BY count DESC LIMIT 10

提前致谢

1 个答案:

答案 0 :(得分:0)

SELECT
    t1.col1,
    t1.col2,
    A.MAX,
    A.Count
FROM 
    table1 t1
INNER JOIN (
        SELECT MAX(tcol1) AS MAX, 
           COUNT(1) AS COUNT 
        FROM table2 
        GROUP BY tcol1, tcol2
    ) A 
    ON t1.col1=A.col1 
    AND t1.col2 = A.col2
ORDER BY COUNT DESC 
LIMIT 10