Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimised SQL query for exporting members #22005

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 70 additions & 40 deletions ghost/core/core/server/services/members/exporter/query.js
Original file line number Diff line number Diff line change
@@ -31,58 +31,88 @@ module.exports = async function (options) {
*/
}

const allProducts = await models.Product.fetchAll();
const allLabels = await models.Label.fetchAll();
const allProducts = await knex('products').select('id', 'name').then(rows => rows.reduce((acc, product) => {
acc[product.id] = product.name;
return acc;
}, {})
);

let query = knex('members')
const allLabels = await knex('labels').select('id', 'name').then(rows => rows.reduce((acc, label) => {
acc[label.id] = label.name;
return acc;
}, {})
);

const members = await knex('members')
.select('id', 'email', 'name', 'note', 'status', 'created_at')
.select(knex.raw(`
(CASE WHEN EXISTS (SELECT 1 FROM members_newsletters n WHERE n.member_id = members.id)
THEN TRUE ELSE FALSE
END) as subscribed
`))
.select(knex.raw(`
(SELECT GROUP_CONCAT(product_id) FROM members_products f WHERE f.member_id = members.id) as tiers
`))
.select(knex.raw(`
(SELECT GROUP_CONCAT(label_id) FROM members_labels f WHERE f.member_id = members.id) as labels
`))
.select(knex.raw(`
(SELECT customer_id FROM members_stripe_customers f WHERE f.member_id = members.id limit 1) as stripe_customer_id
`));
.modify((query) => {
if (hasFilter) {
query.whereIn('id', ids);
}
});

if (hasFilter) {
query = query.whereIn('id', ids);
}
const tiers = await knex('members_products')
.select('member_id', knex.raw('GROUP_CONCAT(product_id) as tiers'))
.groupBy('member_id')
.modify((query) => {
if (hasFilter) {
query.whereIn('member_id', ids);
}
});

const labels = await knex('members_labels')
.select('member_id', knex.raw('GROUP_CONCAT(label_id) as labels'))
.groupBy('member_id')
.modify((query) => {
if (hasFilter) {
query.whereIn('member_id', ids);
}
});

const rows = await query;
for (const row of rows) {
const tierIds = row.tiers ? row.tiers.split(',') : [];
const tiers = tierIds.map((id) => {
const tier = allProducts.find(p => p.id === id);
const stripeCustomers = await knex('members_stripe_customers')
.select('member_id', knex.raw('MIN(customer_id) as stripe_customer_id'))
.groupBy('member_id')
.modify((query) => {
if (hasFilter) {
query.whereIn('member_id', ids);
}
});

const subscriptions = await knex('members_newsletters')
.distinct('member_id')
.modify((query) => {
if (hasFilter) {
query.whereIn('member_id', ids);
}
});

const tiersMap = new Map(tiers.map(row => [row.member_id, row.tiers]));
const labelsMap = new Map(labels.map(row => [row.member_id, row.labels]));
const stripeCustomerMap = new Map(stripeCustomers.map(row => [row.member_id, row.stripe_customer_id]));
const subscribedSet = new Set(subscriptions.map(row => row.member_id));

for (const row of members) {
const tierIds = tiersMap.get(row.id) ? tiersMap.get(row.id).split(',') : [];
const tierDetails = tierIds.map((id) => {
return {
name: tier.get('name')
name: allProducts[id]
};
});
row.tiers = tiers;
row.tiers = tierDetails;

const labelIds = row.labels ? row.labels.split(',') : [];
const labels = labelIds.map((id) => {
const label = allLabels.find(l => l.id === id);
const labelIds = labelsMap.get(row.id) ? labelsMap.get(row.id).split(',') : [];
const labelDetails = labelIds.map((id) => {
return {
name: label.get('name')
name: allLabels[id]
};
});
row.labels = labels;
}
row.labels = labelDetails;

for (const member of rows) {
// Note: we don't modify the array or change/duplicate objects
// to increase performance
member.subscribed = !!member.subscribed;
member.comped = member.status === 'comped';
member.created_at = moment(member.created_at).toISOString();
row.subscribed = subscribedSet.has(row.id);
row.comped = row.status === 'comped';
row.stripe_customer_id = stripeCustomerMap.get(row.id) || null;
row.created_at = moment(row.created_at).toISOString();
}

return rows;
return members;
};