Skip to content

Commit 2efa2bb

Browse files
authored
Merge pull request #5132 from cardstack/search-timing-sql-split
Split realm-server search-timing sql stage into compile vs sqlExec
2 parents c8632e0 + e0a71b2 commit 2efa2bb

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

packages/runtime-common/index-query-engine.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
import { isScopedCSSRequest } from './scoped-css';
7474
import type { FileMetaResource } from './resource-types';
7575
import type { VirtualNetwork } from './virtual-network';
76+
import type { RequestTimings } from './request-timings';
7677

7778
export interface IndexedFile {
7879
type: 'file';
@@ -139,7 +140,8 @@ interface InstanceError extends Partial<
139140
export type InstanceOrError = IndexedInstance | InstanceError;
140141

141142
type GetEntryOptions = WIPOptions;
142-
export type QueryOptions = WIPOptions & PrerenderedCardOptions;
143+
export type QueryOptions = WIPOptions &
144+
PrerenderedCardOptions & { timings?: RequestTimings };
143145

144146
interface PrerenderedCardOptions {
145147
htmlFormat?: PrerenderedHtmlFormat;
@@ -197,8 +199,20 @@ export class IndexQueryEngine {
197199
return await query(this.#dbAdapter, expression, coerceTypes);
198200
}
199201

200-
async #queryCards(query: CardExpression) {
201-
return this.#query(await this.makeExpression(query));
202+
// Split the two phases so the search-timing line can attribute the SQL
203+
// stage. `makeExpression` resolves the filter tree to SQL — that resolution
204+
// runs a `getDefinition` card-definition lookup per type/field (a cache
205+
// read, or a module prerender on a miss), so it can dominate a search whose
206+
// actual row fetch is tiny. `#query` is the DB round-trip itself. The data
207+
// and count queries run concurrently, so these accumulate into the
208+
// parallel-sum `busy` bucket rather than the wall-clock stages.
209+
async #queryCards(query: CardExpression, timings?: RequestTimings) {
210+
let expression = timings
211+
? await timings.busyTime('compile', () => this.makeExpression(query))
212+
: await this.makeExpression(query);
213+
return timings
214+
? await timings.busyTime('sqlExec', () => this.#query(expression))
215+
: this.#query(expression);
202216
}
203217

204218
async getInstance(
@@ -608,8 +622,8 @@ export class IndexQueryEngine {
608622
];
609623

610624
let [results, totalResults] = await Promise.all([
611-
this.#queryCards(query),
612-
this.#queryCards(queryCount),
625+
this.#queryCards(query, opts.timings),
626+
this.#queryCards(queryCount, opts.timings),
613627
]);
614628

615629
return {

0 commit comments

Comments
 (0)