MYSQL query takes too long on server, on localhost however it works fine

时间:2017-04-24 17:29:48

标签: mysql sql

I have this query which helped me save from PHP pain, but if the results are increased, now I get to see, this same query halts my application. Which is worse

SELECT *,
       (SELECT COUNT(*)
          FROM table1
               INNER JOIN table2 ON table1.somecolumn = table2.somecolumn
         WHERE     (   table1.statuscolumn = 'status1'
                    OR table1.statuscolumn = 'status2')
               AND table2.othercolumn2 = 'somevalue'
               AND table1.othercolumn
               AND table1.othercolumn LIKE
                      CONCAT('%', table3.othercolumn3, '%'))
          AS utilization
  FROM table3, table1
 WHERE     table3.othercolumn2 = 'somevalue'
       AND (   table1.statuscolumn = 'status1'
            OR table1.statuscolumn = 'status2')
       AND table3.othercolumn4 = 'on'
GROUP BY table3.othercolumn3

How can I achieve same results within less execution time. Thank you.

1 个答案:

答案 0 :(得分:0)

虽然很难在没有质量问题的情况下给出准确答案,但我会尝试一下:

ALTER TABLE table1
    ADD INDEX (somecolumn);

ALTER TABLE table2
    ADD INDEX (somecolumn);

ALTER TABLE table1
    ADD INDEX (statuscolumn);

ALTER TABLE table2
    ADD INDEX (othercolumn2);

ALTER TABLE table3
    ADD INDEX (othercolumn2);

ALTER TABLE table3
    ADD INDEX (othercolumn4);