@@ -82,6 +82,11 @@ SELECT
8282
8383FROM generate_bucket_rows;
8484
85+ INSERT OR IGNORE INTO ps_updated_rows (bucket, row_type, row_id)
86+ SELECT b.bucket, b.row_type, b.row_id FROM ps_buckets AS buckets
87+ CROSS JOIN ps_oplog AS b ON b.bucket = buckets.id
88+ AND (b.op_id > buckets.last_applied_op);
89+
8590COMMIT;
8691''' );
8792 // Enable this to see stats for initial data generation
@@ -120,6 +125,71 @@ COMMIT;
120125 // The tests below are for comparing different queries, not run as part of the
121126 // standard test suite.
122127
128+ test ('sync_local new new query' , () {
129+ var timer = Stopwatch ()..start ();
130+ final q = '''
131+ -- 1. Filter by the ops added but not applied yet
132+ WITH updated_rows AS (
133+ SELECT row_type, row_id FROM ps_updated_rows
134+ )
135+
136+ -- 2. Find *all* current ops over different buckets for those objects (oplog r).
137+ SELECT
138+ b.row_type,
139+ b.row_id,
140+ (
141+ -- 3. For each unique row, select the data from the latest oplog entry.
142+ -- The max(r.op_id) clause is used to select the latest oplog entry.
143+ -- The iif is to avoid the max(r.op_id) column ending up in the results.
144+ SELECT iif(max(r.op_id), r.data, null)
145+ FROM ps_oplog r
146+ WHERE r.row_type = b.row_type
147+ AND r.row_id = b.row_id
148+
149+ ) as data
150+ FROM updated_rows b
151+ -- Group for (2)
152+ GROUP BY b.row_type, b.row_id;
153+ ''' ;
154+ db.select (q);
155+ print ('${timer .elapsed .inMilliseconds }ms ${vfs .stats ()}' );
156+ }, skip: skip);
157+
158+ test ('sync_local new old query' , () {
159+ // Same as "new query", but ignoring the data in ps_updated_rows since it's unfair to that test.
160+ var timer = Stopwatch ()..start ();
161+ final q = '''
162+ -- 1. Filter oplog by the ops added but not applied yet (oplog b).
163+ -- We do not do any DISTINCT operation here, since that introduces a temp b-tree.
164+ -- We filter out duplicates using the GROUP BY below.
165+ WITH updated_rows AS (
166+ SELECT b.row_type, b.row_id FROM ps_buckets AS buckets
167+ CROSS JOIN ps_oplog AS b ON b.bucket = buckets.id
168+ AND (b.op_id > buckets.last_applied_op)
169+ )
170+
171+ -- 2. Find *all* current ops over different buckets for those objects (oplog r).
172+ SELECT
173+ b.row_type,
174+ b.row_id,
175+ (
176+ -- 3. For each unique row, select the data from the latest oplog entry.
177+ -- The max(r.op_id) clause is used to select the latest oplog entry.
178+ -- The iif is to avoid the max(r.op_id) column ending up in the results.
179+ SELECT iif(max(r.op_id), r.data, null)
180+ FROM ps_oplog r
181+ WHERE r.row_type = b.row_type
182+ AND r.row_id = b.row_id
183+
184+ ) as data
185+ FROM updated_rows b
186+ -- Group for (2)
187+ GROUP BY b.row_type, b.row_id;
188+ ''' ;
189+ db.select (q);
190+ print ('${timer .elapsed .inMilliseconds }ms ${vfs .stats ()}' );
191+ }, skip: skip);
192+
123193 test ('sync_local new query' , () {
124194 // This is the query we're using now.
125195 // This query only uses a single TEMP B-TREE for the GROUP BY operation,
0 commit comments