Php 优化/替换此自引用更新查询

Php 优化/替换此自引用更新查询,php,mysql,sql,optimization,Php,Mysql,Sql,Optimization,我有此更新查询: UPDATE aggregate_usage_input t JOIN (SELECT t2.id FROM aggregate_usage_input t2 WHERE t2.is_excluded_total_gallons = 0 AND t2.is_excluded_cohort = 0 AND t2.is_e

我有此更新查询:

UPDATE aggregate_usage_input t 
       JOIN (SELECT t2.id 
             FROM   aggregate_usage_input t2 
             WHERE  t2.is_excluded_total_gallons = 0 
                    AND t2.is_excluded_cohort = 0 
                    AND t2.is_excluded_outlier = 0 
             ORDER  BY t2.occupant_bucket_id, 
                       t2.residence_type_bucket_id, 
                       t2.reading_year, 
                       t2.nthreading, 
                       t2.total_gallons)t_sorted 
         ON t_sorted.id = t.id 
SET    t.rownum = @rownum := @rownum + 1 
它根据排序更新rownum字段(实际上是orderby字段)

select查询需要9秒钟,因为我们使用order by,所以它是可以接受的

此查询的更新部分需要很长时间。在400.000记录表上记录超过5分钟。我们需要把它缩短到一分钟左右


如何加快速度,或者您是否有其他方法来解决此问题?

子查询将在此处减慢您的速度。在实践中,我注意到将子查询分离成临时表或表变量更快

尝试:


你不明白我的问题。ORDERBY是这个查询的本质,这就是我们这样做的原因。
CREATE TEMPORARY TABLE Temp (id int);

INSERT INTO Temp
SELECT t2.id 
FROM   aggregate_usage_input t2 
WHERE  t2.is_excluded_total_gallons = 0 
    AND t2.is_excluded_cohort = 0 
    AND t2.is_excluded_outlier = 0 
ORDER  BY t2.occupant_bucket_id, 
    t2.residence_type_bucket_id, 
    t2.reading_year, 
    t2.nthreading, 
    t2.total_gallons;

UPDATE aggregate_usage_input t
JOIN Temp t_sorted
     ON t_sorted.id = t.id 
SET t.rownum = @rownum := @rownum + 1