MySQL简单内连接 - 内存不足 - 为什么这个查询的一个版本会产生内存不足错误?

时间:2014-01-14 14:09:31

标签: mysql

以下mysql查询导致内存不足错误:

SELECT 
   TableB.gac_text, 
   gac_id, 
   gac_type, 
   gsacd_widget, 
   gsacd_sales 
FROM 
    TableA 
    JOIN TableB 
        ON gac_id = TableB.gac_id 
    WHERE gsacd_widget > 1000 AND gsacd_sales < 5;

但是一个非常相似的查询,以这种方式重写(感谢sqlYog)不会产生内存不足的错误。为什么呢?

SELECT
    `TableA`.`gsacd_id`
    , `TableA`.`gsacd_account`
    , `TableA`.`gsacd_widget`
    , `TableA`.`gsacd_calls`
    , `TableA`.`gsacd_ctr`
    , `TableA`.`gsacd_sales`
    , `TableA`.`gsacd_abc`
    , `TableA`.`gsacd_cost`
    , `TableA`.`gsacd_revenue`
    , `TableB`.`gac_text`
FROM
    `project_name_v8`.`TableA`
    INNER JOIN `project_name_v8`.`TableB` 
        ON (`TableA`.`gsacd_gac_id` = `TableB`.`gac_id`)        
    WHERE gsacd_widget > 1000 AND gsacd_sales < 5;

1 个答案:

答案 0 :(得分:4)

您将加入TableB联接中的TableB

SELECT 
  TableB.gac_text, 
  gac_id, 
  gac_type, 
  gsacd_widget, 
  gsacd_sales 
FROM 
  TableA 
JOIN TableB 
    ON gac_id = TableB.gac_id  -- <---- This join is joining TableB to TableB, a infinite resultset
WHERE gsacd_widget > 1000 AND gsacd_sales < 5;

JOIN应写成:

JOIN TableB 
    ON TableA.gsacd_gac_id = TableB.gac_id