* The order SQLite's plan for a substring lookup reads candidate rows in, * which is the order its ORDER BY leaves ties of equal length in: the kind * index's (kind, file_path, start_line, id), or the table's (rowid). Which * one depends on the statistics — a small index is scanned whole —
(
memo: { index: NameSubstringIndex; plans: Map<string, ScanOrder | null> },
sql: string,
params: readonly (string | number)[],
patternCount: number,
)
| 2586 | * asked of SQLite once per statement shape. Null for any other plan. |
| 2587 | */ |
| 2588 | private substringScanOrder( |
| 2589 | memo: { index: NameSubstringIndex; plans: Map<string, ScanOrder | null> }, |
| 2590 | sql: string, |
| 2591 | params: readonly (string | number)[], |
| 2592 | patternCount: number, |
| 2593 | ): ScanOrder | null { |
| 2594 | // The patterns (the first `patternCount` parameters) can't change the |
| 2595 | // plan, since a leading `%` uses no index; the kinds and the limit can. |
| 2596 | const key = `${sql}\u0000${params.slice(patternCount).join('\u0000')}`; |
| 2597 | let order = memo.plans.get(key); |
| 2598 | if (order === undefined) { |
| 2599 | order = null; |
| 2600 | try { |
| 2601 | const details = (this.db.prepare(`EXPLAIN QUERY PLAN ${sql}`).all(...params) as Array<{ detail: string }>) |
| 2602 | .map((row) => row.detail) |
| 2603 | .filter((detail) => /^(SCAN|SEARCH) (TABLE )?nodes\b/.test(detail)); |
| 2604 | if (details.length === 1) { |
| 2605 | if (/^SEARCH (TABLE )?nodes USING (COVERING )?INDEX idx_nodes_kind \(/.test(details[0]!)) order = 'index'; |
| 2606 | else if (/^SCAN (TABLE )?nodes$/.test(details[0]!)) order = 'rowid'; |
| 2607 | } |
| 2608 | } catch { |
| 2609 | order = null; |
| 2610 | } |
| 2611 | memo.plans.set(key, order); |
| 2612 | } |
| 2613 | return order; |
| 2614 | } |
| 2615 | |
| 2616 | /** |
| 2617 | * Every node of the kinds the name-substring index holds, read in |