sql - 我怎样才能快速删除 postgres 中的行?

标签 sql postgresql

我有一个包含 17,000,000 行的表。我需要在某些条件下删除 500,000。现在我有一个 500,000 行的脚本,看起来像

delete from table where name = 'John' and date = '2010-08-04';
delete from table  where name = 'John' and date = '2010-08-05';
delete from table  where name = 'Adam' and date = '2010-08-06';

一行执行大约 2.5 秒。它太长了。我怎样才能提高速度?

最佳答案

如果名称和日期字段上没有索引,则尝试创建以下索引并尝试您的代码。

CREATE INDEX idx_table_name_date  ON table  (name, date)

如果可能,您还可以通过合并删除语句来减少删除语句的数量。

代替

delete from table where name = 'John' and date = '2010-08-04';
delete from table  where name = 'John' and date = '2010-08-05';

可以是:

delete from table where name = 'John' and date in('2010-08-04','2010-08-05');

关于sql - 我怎样才能快速删除 postgres 中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67673798/

相关文章:

postgresql 将完整数据读取为一个 json

java - 为什么@NaturalId 不在数据库中创建唯一约束?

即使没有定义 varchar_pattern_ops 并且不使用 "C"区域设置,Postgresql 索引扫描也会使用 LIKE 查询执行

sql - 创建没有外键的 LINQ 关联

sql - 在oracle sql中查找两个日期之间耗时

postgresql - postgres中用逗号分割字符串

按用户和行数据的 Postgresql 行策略

sql - Oracle 是否有内置函数可以按特定顺序连接多个字段?

mysql - 找出最大的单个数

sql - 在SQL中将大数据集分成相等的列表