不要怂,就是干,撸起袖子干!

Commit 3c71d152 by Mick Hansen

Improve performance in cases where we have a single primary key

1 parent d9debb39
Showing with 35 additions and 10 deletions
......@@ -230,7 +230,11 @@ module.exports = (function() {
// Queries with include
} else if (this.options.hasJoin === true) {
results = groupJoinData(results, this.options, {
results = groupJoinData(results, {
daoFactory: this.callee,
includeMap: this.options.includeMap,
includeNames: this.options.includeNames
}, {
checkExisting: this.options.hasMultiAssociation
})
......@@ -338,7 +342,22 @@ module.exports = (function() {
, calleeData
, child
, calleeDataIgnore = ['__children']
, parseChildren = function(result) {
_.each(result.__children, function (children, key) {
result[key] = groupJoinData(children, (includeOptions.includeMap && includeOptions.includeMap[key]), options)
})
delete result.__children
},
primaryKeyAttribute
// Identify singular primaryKey attribute for equality check (if possible)
if (includeOptions.daoFactory.primaryKeyAttributes.length === 1) {
primaryKeyAttribute = includeOptions.daoFactory.primaryKeyAttributes[0]
} else if (includeOptions.daoFactory.rawAttributes.id) {
primaryKeyAttribute = 'id'
}
// Ignore all include keys on main data
if (includeOptions.includeNames) {
calleeDataIgnore = calleeDataIgnore.concat(includeOptions.includeNames)
}
......@@ -349,10 +368,13 @@ module.exports = (function() {
// If there are :M associations included we need to see if the main result of the row has already been identified
existingResult = options.checkExisting && _.find(results, function (result) {
if (calleeDataIgnore) {
return Utils._.isEqual(_.omit(result, calleeDataIgnore), calleeData)
// If we can, detect equality on the singular primary key
if (primaryKeyAttribute) {
return result[primaryKeyAttribute] === calleeData[primaryKeyAttribute]
}
return Utils._.isEqual(result, calleeData)
// If we can't identify on a singular primary key, do a full row equality check
return Utils._.isEqual(_.omit(result, calleeDataIgnore), calleeData)
})
if (!existingResult) {
......@@ -377,14 +399,17 @@ module.exports = (function() {
}
}
}
// parseChildren in same loop if no duplicate values are possible
if (!options.checkExisting) {
parseChildren(existingResult)
}
})
results.forEach(function (result) {
_.each(result.__children, function (children, key) {
result[key] = groupJoinData(children, (includeOptions.includeMap && includeOptions.includeMap[key]), options)
})
delete result.__children
})
// parseChildren after row parsing if duplicate values are possible
if (options.checkExisting) {
results.forEach(parseChildren)
}
return results
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!