Removed DISTINCT from member labels association query (#12088)

no issue

- bookshelf adds `DISTINCT` to any relation query that does not have an explicit `columns` statement
- when measuring the impact of `DISTINCT` on the eager-loading association query when listing members using `{withRelated: 'labels'}`, it can be 2x slower with no index on the sort_order column or 4x slower with an index on sort_order
This commit is contained in:
Kevin Ansfield 2020-07-29 12:50:22 +01:00 committed by GitHub
parent d9da01ea85
commit 577a934f53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,7 +25,12 @@ const Member = ghostBookshelf.Model.extend({
labels: function labels() { labels: function labels() {
return this.belongsToMany('Label', 'members_labels', 'member_id', 'label_id') return this.belongsToMany('Label', 'members_labels', 'member_id', 'label_id')
.withPivot('sort_order') .withPivot('sort_order')
.query('orderBy', 'sort_order', 'ASC'); .query('orderBy', 'sort_order', 'ASC')
.query((qb) => {
// avoids bookshelf adding a `DISTINCT` to the query
// we know the result set will already be unique and DISTINCT hurts query performance
qb.columns('labels.*');
});
}, },
stripeCustomers() { stripeCustomers() {