improve queries with limit

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-07-24 00:19:09 +02:00
parent 65c464a63c
commit 8025859b6d
2 changed files with 18 additions and 0 deletions

View File

@@ -56,6 +56,11 @@ export const ch = new Proxy(originalCh, {
throw retryError; // Rethrow or handle as needed
}
} else {
if (args[0].query) {
console.log('FAILED QUERY:');
console.log(args[0].query);
}
// Handle other errors or rethrow them
throw error;
}

View File

@@ -16,6 +16,8 @@ export function getChartSql({
startDate,
endDate,
projectId,
chartType,
limit,
}: IGetChartDataInput) {
const { sb, join, getWhere, getFrom, getSelect, getOrderBy, getGroupBy } =
createSqlBuilder();
@@ -59,6 +61,17 @@ export function getChartSql({
sb.where.endDate = `created_at <= '${formatClickhouseDate(endDate)}'`;
}
if (breakdowns.length > 0 && limit) {
sb.where.bar = `(${breakdowns.map((b) => b.name).join(',')}) IN (
SELECT ${breakdowns.map((b) => b.name).join(',')}
FROM ${TABLE_NAMES.events}
${getWhere()}
GROUP BY ${breakdowns.map((b) => b.name).join(',')}
ORDER BY count(*) DESC
LIMIT ${limit}
)`;
}
breakdowns.forEach((breakdown, index) => {
const key = `label_${index}`;
const value = breakdown.name.startsWith('properties.')