0

I'm trying to query our entire database to build a report and I've run into the problem that I have queries like the one below that are taking near 2 hours to run and some take even longer. I've changed the some of the names but the structure is the same.

Person_table has about 5 million rows

check_table is a subset of Person_table and has about 3 million rows

event_table has about 20 million rows

update  check_table as CT
    inner join  
        ( SELECT  B1.id
            from  Person_table as PT
            inner join  event_table as B1  ON PT.id = B1.person_id
            inner join  event_type_table as E using (code_id)
            inner join  
                ( SELECT  *
                    from  event_table as ET
                    where  ET.code_id in ('XYZ','ABC','DEF','ETC.')
                ) as F  ON ET.id = F.person_id
                       and  B1.Event_date = F.event_date
        ) as B2 using (id) set flag_1=1;

I've tried to get an explain on many of the queries I have to do but it always spits back a "Explain Data not available for statement".

4
  • Have you applied index to B.person_id and A.code_id and F.person_id fields? Commented Mar 7, 2018 at 11:10
  • I think you want to try it such as EXPLAIN select B.id from Person_table as A inner join event_table as B on A.id=B.person_id inner join event_type_table as E using (code_id) inner join (select * from event_table as A where A.code_id in ('XYZ','ABC','DEF','ETC.')) as F on A.id = F.person_id and B.Event_date = F.event_date) as B using (id) set flag_1=1; Commented Mar 7, 2018 at 13:28
  • Please do not reuse an alias (A); it makes the query hard to read.
    – Rick James
    Commented Mar 9, 2018 at 17:24
  • EXPLAIN UPDATE ... is available only in newer versions of MySQL.
    – Rick James
    Commented May 15, 2018 at 15:56

1 Answer 1

0

It may (or may not) be possible to add some composite indexes to speed up the query. It may (or may not) be possible to reformulate the query to speed it up -- perhaps by turning subqueries into JOINs.

But it seems like 3M possible updates will lead to a very slow query in any case. I suggest chunking the UPDATE into no more than 1000 rows at a time. This will avoid various locks that interfere with other traffic. And it may actually run faster -- due to not needing to hang onto millions of rows for potential ROLLBACK.

Discussion of chunking: http://mysql.rjweb.org/doc.php/deletebig#deleting_in_chunks

If you want me to check your indexes, please provide SHOW CREATE TABLE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.