0

我有 2 个 mysql 表,其中一个在另一个表中有一个外键。当 mysqldump 使用 where 语句选择性地转储某些子集时,第二个 mysqldump 进入准备状态并永远挂起。我该如何解决?

mysqldump -u$username -p$password --quick --lock-all-tables --where="table1_id<1000" $db table1 > $dump_dir/table1.sql                                                                                                 

mysqldump -u$username -p$password --quick --lock-all-tables --where="table2_id in (select table2_id from table1 where table1_id<1000)" $db table2 >$dump_dir/table2.sql
4

1 回答 1

0

Here's a couple of things you could try.

Try adding indexes:

... ON table1 (table1_id, table2_id)

Also, try adding the DISTINCT keyword in subquery:

 table2_id in (select DISTINCT table2_id from table1 where table1_id<1000)

Other than performance of the query that MySQL is using, I don't see anything there that should cause a "hang", other than that lock-all-tables option. I'm assuming you've tested without that, and observed the same behavior.


Q: What do you suspect is causing the hang?

I suspect it's just the SELECT statement that is cranking. I think it's likely that for EVERY row in table2, the execution plan is doing a nested loops lookup from table1. I suspect that the IN (subquery) predicate is being optimized into an equivalent EXISTS (correlated subquery).

Q: Why would adding indexes help?

Indexes may not help at all. But an appropriate index on table1 may cut down on the amount of time required to retrieve the results from the subquery. (It's likely the results from the subquery will be "cached", so that there will only be a savings when that subquery is first run, adn not a savings for each row from table2 that is evaluated.)

于 2013-01-15T00:10:35.030 回答