在MySQL查询上运行EXPLAIN会出现语法错误

时间:2014-04-04 17:50:01

标签: mysql sql amazon-web-services amazon-rds

我有一个基本查询,看起来像下面那个运行缓慢,所以我试图弄清楚哪里缺少索引或我可以进行其他优化:

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;

此查询运行速度非常慢,因此我尝试使用以下内容进行一次简单的更改:

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;

当我弹出EXPLAIN时,我收到语法错误。我在Amazon RDS上使用MySQL 5.6.13。错误如下所示:

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

2 个答案:

答案 0 :(得分:1)

您必须以分号结束sql命令。

更改为

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

答案 1 :(得分:1)

仅使用EXPLAINSELECT查询,而不需要前两行来删除/创建表格:

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;