下次,使用一个"alter table"语句来更新主键.
alter table xx drop primary key, add primary key(k1, k2, k3);
要解决问题:
create table fixit (user_2, user_1, type, timestamp, n, primary key( user_2, user_1, type) );
lock table fixit write, user_interactions u write, user_interactions write;
insert into fixit
select user_2, user_1, type, max(timestamp), count(*) n from user_interactions u
group by user_2, user_1, type
having n > 1;
delete u from user_interactions u, fixit
where fixit.user_2 = u.user_2
and fixit.user_1 = u.user_1
and fixit.type = u.type
and fixit.timestamp != u.timestamp;
alter table user_interactions add primary key (user_2, user_1, type );
unlock tables;
锁应该会阻止在您执行此操作时进一步更新.这需要多长时间显然取决于你桌子的大小.
主要问题是,如果您有一些具有相同时间戳的副本.