【问题标题】:2 identfical sql queries w' diff params - only one requires temp table2 个相同的 sql 查询 w' diff params - 只有一个需要临时表
【发布时间】:2011-04-23 12:28:00
【问题描述】:

这些 sql 查询的唯一区别是 record_id 参数(这是我对整个结果集进行分页的方式)。桌子是myisam。第一个查询执行良好,第二个查询非常慢。知道为什么会这样吗?

这个查询工作正常

explain select r.record_id, r.oai_datestamp, r.format_id, r.status,  x.xml,  max(u.date_updated) as date_updated 
from  marcnormalization.records r, 
   marcnormalization.records_xml x, 
   marcnormalization.record_updates u 
where r.record_id = x.record_id 
  and (r.record_id > 1802000 or 1802000 is null) 
  and r.record_id = u.record_id 
  and (u.date_updated > '1960-10-19 10:18:52.0' or '1960-10-19 10:18:52.0' is null) 
  and u.date_updated <= '2010-10-07 10:18:52.0'
group by u.record_id 
order by u.record_id 
limit 1000;

这个查询超级慢(创建一个临时表)

explain select r.record_id, r.oai_datestamp, r.format_id, r.status,  x.xml,  max(u.date_updated) as date_updated 
from  marcnormalization.records r, 
   marcnormalization.records_xml x, 
   marcnormalization.record_updates u 
where r.record_id = x.record_id 
  and (r.record_id > 2202000 or 2202000 is null) 
  and r.record_id = u.record_id 
  and (u.date_updated > '1960-10-19 10:18:52.0' or '1960-10-19 10:18:52.0' is null) 
  and u.date_updated <= '2010-10-07 10:18:52.0'
group by u.record_id 
order by u.record_id 
limit 1000;

更新:我已经通过更改从

解决了我的问题
group by u.record_id 
order by u.record_id 

group by r.record_id 
order by r.record_id 

所以,现在这是一个有争议的问题,但我仍然对最初的问题感到好奇。

【问题讨论】:

    标签: sql mysql myisam


    【解决方案1】:

    我认为这种情况也与您的连接排序区域有关。 你可以增加这个用于会话。 试试这样;

    mysql> 选择@@max_heap_table_size;

    mysql> SET SESSION max_heap_table_size=19777216;

    然后执行查询。

    巴里斯·阿克韦尔迪

    【讨论】:

      【解决方案2】:

      怎么样:

      EXPLAIN
      SELECT r.record_id, r.oai_datestamp, r.format_id, r.status,  x.xml,  max(u.date_updated) as date_updated 
      FROM marcnormalization.records_xml x
      INNER JOIN (SELECT * FROM marcnormalization.records WHERE record_id > 1802000) r
      ON r.record_id = x.record_id
      INNER JOIN (SELECT * FROM marcnormalization.record_updates WHERE date_updated BETWEEN '1960-10-19 10:18:52.0' AND '2010-10-07 10:18:52.0') u
      ON r.record_id = u.record_id
      group by u.record_id ASC
      limit 1000;
      

      我认为它可以更快?

      【讨论】:

        猜你喜欢
        • 2012-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-10
        • 2017-04-19
        • 2022-11-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多