Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit a7f4676

Browse files
authored
Performance: use efficient sub-query to find leafs (#562)
* Performance: use efficient sub-query to find leafs
1 parent 9f9af9b commit a7f4676

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Unreleased
2+
- [IMPROVED] Improved efficiency of sub-query when picking winning
3+
revisions. This improves performance when inserting revisions,
4+
including during pull replication.
5+
16
# 2.1.0 (2017-12-04)
27
- [NEW] Added API for upcoming IBM Cloud Identity and Access
38
Management support for Cloudant on IBM Cloud. Note: IAM API key

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/DeleteDocumentCallable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2018 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -77,7 +77,7 @@ public InternalDocumentRevision call(SQLDatabase db) throws ConflictException,
7777
// now check it's a leaf revision
7878
String leafQuery = "SELECT " + CallableSQLConstants.METADATA_COLS + " FROM revs, docs WHERE " +
7979
"docs.docid=? AND revs.doc_id=docs.doc_id AND revid=? AND revs.sequence NOT " +
80-
"IN (SELECT DISTINCT parent FROM revs WHERE parent NOT NULL) ";
80+
"IN (SELECT DISTINCT parent FROM revs revs_inner WHERE parent NOT NULL AND revs_inner.doc_id=docs.doc_id) ";
8181
c = db.rawQuery(leafQuery, new String[]{docId, prevRevId});
8282
boolean isLeaf = c.moveToFirst();
8383
if (!isLeaf) {

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/PickWinningRevisionCallable.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2018 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -39,13 +39,13 @@ public class PickWinningRevisionCallable implements SQLCallable<Void> {
3939
// gets all revs whose sequence is not a parent of another rev and the rev isn't deleted
4040
public static final String GET_NON_DELETED_LEAFS = "SELECT revs.revid, revs.sequence FROM " +
4141
"revs WHERE revs.doc_id = ? AND revs.deleted = 0 AND revs.sequence NOT IN " +
42-
"(SELECT DISTINCT parent FROM revs WHERE parent NOT NULL) ";
42+
"(SELECT DISTINCT parent FROM revs revs_inner WHERE parent NOT NULL AND revs_inner.doc_id = revs.doc_id) ";
4343

4444
// get all leaf rev IDs for a given doc ID
4545
// gets all revs whose sequence is not a parent of another rev
4646
public static final String GET_ALL_LEAFS = "SELECT revs.revid, revs.sequence FROM revs " +
4747
"WHERE revs.doc_id = ? AND revs.sequence NOT IN " +
48-
"(SELECT DISTINCT parent FROM revs WHERE parent NOT NULL) ";
48+
"(SELECT DISTINCT parent FROM revs revs_inner WHERE parent NOT NULL AND revs_inner.doc_id = revs.doc_id) ";
4949

5050
private final long docNumericId;
5151

@@ -127,7 +127,7 @@ public Void call(SQLDatabase db) throws DocumentStoreException {
127127
currentFalse.put("current", 0);
128128
db.update("revs", currentFalse,
129129
"sequence!=? AND doc_id=? AND sequence NOT IN " +
130-
"(SELECT DISTINCT parent FROM revs WHERE parent NOT NULL)",
130+
"(SELECT DISTINCT parent FROM revs revs_inner WHERE parent NOT NULL AND revs_inner.doc_id=revs.doc_id)",
131131
new String[]{Long.toString(newWinnerSeq), Long.toString(docNumericId)});
132132
return null;
133133
}

0 commit comments

Comments
 (0)