sql >> Base de Datos >  >> RDS >> Mysql

¿Optimizar la consulta de ACTUALIZACIÓN de MySQL que contiene DÓNDE y ORDEN POR?

trate de no usar ORDER-BY y LIMIT para una cantidad tan pequeña de actualizaciones.

    UPDATE companies t1
    join
    (
        SELECT c.id,@RowNum:[email protected]+1 AS RowID
        FROM companies c, (SELECT @RowNum := 0)r
        WHERE c.crawling = 0 AND c.url_host IS NOT NULL
        ORDER BY c.last_crawled ASC
    )t2
    ON t2.RowID=1 AND t1.id=t2.id
    SET t1.crawling = 1

EDITAR:1

asegúrese de tener el índice en (last_crawled ASC, id ASC)

    UPDATE companies t1
    join
    (
        Select ID,RowID
        From
        (
            SELECT c.id,@RowNum:[email protected]+1 AS RowID
            FROM companies c, (SELECT @RowNum := 0)r
            WHERE c.crawling = 0 AND c.url_host IS NOT NULL
            ORDER BY c.last_crawled ASC
        )t2
        WHERE ROWID=1
    )t3
    ON t1.id=t3.id
    SET t1.crawling = 1