Skip to content

Commit

Permalink
Create temporary table for search without transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
dstillman committed Mar 9, 2017
1 parent 580cc32 commit 1711ba4
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions chrome/content/zotero/xpcom/data/search.js
Expand Up @@ -582,9 +582,6 @@ Zotero.Search.prototype.search = Zotero.Promise.coroutine(function* (asTempTable
if (!ids) {
return [];
}

Zotero.debug('g');
Zotero.debug(ids);
tmpTable = yield Zotero.Search.idsToTempTable(ids);
}
// Otherwise, just copy to temp table directly
Expand Down Expand Up @@ -897,26 +894,20 @@ Zotero.Search.prototype.getSQLParams = Zotero.Promise.coroutine(function* () {
/*
* Batch insert
*/
Zotero.Search.idsToTempTable = function (ids) {
const N_COMBINED_INSERTS = 1000;

Zotero.Search.idsToTempTable = Zotero.Promise.coroutine(function* (ids) {
var tmpTable = "tmpSearchResults_" + Zotero.randomString(8);

return Zotero.DB.executeTransaction(function* () {
var sql = "CREATE TEMPORARY TABLE " + tmpTable + " (itemID INTEGER PRIMARY KEY)";
yield Zotero.DB.queryAsync(sql);

var ids2 = ids ? ids.concat() : [];
while (ids2.length) {
let chunk = ids2.splice(0, N_COMBINED_INSERTS);
let sql = 'INSERT INTO ' + tmpTable + ' VALUES '
+ chunk.map((x) => "(" + parseInt(x) + ")").join(", ");
yield Zotero.DB.queryAsync(sql, false, { debug: false });
}

return tmpTable;
});
}
Zotero.debug(`Creating ${tmpTable} with ${ids.length} item${ids.length != 1 ? 's' : ''}`);
var sql = "CREATE TEMPORARY TABLE " + tmpTable + " AS "
+ "WITH cte(itemID) AS ("
+ "VALUES " + ids.map(id => "(" + parseInt(id) + ")").join(',')
+ ") "
+ "SELECT * FROM cte";
yield Zotero.DB.queryAsync(sql, false, { debug: false });
yield Zotero.DB.queryAsync(`CREATE UNIQUE INDEX ${tmpTable}_itemID ON ${tmpTable}(itemID)`);

return tmpTable;
});


/*
Expand Down

0 comments on commit 1711ba4

Please sign in to comment.