2017-04-25 27 views
0

表table1包含1500000行和包含80個字段,我想基於Field 1和場2和ID字段,以便移除重複是唯一的,所以我使用的最大選項。MYSQL插入查詢失敗與鎖的總數目超過鎖表大小

選項1:插入選項

insert into table2_unique 
select * from table1 a 
where a.id = (select max(b.id) from table1 b 
       where a.field1 = b.field1 
       and a.field2 = b.field2); 

但查詢失敗,因爲下面的錯誤的。

Error Code: 1206. The total number of locks exceeds the lock table size 

EXPLAIN語句:

id select_type table partitions type possible_keys key key_len ref rows filtered Extra 
1 INSERT table2 NULL ALL NULL NULL NULL NULL NULL NULL NULL 
1 PRIMARY a NULL ALL NULL NULL NULL NULL 1387764 100 Using where 
2 DEPENDENT SUBQUERY b NULL ref field1x,field2x field1x 39 a.field1 537 10 Using where 

選項2 DELETE語句:

DELETE n1 FROM table1 n1, table1 n2 WHERE n1.id > n2.id AND n1.field1 = n2.field1 and n1.field2 and n2.field2 

當我執行再發生死鎖。

我不能增加緩衝池大小,請讓我知道我該寫不同的方式查詢。

+0

哪裏是你的delete語句? –

+0

DELETE語句還發布了 – user3738664

+0

80個字段?聽起來像你缺乏一個正確定義的主鍵不是你唯一的問題。 – symcbean

回答

0

我不知道這將如何影響鎖,但使用相關子查詢(即推謂詞)在MySQL從來沒有在我的經驗,工作非常出色。我會寫第一個查詢爲:

insert into table2_unique (id, col1, col2, ...col79) 
select a.id, a.col1, a.col2, ...a.col79 
from table1 a 
Inner join (
    Select max(b.id) as id 
    From table1 b 
    Group by b.col1, b.col2 
) As dedup 
On a.id=dedup.id; 

嘗試使用連接更新表總是有點狡猾。當它是一個自我加入的時候,那麼它就不會令人驚訝了。使用一個臨時表並將操作分成兩個步驟可以避免這種情況。

0

增加了INNODB_BUFFER_POOL_SIZE在my.ini文件和查詢在27分鐘跑爲指定卷