Skip to content

Commit bf38faf

Browse files
committed
Move annotations to the test file; add a link to docs.
1 parent cf83c7e commit bf38faf

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

crates/core/src/sync_local.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,35 +186,28 @@ impl<'a> SyncOperation<'a> {
186186
Ok(match &self.partial {
187187
None => {
188188
// Complete sync
189+
// See dart/test/sync_local_performance_test.dart for an annotated version of this query.
189190
self.db
190191
.prepare_v2(
191192
"\
192-
-- 1. Filter oplog by the ops added but not applied yet (oplog b).
193-
-- We do not do any DISTINCT operation here, since that introduces a temp b-tree.
194-
-- We filter out duplicates using the GROUP BY below.
195193
WITH updated_rows AS (
196194
SELECT b.row_type, b.row_id FROM ps_buckets AS buckets
197195
CROSS JOIN ps_oplog AS b ON b.bucket = buckets.id
198196
AND (b.op_id > buckets.last_applied_op)
199197
UNION ALL SELECT row_type, row_id FROM ps_updated_rows
200198
)
201199
202-
-- 2. Find *all* current ops over different buckets for those objects (oplog r).
203200
SELECT
204201
b.row_type,
205202
b.row_id,
206203
(
207-
-- 3. For each unique row, select the data from the latest oplog entry.
208-
-- The max(r.op_id) clause is used to select the latest oplog entry.
209-
-- The iif is to avoid the max(r.op_id) column ending up in the results.
210204
SELECT iif(max(r.op_id), r.data, null)
211205
FROM ps_oplog r
212206
WHERE r.row_type = b.row_type
213207
AND r.row_id = b.row_id
214208
215209
) as data
216210
FROM updated_rows b
217-
-- Group for (2)
218211
GROUP BY b.row_type, b.row_id;",
219212
)
220213
.into_db_result(self.db)?

dart/test/sync_local_performance_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ COMMIT;
119119
// standard test suite.
120120

121121
test('sync_local new query', () {
122+
// This is the query we're using now.
122123
// This query only uses a single TEMP B-TREE for the GROUP BY operation,
123124
// leading to fairly efficient execution.
124125

@@ -134,27 +135,40 @@ COMMIT;
134135
// |--USE TEMP B-TREE FOR GROUP BY
135136
// `--CORRELATED SCALAR SUBQUERY 3
136137
// `--SEARCH r USING INDEX ps_oplog_row (row_type=? AND row_id=?)
138+
//
139+
// For details on the max(r.op_id) clause, see:
140+
// https://sqlite.org/lang_select.html#bare_columns_in_an_aggregate_query
141+
// > If there is exactly one min() or max() aggregate in the query, then all bare columns in the result
142+
// > set take values from an input row which also contains the minimum or maximum.
137143

138144
var timer = Stopwatch()..start();
139145
final q = '''
146+
-- 1. Filter oplog by the ops added but not applied yet (oplog b).
147+
-- We do not do any DISTINCT operation here, since that introduces a temp b-tree.
148+
-- We filter out duplicates using the GROUP BY below.
140149
WITH updated_rows AS (
141150
SELECT b.row_type, b.row_id FROM ps_buckets AS buckets
142151
CROSS JOIN ps_oplog AS b ON b.bucket = buckets.id
143152
AND (b.op_id > buckets.last_applied_op)
144153
UNION ALL SELECT row_type, row_id FROM ps_updated_rows
145154
)
146155
156+
-- 2. Find *all* current ops over different buckets for those objects (oplog r).
147157
SELECT
148158
b.row_type,
149159
b.row_id,
150160
(
161+
-- 3. For each unique row, select the data from the latest oplog entry.
162+
-- The max(r.op_id) clause is used to select the latest oplog entry.
163+
-- The iif is to avoid the max(r.op_id) column ending up in the results.
151164
SELECT iif(max(r.op_id), r.data, null)
152165
FROM ps_oplog r
153166
WHERE r.row_type = b.row_type
154167
AND r.row_id = b.row_id
155168
156169
) as data
157170
FROM updated_rows b
171+
-- Group for (2)
158172
GROUP BY b.row_type, b.row_id;
159173
''';
160174
db.select(q);

0 commit comments

Comments
 (0)