Added option param to skip distinct from count query for members API

ref https://linear.app/tryghost/issue/SLO-173/removed-distinct-from-member-count-query

Performance of GET /members API can be improved by dropping the distinct from the total members count query.

select count(distinct members.id) as aggregate from `members`; // 275ms
select count(*) as aggregate from `members`; // 30ms

In this case we know that the result set will always be unique.
This commit is contained in:
Princi Vershwal 2024-06-27 17:35:19 +05:30 committed by GitHub
parent f9a6610823
commit 7bffe5b79a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 101 additions and 70 deletions

View File

@ -120,6 +120,11 @@ module.exports = function (Bookshelf) {
});
}
//option param to skip distinct from count query, distinct adds a lot of latency and in this case the result set will always be unique.
if (unfilteredOptions.useBasicCount) {
options.useBasicCount = unfilteredOptions.useBasicCount;
}
const response = await itemCollection.fetchPage(options);
// Attributes are being filtered here, so they are not leaked into calling layer
// where models are serialized to json and do not do more filtering.

View File

@ -76,7 +76,7 @@
"@tryghost/api-framework": "0.0.0",
"@tryghost/api-version-compatibility-service": "0.0.0",
"@tryghost/audience-feedback": "0.0.0",
"@tryghost/bookshelf-plugins": "0.6.17",
"@tryghost/bookshelf-plugins": "0.6.19",
"@tryghost/bootstrap-socket": "0.0.0",
"@tryghost/collections": "0.0.0",
"@tryghost/color-utils": "0.2.2",

View File

@ -393,6 +393,9 @@ module.exports = class MemberBREADService {
withRelated.add('email_recipients.email');
}
//option param to skip distinct from count query, distinct adds a lot of latency and in this case the result set will always be unique.
options.useBasicCount = true;
const page = await this.memberRepository.list({
...options,
withRelated: Array.from(withRelated)

161
yarn.lock
View File

@ -7556,95 +7556,95 @@
ajv "^6.12.6"
lodash "^4.17.11"
"@tryghost/bookshelf-collision@^0.1.42":
version "0.1.42"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-collision/-/bookshelf-collision-0.1.42.tgz#a00db529828b4288381fc882f26ef9e891c178a2"
integrity sha512-WCm7xgHR1HQ9NrJ4AGzOo54dfyztGJBKnKoQK7b3fyRwOXr04e1REGhjCkrDAOnXv0TEKUK4y+loF5d96gKWkg==
"@tryghost/bookshelf-collision@^0.1.43":
version "0.1.43"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-collision/-/bookshelf-collision-0.1.43.tgz#6491d47c642565735b0a0cbec65925862a7b88ee"
integrity sha512-RPe5ZpkkETlAmQxuVUKZ6LstYokfb1+mJCt6NMYFIcrXAPW1WSS7l11rCBs7WcuhvVKc6yPD+fwD4MmHuVtJ0w==
dependencies:
"@tryghost/errors" "^1.3.2"
"@tryghost/errors" "^1.3.3"
lodash "^4.17.21"
moment-timezone "^0.5.33"
"@tryghost/bookshelf-custom-query@^0.1.25":
version "0.1.25"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-custom-query/-/bookshelf-custom-query-0.1.25.tgz#b3f7e0e44eb63905631ac0be6de6e500d6fc8087"
integrity sha512-OjKpAL2P9X7BFKOKVD2oAsftUK01AWUBBIuO+FaGosOetIGEeyMZeUV9zI4jB5Us6JNLDiknYzaVZQMd9NsD6w==
"@tryghost/bookshelf-custom-query@^0.1.26":
version "0.1.26"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-custom-query/-/bookshelf-custom-query-0.1.26.tgz#57a0f1772710d2f2866847b333e03b2d2d7f1e3c"
integrity sha512-p4ocob5jchlsPZLw5bycCQwDL026t3HVB1kxZnZHmkdGluTEBpL5/rYCRJj/kGG2zWX/1wzhP1cFFiLCOQjfsQ==
"@tryghost/bookshelf-eager-load@^0.1.29":
version "0.1.29"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-eager-load/-/bookshelf-eager-load-0.1.29.tgz#8d68a709ce3b91055436833f42942de1ced39f09"
integrity sha512-zz1a0D3HER4i2dSRxkA+hTeYZqIdai8Zk+tPYmGTBsm4SrqfQFMR0B9U+FyA8Yn6lCproQx2xFeiCWhThGp9nA==
dependencies:
"@tryghost/debug" "^0.1.30"
lodash "^4.17.21"
"@tryghost/bookshelf-filter@^0.5.14":
version "0.5.14"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-filter/-/bookshelf-filter-0.5.14.tgz#14f5279ccb605a872ae16e5ac49959eb267f4d8d"
integrity sha512-h1dEV9Uq3CvSRCSwTs7Xcx6st2k2aTf0qxqmZppMkiuBKwuijR/e27AFi+n0VfTInYpjkzaiWzAaTs0oijNLJA==
dependencies:
"@tryghost/debug" "^0.1.30"
"@tryghost/errors" "^1.3.2"
"@tryghost/nql" "^0.12.3"
"@tryghost/tpl" "^0.1.30"
"@tryghost/bookshelf-has-posts@^0.1.30":
"@tryghost/bookshelf-eager-load@^0.1.30":
version "0.1.30"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-has-posts/-/bookshelf-has-posts-0.1.30.tgz#7b842b1defcbd7dce002622ad6be4c4b2a104253"
integrity sha512-KCUSsaVspWhy4TLfruh6Vc4GaecMtBZ2I6zTu1Pei14U/nkhQgiXanpnFGPSzfcswbNyNRxGcOJcqOnr5/BQJg==
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-eager-load/-/bookshelf-eager-load-0.1.30.tgz#3227ee336a073422f60c588daee7a338457f4a5e"
integrity sha512-bMnBc4LelXsp+cfC9ovebvWr9lV0QTkThWLKSbVi/FRUV5JiMJTGRLMNNwv0bZfH9MBalynDBpvaHv1t+G1qcw==
dependencies:
"@tryghost/debug" "^0.1.30"
"@tryghost/debug" "^0.1.31"
lodash "^4.17.21"
"@tryghost/bookshelf-include-count@^0.3.13":
version "0.3.13"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-include-count/-/bookshelf-include-count-0.3.13.tgz#0f802d52eda658bd886caebe218f993780ece0fe"
integrity sha512-kz9ziI9yMh4ccyTWkG03WFXhFlVMaA5tJMGVD1AMUQ+0dS/7TnwT1QuUVjspxIYow7LuhuMXDRafgwwOPTuBBA==
"@tryghost/bookshelf-filter@^0.5.15":
version "0.5.15"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-filter/-/bookshelf-filter-0.5.15.tgz#4979da196a8010a6262299d1927d905e3e500083"
integrity sha512-++k8dUOhccPikPDaRLFNdtiNgHLnlx5e64j/6IGTY4F8L/NeqEj/CxziR9Wxw5sV9USUMsg42fDulZXZ0dJgcg==
dependencies:
"@tryghost/debug" "^0.1.30"
"@tryghost/debug" "^0.1.31"
"@tryghost/errors" "^1.3.3"
"@tryghost/nql" "^0.12.3"
"@tryghost/tpl" "^0.1.31"
"@tryghost/bookshelf-has-posts@^0.1.31":
version "0.1.31"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-has-posts/-/bookshelf-has-posts-0.1.31.tgz#e37622cdf2cdf537afb75a907428942af1587833"
integrity sha512-QJxytGKcBdo3aa+FZeKRglm0+Zb7rwLT9D8CEvCPJ8OxbbsKlCgjTm9Z2feeF7ui+2WQk9Mi8eV7q/wozkTfrw==
dependencies:
"@tryghost/debug" "^0.1.31"
lodash "^4.17.21"
"@tryghost/bookshelf-order@^0.1.25":
version "0.1.25"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-order/-/bookshelf-order-0.1.25.tgz#0dbdf6353ad9218e8a2091a14d86fcfb697e4fb6"
integrity sha512-JdHg34oVQ9qUldmp8OgQ352VumbYIV/fQLRLy0lst4AkL6UO9MwV3aCnw74gsLA2Z44KIVhPf9oDfQbuFfmJIw==
"@tryghost/bookshelf-include-count@^0.3.14":
version "0.3.14"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-include-count/-/bookshelf-include-count-0.3.14.tgz#1fb400a8e4bb2cf1f73e06ff8749d986072b2b12"
integrity sha512-m01Phu29/HcDb1Y84dKRkjGaxDTPrSqebunJm7PBOpF4nR5Qy+w//2df/QB9XvD+rdUCwzLT+yi9E6zgfPdnng==
dependencies:
"@tryghost/debug" "^0.1.31"
lodash "^4.17.21"
"@tryghost/bookshelf-order@^0.1.26":
version "0.1.26"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-order/-/bookshelf-order-0.1.26.tgz#f45212f708e28fc9b613bdaf93891a8fe625bc9c"
integrity sha512-9R8sFqjgeD02lPDxbeweP8QY290rEgMCs7Fnqu4MX77eU4Rf8q8Tfg92Wsmfljie3JLPZYhItIR1OSqQpRd3xg==
dependencies:
lodash "^4.17.21"
"@tryghost/bookshelf-pagination@^0.1.44":
version "0.1.44"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-pagination/-/bookshelf-pagination-0.1.44.tgz#597c93c33735f74c22662d4635f9e9c6b26a81cb"
integrity sha512-Le6W5nORcwn3Mnincg7dSwAOc/+nbYwPV5wRaml0+iuh67qJJHkoWt/BZgMrps/c7/zFlAUY05Pts29gCoqXAw==
"@tryghost/bookshelf-pagination@^0.1.46":
version "0.1.46"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-pagination/-/bookshelf-pagination-0.1.46.tgz#6622dbf9afbfe8be7fa6ae13315c06a8c2866e3b"
integrity sha512-dsikUD00DyW+C6GMZiuTIHkipJbzNlx47Rb160ZAPhahuma8LoAr4GAUMxgxEjFVwjveg45v4ZGZjn7tpkMtgQ==
dependencies:
"@tryghost/errors" "^1.3.2"
"@tryghost/tpl" "^0.1.30"
"@tryghost/errors" "^1.3.3"
"@tryghost/tpl" "^0.1.31"
lodash "^4.17.21"
"@tryghost/bookshelf-plugins@0.6.17":
version "0.6.17"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-plugins/-/bookshelf-plugins-0.6.17.tgz#875b23d5d8abd24c64bba33e0660aadfddad1c7e"
integrity sha512-sstie43C0UhwN/cf55q/C3XxygEgLVCnYXhHkiTSZhL1CBqIFoNxRUgO3piKdMq8smx7C1Q0zROT/T0onNDchg==
"@tryghost/bookshelf-plugins@0.6.19":
version "0.6.19"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-plugins/-/bookshelf-plugins-0.6.19.tgz#e5a51d6973a00ba7801aad6f13b3103e59d6acc4"
integrity sha512-/9UTHm1kMy8fJpAroEPpB8eU4MFGBmBr+GAr1ejm2jfSZBgeJFDEoWNDGJNvplnadtsbogprn6L2AAKD//kMBg==
dependencies:
"@tryghost/bookshelf-collision" "^0.1.42"
"@tryghost/bookshelf-custom-query" "^0.1.25"
"@tryghost/bookshelf-eager-load" "^0.1.29"
"@tryghost/bookshelf-filter" "^0.5.14"
"@tryghost/bookshelf-has-posts" "^0.1.30"
"@tryghost/bookshelf-include-count" "^0.3.13"
"@tryghost/bookshelf-order" "^0.1.25"
"@tryghost/bookshelf-pagination" "^0.1.44"
"@tryghost/bookshelf-search" "^0.1.25"
"@tryghost/bookshelf-transaction-events" "^0.2.13"
"@tryghost/bookshelf-collision" "^0.1.43"
"@tryghost/bookshelf-custom-query" "^0.1.26"
"@tryghost/bookshelf-eager-load" "^0.1.30"
"@tryghost/bookshelf-filter" "^0.5.15"
"@tryghost/bookshelf-has-posts" "^0.1.31"
"@tryghost/bookshelf-include-count" "^0.3.14"
"@tryghost/bookshelf-order" "^0.1.26"
"@tryghost/bookshelf-pagination" "^0.1.46"
"@tryghost/bookshelf-search" "^0.1.26"
"@tryghost/bookshelf-transaction-events" "^0.2.14"
"@tryghost/bookshelf-search@^0.1.25":
version "0.1.25"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-search/-/bookshelf-search-0.1.25.tgz#9815de91a21d0d3b59b4fbf18e521b1e4b393e0a"
integrity sha512-eU39sSFQ/KCLXrEO2uPXmrNWU31vz7Hq9LdzKe38cyZVHG/ntccMNvvPqPdHbqqkW2E9KoQ67Q61CYGuK446Og==
"@tryghost/bookshelf-search@^0.1.26":
version "0.1.26"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-search/-/bookshelf-search-0.1.26.tgz#7051d52a6fe3bebd5c4a280a976ebbede0cc55df"
integrity sha512-ALO7B+Y7jb8GWl8Z5oC57lb85INGj7JBXxCIc40g1+otori6qUuWmooCY/3AymeSr4TdKg5PMmgavSB7KnVUkw==
"@tryghost/bookshelf-transaction-events@^0.2.13":
version "0.2.13"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-transaction-events/-/bookshelf-transaction-events-0.2.13.tgz#986a9a8e88bb63963a5bbc850a1be73ede8a2829"
integrity sha512-q0Ww0Bvf1t2wjdJD3J54idN07SF1gQSLPqMRvywEmP1IWbqbXdO4UvAkDQkJuWs8P/AiihXZgO5ykzJFhsq0MQ==
"@tryghost/bookshelf-transaction-events@^0.2.14":
version "0.2.14"
resolved "https://registry.yarnpkg.com/@tryghost/bookshelf-transaction-events/-/bookshelf-transaction-events-0.2.14.tgz#194d375bd7400096169e709b4f071b1296171e05"
integrity sha512-tv1DLxHszuEbERUNzJBRLKZHh3ZlIu+8alGQPLN4F4okC1biZQIiiRp9qHaTFvTpzgY4uDD5ysH4HtdRoVV7yw==
"@tryghost/bunyan-rotating-filestream@^0.0.7":
version "0.0.7"
@ -7699,6 +7699,14 @@
"@tryghost/root-utils" "^0.3.28"
debug "^4.3.1"
"@tryghost/debug@^0.1.31":
version "0.1.31"
resolved "https://registry.yarnpkg.com/@tryghost/debug/-/debug-0.1.31.tgz#242fff08b1380dc711e27134d48de2cc07042761"
integrity sha512-h2uYaAsr71oUvHHBaBWq3pY9ej/0jYnx8i4AhcTwAnEFxmPF0yEB14Hb3oxYhYKnlm5rD7u64CvoCtoM3vmyew==
dependencies:
"@tryghost/root-utils" "^0.3.29"
debug "^4.3.1"
"@tryghost/elasticsearch@^3.0.19":
version "3.0.19"
resolved "https://registry.yarnpkg.com/@tryghost/elasticsearch/-/elasticsearch-3.0.19.tgz#a0a94b667c83575a57775027aea5cb4ff4f216ef"
@ -7729,7 +7737,7 @@
focus-trap "^6.7.2"
postcss-preset-env "^7.3.1"
"@tryghost/errors@1.3.1", "@tryghost/errors@1.3.2", "@tryghost/errors@^1.2.26", "@tryghost/errors@^1.2.3", "@tryghost/errors@^1.3.2":
"@tryghost/errors@1.3.1", "@tryghost/errors@1.3.2", "@tryghost/errors@^1.2.26", "@tryghost/errors@^1.2.3", "@tryghost/errors@^1.3.2", "@tryghost/errors@^1.3.3":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@tryghost/errors/-/errors-1.3.2.tgz#3612f6f59ca07e37d1095f9eb31ff6a069a3b7f2"
integrity sha512-Aqi2vz7HHwN6p2juIYUu8vpMtaKavjULBKNnL0l1req9qXjPs90i/HV8zhvK0ceeWuPdEXaCkfHSRr/yxG3/uw==
@ -8081,6 +8089,14 @@
caller "^1.0.1"
find-root "^1.1.0"
"@tryghost/root-utils@^0.3.29":
version "0.3.29"
resolved "https://registry.yarnpkg.com/@tryghost/root-utils/-/root-utils-0.3.29.tgz#d6e941b586da8d14f3fbb82e44e20503377f8cf6"
integrity sha512-IM/Yt0iR+y4a6t7Lt5QQrgonlTXxocmAOQN9IOge4BmRlCwXL3RzTJo2jIqVxMHWlMpaK65jVL4frWQ+ROBUxw==
dependencies:
caller "^1.0.1"
find-root "^1.1.0"
"@tryghost/server@^0.1.37":
version "0.1.37"
resolved "https://registry.yarnpkg.com/@tryghost/server/-/server-0.1.37.tgz#04ee5671b19a4a5be05e361e293d47eb9c6c2482"
@ -8113,6 +8129,13 @@
dependencies:
lodash.template "^4.5.0"
"@tryghost/tpl@^0.1.31":
version "0.1.31"
resolved "https://registry.yarnpkg.com/@tryghost/tpl/-/tpl-0.1.31.tgz#3aad44ef36b6a3af4f4c9c33c320bc7ac565cfab"
integrity sha512-AJVKeGq8/5ZHCJyKJQAB26/KXXsoRmRZUY+viJ6fgTV4eMwgsWh24Dv2L3gzOCiUi1iPYNf3MZaPnZqSquyxHg==
dependencies:
lodash.template "^4.5.0"
"@tryghost/url-utils@4.4.8":
version "4.4.8"
resolved "https://registry.yarnpkg.com/@tryghost/url-utils/-/url-utils-4.4.8.tgz#fb867d8bd59a640dc67ab61c1a3d921bd12a8c2b"