简体   繁体   English

将> =更改为<=会导致查询超出执行时间限制

[英]Changing >= to <= causes query to exceed execution time limit

I have a query that checks inventory for vehicles that have not been seen in a week but that are not listed in the sold_vehicles table. 我有一个用于检查库存查询vehicles尚未见过一个星期,但未在上市sold_vehicles表。

If I run this query: 如果我运行此查询:

  SELECT all_vehicles.Vin
  FROM all_vehicles
  LEFT JOIN sold_vehicles
    ON all_vehicles.Vin = sold_vehicles.Vin
  WHERE
    sold_vehicles.id IS NULL        AND
    all_vehicles.last_seen >= 1502672069
  ORDER BY all_vehicles.id ASC
  LIMIT 1000

Everything works fine. 一切正常。

However, if I reverse the >= to <= it exceeds the 120 second execution time limit. 但是,如果我将>=反转为<=则超过了120秒的执行时间限制。 Why would reversing this behave this way? 为什么要扭转这种现象呢? Any reason other than a lot of results to return, and wouldn't the limit fix that? 除了返回大量结果以外,还有其他原因吗?

To be clear, this is nearly the exact same query, but with <= and a LIMIT 10 yet fails to execute: 需要明确的是,这几乎是完全相同的查询,但是使用<=LIMIT 10仍无法执行:

  SELECT all_vehicles.Vin
  FROM all_vehicles
  LEFT JOIN sold_vehicles
    ON all_vehicles.Vin = sold_vehicles.Vin
  WHERE
    sold_vehicles.id IS NULL        AND
    all_vehicles.last_seen <= 1502672069
  ORDER BY all_vehicles.id ASC
  LIMIT 10

Any ideas? 有任何想法吗? Is it amount of results found alone? 是否仅发现一个结果量? How do I fix it other than LIMIT ? 除了LIMIT以外,我该如何解决?

Start by removing the order by . 首先通过删除order by Does this return anything? 这会返回什么吗?

SELECT av.Vin
FROM all_vehicles av LEFT JOIN
     sold_vehicles sv
     ON av.Vin = sv.Vin
WHERE sv.id IS NULL AND
      av.last_seen <= 1502672069
--ORDER BY av.id ASC
LIMIT 10;

Then, I would create indexes on all_vehicles(last_seen, Vin, id) and sold_vehicles(Vin, id) . 然后,我将在all_vehicles(last_seen, Vin, id)sold_vehicles(Vin, id)上创建索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM