简体   繁体   English

MySQL-多线程更新(没有线程会更新相同的ID),但有死锁

[英]MySQL - Multi-threaded Update (no thread updates the same id) but having Deadlock

I have a multi threaded java program that is running update statements on my MySQL DB. 我有一个多线程Java程序,正在我的MySQL DB上运行更新语句。 When I use threads I get deadlock although I am never updating the same rows in any two threads. 当我使用线程时,尽管我从未在任何两个线程中更新相同的行,但会出现死锁。 For each time this query is ran field_a is different so why would I have lock issues? 对于每次运行此查询,field_a都不相同,所以为什么会有锁问题?

thread1: field_a - 'A'
thread2: field_a - 'B'
thread3: field_a - 'C'

I am running a query like this 我正在运行这样的查询

 UPDATE table as t, 
            (
             SELECT field_a,
                   field_b,
                   TRUNCATE(AVG(Sumfield_c), 2) avgfield_c,
                   TRUNCATE(AVG(Sumfield_d), 2) avgfield_d
             FROM
              (SELECT field_a,
                      field_b,
                      DateString,
                      sum(field_c) Sumfield_c,
                      sum(field_d) Sumfield_d
               FROM table
               WHERE DateString > DATE_FORMAT(SUBDATE(CURDATE(), 22), '%Y%m%d') and field_a = ? and id <= ?
               GROUP BY field_a,
               field_b,
                        DateString) A
             GROUP BY field_a,
                     field_b
            ) as temp
             SET t.Avgfield_c = temp.avgfield_c, t.Avgfield_d = temp.avgfield_d WHERE t.field_a = temp.field_a and t.field_b = temp.field_b and t.id > ?;

After hours and hours I got it working by creating a new index. 几个小时后,我通过创建新索引使其工作。

CREATE INDEX newIndex ON table (fieldA, fieldB); CREATE INDEX newIndex ON表(fieldA,fieldB);

Hope this helps someone in the future running a similar query with threading. 希望这有助于将来使用线程运行类似查询的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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