0

以下 SQL 的准备时间为 30+ 秒。SQL是错误的,还是我有接近一百万的事实导致数据库?可以优化此 SQL 使其不准备那么长时间吗?

UPDATE url_source_wp SET hash="ASDF2"
WHERE (url_source_wp.id NOT IN (
   SELECT url_done_wp.url_source_wp FROM url_done_wp WHERE url_done_wp.url_group = 4)
)
AND (hash IS NULL) LIMIT 50
4

2 回答 2

0

It seems like you could more optimally do this update across a JOIN, avoiding the use of the sub-select.

UPDATE
  url_source_wp AS s
  INNER JOIN url_done_wp AS d
    ON s.id = d.url_source_wp
SET
  s.hash = 'ASDF2'
WHERE
  s.hash IS NULL
  AND d.url_group = 4

You need to make sure you have indexes on s.id, d.url_source_wp, s.hash, and d.url_group. Also, note that you can't use LIMIT with multi-table syntax, so if this is important this suggestion will likely not work for you.

于 2013-04-19T16:51:45.183 回答
0

如果准备是您的问题,您可以将其预编译为存储过程。

看到这个:http ://dev.mysql.com/doc/refman/5.0/en/stored-routines.html

于 2013-04-19T16:45:44.253 回答