【问题标题】:SQL Update query with Inner join gives Lock wait timeout exceeded带有内部连接的 SQL 更新查询给出了超过锁定等待超时
【发布时间】:2013-12-23 14:21:03
【问题描述】:

我有以下查询在两个表上运行,每个表有超过 80,000 和 40,000 条记录:

update 
profileimageurls piu 
inner join 
profileuniqref pr
on piu.imageurl = pr.uniqueimageurl
set piu.uniqueimagekey = pr.keyid;

profileimageurls的表结构:

 Field              Type                  Null
 KeyID              bigint(20) unsigned   NO
 UserID             bigint(20) unsigned   NO
 ImageURL           varchar(4000)         NO
 UniqueImageURL     varchar(4000)         YES
 Error              varchar(10000)        YES
 Status             tinyint(3) unsigned   YES
 uniqueImageKeyid   bigint(20) unsigned   YES

profileuniqref 的表结构:

 Field          Type
 KeyId          bigint(20) unsigned
 uniqueimageurl varchar(4000)
 md5hash        varchar(32)
 md5hashbinary  varchar(16)

当我运行此查询时,我反复遇到锁定和等待超时。

【问题讨论】:

  • imageurl 列上是否有索引?
  • 在两个表中的 URL 列上添加索引。
  • 让你的查询更快(不超过 50 秒),或者将 innodb_lock_wait_timeout 从默认的 50 秒增加到更多 --> dev.mysql.com/doc/refman/5.0/en/…

标签: mysql sql timeout inner-join


【解决方案1】:

确保在连接条件中使用的列上有索引。

【讨论】:

    【解决方案2】:

    如果连接表之间有外键并且正在更新键,这可能会导致 mysql 更新这两个表之间的键数据。

    【讨论】:

    • 仅供参考:我指的是 profileuniqre 表中的 uniqueImageKeyId。但是我没有在两个表之间添加 FK 约束。 this could cause mysql to update key data between these two tables. 是什么意思,它对查询有何影响。
    猜你喜欢
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2011-01-07
    • 1970-01-01
    • 2021-10-15
    • 2015-07-18
    • 2019-11-20
    相关资源
    最近更新 更多