价格
ID
id_stock
价格
日期

**stocks**
id
stock_name
active

现在我必须为每只具有 stocks.active=0(我必须通过的日期)的股票设置 MAX(prices.date) > 15 days
这是我的查询,但它非常非常慢!!!
update stocks set stocks.active=0 where stocks.id IN (

SELECT prices.id_stock
FROM prices
GROUP BY prices.id_stock
HAVING datediff('2010-08-17', MAX( prices.date )) > 15

)

如何优化它?

非常感谢!

最佳答案

UPDATE  stocks
SET     active = 0
WHERE   DATEDIFF('2010-08-17',
        (
        SELECT  MAX(prices.date)
        FROM    prices
        WHERE   id_stock = stocks.id
        )) > 15

prices (id_stock, date) 上创建索引

关于sql - 如何优化这个查询?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3514505/

10-13 04:29