简体   繁体   中英

performance issue when using WHERE EXISTS condition

I have two tables that hold GBs of data.

When I perform a query with "WHERE EXISTS ...", on either table, the whole MySQL goes down.

Example of the query:

DELETE FROM `records`  
where exists (
    select * 
    from `measurements` 
    where `file_id` = 17
    and measurements.id = records.measurement_id
    ) 

I am not sure on where to start debugging or how this can be solved.

Example of a query which works fine on the same server running on a different database, but takes forever on the main DB

select * 
from `params` 
where exists (
    select * 
    from `records` 
    where `params`.`record_id` = `records`.`id` and exists (
        select * 
        from `measurements` 
        where `records`.`measurement_id` = `measurements`.`id`
            and `file_id` = 17"
    )
)

You simply perform DELETE on entire table if subquery returns any row:

DELETE FROM `records`;

I believe you want correlated subquery:

DELETE FROM `records`  
where exists (
  select * 
  from `measurements` 
  where `file_id` = 17
   and `measurements`.col_name = `records`.col_name 
) ;

EDIT:

You are correct about correlation part. The query still taking DB down

You should check metrics (how many % of intital table you are removing). If more than 20-30% I would simply use CTAs and recreate table.

Second thing: you need to check FK ( ON DELETE CASCADE )

I had to rewrite query using LEFT JOINs to perform deletes. The reason why "exists" took too long is because "exists" has to check each record against the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM