MySql尝试提高查询性能但是在列上出错

时间:2013-08-01 10:37:21

标签: mysql

这实际上是两个问题,但我会尽可能地尽力而为。

我向朋友展示了我的初步查询,告诉我使用子选择(使用MySql Here )可以进一步提高性能。

不幸的是,由于我有一个Error Code: 1054 Unknown column 'e.id' in 'where clause'

,我无法让他的查询工作

他的下面的问题:如果有人告诉我为什么它无法识别e.id,我将不胜感激。

SELECT e.id AS event_id, er.name AS event_name, er.revision AS revision, er.start_date AS event_dates
 FROM `events` AS e
 INNER JOIN 
 (
    SELECT event_id, revision, start_date 
    FROM event_revisions
    WHERE YEAR(start_date) = YEAR(CURDATE())
    AND revision = (
       SELECT MAX(er_tmp.revision) 
       FROM event_revisions AS er_tmp
       WHERE er_tmp.approved = 1
       AND e.id = er_tmp.event_id
    )
 ) AS er ON (e.id = er.event_id)
 INNER JOIN member_events AS me ON (me.event_id = e.id)
 WHERE 
  e.enabled = 1 AND 
  e.deleted = 0
 GROUP BY event_id
 ORDER BY event_dates

这是我原来的疑问:

SELECT e.id AS event_id, er.name AS event_name, er.revision AS revision, er.start_date AS event_dates
FROM `events` AS e
INNER JOIN event_revisions AS er ON (e.id = er.event_id)
INNER JOIN member_events AS me ON (me.event_id = e.id)
WHERE 
    e.enabled = 1 AND 
    e.deleted = 0 AND 
    er.revision = (
        SELECT MAX(er_tmp.revision) 
        FROM event_revisions AS er_tmp
        WHERE er_tmp.approved = 1
        AND  e.id = er_tmp.event_id
    )
    AND YEAR(er.start_date) = YEAR(CURDATE())
GROUP BY event_id
ORDER BY event_dates

第二个问题是:为什么他的查询会比我的查询更快?

任何输入都非常受欢迎。

1 个答案:

答案 0 :(得分:1)

AD 1.因为子选择:

 SELECT MAX(er_tmp.revision) 
    FROM event_revisions AS er_tmp
    WHERE er_tmp.approved = 1
    AND  e.id = er_tmp.event_id

为您查询中的每一行“调用”。

(公元2年),正如@Dor告诉你的那样,你正在使用子查询中的alaias