MySQL Update query with left join and group by

后端 未结 3 1815
别跟我提以往
别跟我提以往 2020-12-07 15:31

I am trying to create an update query and making little progress in getting the right syntax. The following query is working:

SELECT t.Index1, t.Index2, COUN         


        
3条回答
  •  庸人自扰
    2020-12-07 16:11

    Doing a left join with a subquery will generate a giant temporary table in-memory that will have no indexes.

    For updates, try avoiding joins and using correlated subqueries instead:

    UPDATE
        Table AS t
    SET
        t.SpecialEventCount = (
            SELECT COUNT(m.EventType)
            FROM MEvents m
            WHERE m.EventType in ('A','B')
              AND m.Index1 = t.Index1
              AND m.Index2 = t.Index2
        )
    WHERE
        t.SpecialEventCount IS NULL
    

    Do some profiling, but this can be significantly faster in some cases.

提交回复
热议问题