* 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,
)
| 2570 | * asked of SQLite once per statement shape. Null for any other plan. |
| 2571 | */ |
| 2572 | private substringScanOrder( |
| 2573 | memo: { index: NameSubstringIndex; plans: Map<string, ScanOrder | null> }, |
| 2574 | sql: string, |
| 2575 | params: readonly (string | number)[], |
| 2576 | patternCount: number, |
| 2577 | ): ScanOrder | null { |
| 2578 | // The patterns (the first `patternCount` parameters) can't change the |
| 2579 | // plan, since a leading `%` uses no index; the kinds and the limit can. |
| 2580 | const key = `${sql}\u0000${params.slice(patternCount).join('\u0000')}`; |
| 2581 | let order = memo.plans.get(key); |
| 2582 | if (order === undefined) { |
| 2583 | order = null; |
| 2584 | try { |
| 2585 | const details = (this.db.prepare(`EXPLAIN QUERY PLAN ${sql}`).all(...params) as Array<{ detail: string }>) |
| 2586 | .map((row) => row.detail) |
| 2587 | .filter((detail) => /^(SCAN|SEARCH) (TABLE )?nodes\b/.test(detail)); |
| 2588 | if (details.length === 1) { |
| 2589 | if (/^SEARCH (TABLE )?nodes USING (COVERING )?INDEX idx_nodes_kind \(/.test(details[0]!)) order = 'index'; |
| 2590 | else if (/^SCAN (TABLE )?nodes$/.test(details[0]!)) order = 'rowid'; |
| 2591 | } |
| 2592 | } catch { |
| 2593 | order = null; |
| 2594 | } |
| 2595 | memo.plans.set(key, order); |
| 2596 | } |
| 2597 | return order; |
| 2598 | } |
| 2599 | |
| 2600 | /** |
| 2601 | * Every node of the kinds the name-substring index holds, read in |