Fix extremely slow tag query with some SQLite databases

Reverts a80f13099, "Avoid temporary table when getting tags for current view",
fixes a couple things, and removes the unused Zotero.Tags.getAllWithinSearch().

Fixes #1290
This commit is contained in:
Dan Stillman 2017-11-11 02:56:27 -05:00
parent 08eefeaf57
commit fbf2fbe0c6
2 changed files with 10 additions and 31 deletions

View file

@ -381,8 +381,8 @@ Zotero.CollectionTreeRow.prototype.getChildTags = Zotero.Promise.coroutine(funct
case 'bucket':
return [];
}
var results = yield this.getSearchResults();
return Zotero.Tags.getAllWithinItemsList(results);
var results = yield this.getSearchResults(true);
return Zotero.Tags.getAllWithinSearchResults(results);
});

View file

@ -154,41 +154,20 @@ Zotero.Tags = new function() {
/**
* Get all tags within the items of a Zotero.Search object
* Get all tags within the items of a temporary table of search results
*
* @param {Zotero.Search} search
* @param {Array} [types] Array of tag types to fetch
* @param {String} tmpTable Temporary table with items to use
* @param {Array} [types] Array of tag types to fetch
* @return {Promise<Object>} Promise for object with tag data in API JSON format, keyed by tagID
*/
this.getAllWithinSearch = Zotero.Promise.coroutine(function* (search, types) {
var ids = yield search.search();
return this.getAllWithinItemsList(ids, types);
});
this.getAllWithinItemsList = Zotero.Promise.coroutine(function* (ids, types) {
if (!Array.isArray(ids)) {
throw new Error("ids must be an array");
}
if (!ids.length) {
return [];
}
var prefix = "SELECT DISTINCT name AS tag, type FROM itemTags "
this.getAllWithinSearchResults = Zotero.Promise.coroutine(function* (tmpTable, types) {
var sql = "SELECT DISTINCT name AS tag, type FROM itemTags "
+ "JOIN tags USING (tagID) WHERE itemID IN "
+ "(";
var suffix = ") ";
+ "(SELECT itemID FROM " + tmpTable + ") ";
if (types) {
suffix += "AND type IN (" + types.join() + ") ";
sql += "AND type IN (" + types.join() + ") ";
}
// Don't include ids in debug output
Zotero.DB.logQuery(`${prefix}[...${ids.length}]${suffix}`);
var rows = yield Zotero.DB.queryAsync(
prefix + ids.map(id => parseInt(id)).join(",") + suffix,
false,
{ debug: false }
);
var rows = yield Zotero.DB.queryAsync(sql);
return rows.map((row) => this.cleanData(row));
});