Skip to content

Commit 24f0ab3

Browse files
authored
Optimize all_resource_timestamps() (#3597)
1 parent 6bc5844 commit 24f0ab3

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

kinto/core/storage/postgresql/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Storage(StorageBase, MigratorMixin):
7979

8080
# MigratorMixin attributes.
8181
name = "storage"
82-
schema_version = 24
82+
schema_version = 25
8383
schema_file = os.path.join(HERE, "schema.sql")
8484
migrations_directory = os.path.join(HERE, "migrations")
8585

@@ -252,20 +252,20 @@ def all_resources_timestamps(self, resource_name):
252252
WITH existing_timestamps AS (
253253
-- Timestamp of latest object by parent_id.
254254
(
255-
SELECT parent_id, MAX(last_modified) AS last_modified
255+
SELECT DISTINCT ON (parent_id) parent_id, last_modified
256256
FROM objects
257257
WHERE resource_name = :resource_name
258-
GROUP BY parent_id
258+
ORDER BY parent_id, last_modified DESC
259259
)
260260
-- Timestamp of resources without sub-objects.
261-
UNION
261+
UNION ALL
262262
(
263263
SELECT parent_id, last_modified
264264
FROM timestamps
265265
WHERE resource_name = :resource_name
266266
)
267267
)
268-
SELECT parent_id, MAX(as_epoch(last_modified)) AS last_modified
268+
SELECT parent_id, as_epoch(MAX(last_modified)) AS last_modified
269269
FROM existing_timestamps
270270
GROUP BY parent_id
271271
ORDER BY last_modified DESC
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE INDEX IF NOT EXISTS idx_objects_parent_id_record_last_modified
2+
ON objects (parent_id, last_modified DESC)
3+
WHERE resource_name = 'record';
4+
5+
-- Bump storage schema version.
6+
INSERT INTO metadata (name, value) VALUES ('storage_schema_version', '25');

kinto/core/storage/postgresql/schema.sql

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ CREATE INDEX IF NOT EXISTS idx_objects_last_modified_epoch
4949
ON objects(as_epoch(last_modified));
5050
CREATE INDEX IF NOT EXISTS idx_objects_resource_name_parent_id_deleted
5151
ON objects(resource_name, parent_id, deleted);
52+
-- Index for collections timestamps
53+
CREATE INDEX IF NOT EXISTS idx_objects_parent_id_record_last_modified
54+
ON objects (parent_id, last_modified DESC)
55+
WHERE resource_name = 'record';
5256
-- Index for history plugin trimming.
5357
CREATE INDEX IF NOT EXISTS idx_objects_history_userid_and_resourcename
54-
ON objects ((data->'user_id'), (data->'resource_name'))
55-
WHERE resource_name = 'history';
58+
ON objects ((data->'user_id'), (data->'resource_name'))
59+
WHERE resource_name = 'history';
5660

5761
CREATE TABLE IF NOT EXISTS timestamps (
5862
parent_id TEXT NOT NULL COLLATE "C",
@@ -136,4 +140,4 @@ INSERT INTO metadata (name, value) VALUES ('created_at', NOW()::TEXT);
136140

137141
-- Set storage schema version.
138142
-- Should match ``kinto.core.storage.postgresql.PostgreSQL.schema_version``
139-
INSERT INTO metadata (name, value) VALUES ('storage_schema_version', '24');
143+
INSERT INTO metadata (name, value) VALUES ('storage_schema_version', '25');

0 commit comments

Comments
 (0)