Skip to content

Commit a59beae

Browse files
authored
Merge pull request #40 from powersync-ja/fix-duplicate-id-performance
Fix performance issue with many duplicate ids
2 parents 1c56794 + 0f56b71 commit a59beae

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

crates/core/src/sync_local.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,17 @@ pub fn sync_local(db: *mut sqlite::sqlite3, _data: &str) -> Result<i64, SQLiteEr
5555

5656
// Query for updated objects
5757

58-
// QUERY PLAN
59-
// |--SCAN buckets
60-
// |--SEARCH b USING INDEX ps_oplog_by_opid (bucket=? AND op_id>?)
61-
// |--SEARCH r USING INDEX ps_oplog_by_row (row_type=? AND row_id=?)
62-
// `--USE TEMP B-TREE FOR GROUP BY
6358
// language=SQLite
6459
let statement = db
6560
.prepare_v2(
6661
"\
6762
-- 1. Filter oplog by the ops added but not applied yet (oplog b).
63+
-- SELECT DISTINCT / UNION is important for cases with many duplicate ids.
6864
WITH updated_rows AS (
69-
SELECT b.row_type, b.row_id FROM ps_buckets AS buckets
65+
SELECT DISTINCT b.row_type, b.row_id FROM ps_buckets AS buckets
7066
CROSS JOIN ps_oplog AS b ON b.bucket = buckets.id
7167
AND (b.op_id > buckets.last_applied_op)
72-
UNION ALL SELECT row_type, row_id FROM ps_updated_rows
68+
UNION SELECT row_type, row_id FROM ps_updated_rows
7369
)
7470
7571
-- 3. Group the objects from different buckets together into a single one (ops).

0 commit comments

Comments
 (0)