我希望有人可以解释为什么这个查询需要花费近3分钟来执行,结果只有18行。
添加到WHERE
子句以显示最多2个月前的结果时会出现问题。
执行以下查询大约需要2分40秒:
SELECT carts.id AS id, carts.order_id, carts.product_id, users.title, users.first_name, users.last_name, details.date_time, details.status
FROM orders_carts AS carts
LEFT JOIN orders_details AS details
ON carts.order_id = details.order_id
LEFT JOIN orders_users AS users
ON users.order_id = details.order_id
WHERE carts.product_id = '11698'
AND details.date_time >= now()-INTERVAL 2 month
ORDER BY id DESC;
然而,此查询在大约1.5秒内执行:
SELECT carts.id AS id, carts.order_id, carts.product_id, users.title, users.first_name, users.last_name, details.date_time, details.status
FROM orders_carts AS carts
LEFT JOIN orders_details AS details
ON carts.order_id = details.order_id
LEFT JOIN orders_users AS users
ON users.order_id = details.order_id
WHERE carts.product_id = '11698'
ORDER BY id DESC
唯一添加的是AND details.date_time >= now()-INTERVAL 2 month
,我不知道为什么它会导致滞后。有人可以帮忙吗?