Question

I have a basic query that looks like the following that's running slow, so I'm trying to figure out where there's missing indexes or other optimizations I can make:

drop table weights;
CREATE TABLE weights engine=myisam
SELECT 'K' AS STAT,
           z.WEIGHT,
           COUNT(*) AS SAMPLE,
           round(AVG(z.FINAL_2Y),15) AS 2Y,
           round(AVG(z.FINAL_3Y),15) AS 3Y,
           round(AVG(z.FINAL_4Y),15) AS 4Y,
           round(AVG(z.FINAL_5Y),15) AS 5Y,
           round(AVG(z.FINAL_6Y),15) AS 6Y
    FROM
      ( SELECT /* insert big query here */ ) z
    GROUP BY WEIGHT;

This query is running awfully slow so I'm trying to make one simple change with the following:

drop table sp_weights_holding_table
CREATE TABLE sp_weights_holding_table engine=myisam
EXPLAIN SELECT 'K' AS STAT,
               z.WEIGHT,
               COUNT(*) AS SAMPLE,
               round(AVG(z.FINAL_2Y),15) AS 2Y,
               round(AVG(z.FINAL_3Y),15) AS 3Y,
               round(AVG(z.FINAL_4Y),15) AS 4Y,
               round(AVG(z.FINAL_5Y),15) AS 5Y,
               round(AVG(z.FINAL_6Y),15) AS 6Y
        FROM
          ( SELECT /* insert big query here */ ) z
        GROUP BY WEIGHT;

As soon as I pop the EXPLAIN in, I get a syntax error. I'm using MySQL 5.6.13 on Amazon RDS. The error looks like the following:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EXPLAIN SELECT 'K' AS STAT,
       z.WEIGHT,
       COUNT(*) AS SAMPLE,
       r' at line 2
Was it helpful?

Solution

You must semicolon the end of the sql command.

Change to

drop table sp_weights_holding_table;
CREATE TABLE sp_weights_holding_table engine=myisam;
EXPLAIN SELECT 'K' AS STAT,
....

OTHER TIPS

Use the EXPLAIN with the SELECT query alone without the first two lines for dropping/creating the table:

EXPLAIN SELECT 'K' AS STAT,
           z.WEIGHT,
           COUNT(*) AS SAMPLE,
           round(AVG(z.FINAL_2Y),15) AS 2Y,
           round(AVG(z.FINAL_3Y),15) AS 3Y,
           round(AVG(z.FINAL_4Y),15) AS 4Y,
           round(AVG(z.FINAL_5Y),15) AS 5Y,
           round(AVG(z.FINAL_6Y),15) AS 6Y
    FROM
      ( SELECT /* insert big query here */ ) z
    GROUP BY WEIGHT;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top