* 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,
)
| 2852 | * asked of SQLite once per statement shape. Null for any other plan. |
| 2853 | */ |
| 2854 | private substringScanOrder( |
| 2855 | memo: { index: NameSubstringIndex; plans: Map<string, ScanOrder | null> }, |
| 2856 | sql: string, |
| 2857 | params: readonly (string | number)[], |
| 2858 | patternCount: number, |
| 2859 | ): ScanOrder | null { |
| 2860 | // The patterns (the first `patternCount` parameters) can't change the |
| 2861 | // plan, since a leading `%` uses no index; the kinds and the limit can. |
| 2862 | const key = `${sql}\u0000${params.slice(patternCount).join('\u0000')}`; |
| 2863 | let order = memo.plans.get(key); |
| 2864 | if (order === undefined) { |
| 2865 | order = null; |
| 2866 | try { |
| 2867 | const details = (this.db.prepare(`EXPLAIN QUERY PLAN ${sql}`).all(...params) as Array<{ detail: string }>) |
| 2868 | .map((row) => row.detail) |
| 2869 | .filter((detail) => /^(SCAN|SEARCH) (TABLE )?nodes\b/.test(detail)); |
| 2870 | if (details.length === 1) { |
| 2871 | if (/^SEARCH (TABLE )?nodes USING (COVERING )?INDEX idx_nodes_kind \(/.test(details[0]!)) order = 'index'; |
| 2872 | else if (/^SCAN (TABLE )?nodes$/.test(details[0]!)) order = 'rowid'; |
| 2873 | } |
| 2874 | } catch { |
| 2875 | order = null; |
| 2876 | } |
| 2877 | memo.plans.set(key, order); |
| 2878 | } |
| 2879 | return order; |
| 2880 | } |
| 2881 | |
| 2882 | /** |
| 2883 | * Every node of the kinds the name-substring index holds, read in |