Mysql Update IN条件

时间:2019-01-25 14:51:21

标签: mysql

我无法通过查询获得“快速”(<6分钟): (选择者本人最多需要10秒)

UPDATE
    peel_utilisateurs AS PU
SET
    PU.online = 3,
    PU.date_desactivation = NOW()
WHERE
    PU.id_utilisateur IN(
    SELECT
        PC.id_utilisateur
    FROM
        peel_commandes AS PC
    GROUP BY
        PC.id_utilisateur
    HAVING
        MAX(PC.o_timestamp) <(NOW() - INTERVAL 1 YEAR)
    ORDER BY
        `PC`.`o_timestamp`
    DESC
        )

有人可以帮助我吗? :)

谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试使用JOIN更改IN运算符。通常,对于大型数据集,as described here的IN运算符比JOIN慢。

UPDATE
    peel_utilisateurs AS PU
    INNER JOIN peel_commandes AS PC 
    ON 
        PU.id_utilisateur = PC.id_utilisateur 
        AND PC.o_timestamp <(NOW() - INTERVAL 1 YEAR)

SET
    PU.online = 3,
    PU.date_desactivation = NOW()