Skip to content

Commit 0cdaa66

Browse files
committed
Use a faster query to perform the table trimming
1 parent 2f5af67 commit 0cdaa66

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

daiquiri/core/adapter/database/postgres.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,14 @@ def trim_table_rows(self, schema_name, table_name, max_records):
370370
return
371371

372372
user_table = f'{self.escape_identifier(schema_name)}.{self.escape_identifier(table_name)}'
373-
query = f"""DELETE FROM {user_table}
374-
WHERE ctid NOT IN (SELECT ctid FROM {user_table} LIMIT %s);
373+
query = f"""DELETE FROM {user_table} as t
374+
USING (
375+
SELECT ctid
376+
FROM {user_table}
377+
ORDER BY ctid
378+
OFFSET %s
379+
) as d
380+
WHERE t.ctid = d.ctid;
375381
"""
376382
self.execute(query, args=[max_records,])
377383

0 commit comments

Comments
 (0)