SQL查询无主键批量删除table

SQL Query for mass deleting for no primary key table

我有一个 SQL Table,它没有主键,但应该有 account_id、键和索引。

但不幸的是,在我注意到这个问题之前,我已经有数百万个条目。

我有 account_id 从 2000001 到 2004000+ 左右,还有几个应该是主键的键,这样只有在有更新时才会替换值。

下面的示例图片, https://i.imgur.com/g6je2zL.png

account_id2000846下应该只有1个#betaminutes和#online_minute,而且这个值应该只有最高的

是否可以对我拥有的所有不同键的每个 account_id 进行批量删除,并且只保存每个 account_id 键的最高值条目?

谢谢

大删除会很费钱,你正在使用。在MySQL中,您可以使用:

delete t
    from t join
         (select account_id, key, max(value) as max_value
          from t
          group by account_id, key
          having count(*) > 1
         ) tt
         using (account_id, key)
     where value < max_value;

注意:如果最高值有联系,那么这将保留联系,但您的示例数据表明没有联系。

您可以按如下方式使用exists

delete from your_table t
 where exists 
       (select 1 from your_Table tt
         where t.account_id = tt.account_id
           and t.key = tt.key
           and tt.value > t.value)
   -- use following condition if there is more than given key and you only want this query to operat on this two keys
   and t.key in ('#betaminutes', '#online_minute')