Commit 4fd4a2e
authored
Optimize find_query_preview_references
The optimization achieves a **14,667% speedup** primarily through **LRU caching** of expensive SQL parsing operations. Here's what changed:
**Key Optimizations:**
1. **LRU Cache for SQL Parsing**: Added `@lru_cache(maxsize=64)` to cache `sqlparse.parse()` results, which was the dominant bottleneck (97.6% of original runtime). The same SQL strings are parsed multiple times during recursive traversal of query references.
2. **Cache Table Reference Extraction**: The `extract_table_references` function now uses cached `_cached_extract_table_references` that returns immutable tuples for cache efficiency while maintaining list compatibility for callers.
3. **Eliminated Redundant Object Comparisons**: Replaced the expensive `any(id(variable) == id(ref) for ref in query_preview_references)` check with a simple dictionary key lookup (`if variable_name in query_preview_references`), reducing O(n) iterations.
4. **Minor Micro-optimizations**: Stored `token.ttype` in a local variable to reduce attribute access overhead.
**Why This Works:**
- **Repeated Parsing**: The line profiler shows `sqlparse.parse()` consuming 99.7% of `is_single_select_query` runtime and 97.6% of `extract_table_references`. Caching eliminates this redundancy.
- **Recursive Query Analysis**: When analyzing nested query references, the same SQL strings are parsed multiple times - caching provides exponential benefits.
- **Test Results Pattern**: All test cases show 25x-400x improvements, with larger improvements for complex recursive/multiple reference scenarios (up to 45,000x for large-scale tests).
**Best Performance Gains**: The optimization excels with repeated query analysis, recursive query references, and large-scale scenarios with many table references - exactly the patterns shown in the test cases where speedups range from 554% to 45,845%.1 parent 67a97b4 commit 4fd4a2e
2 files changed
+54
-35
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
67 | 69 | | |
68 | 70 | | |
69 | 71 | | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 72 | + | |
| 73 | + | |
98 | 74 | | |
99 | 75 | | |
100 | 76 | | |
| |||
140 | 116 | | |
141 | 117 | | |
142 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
143 | 122 | | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
| 123 | + | |
150 | 124 | | |
151 | 125 | | |
152 | 126 | | |
| |||
235 | 209 | | |
236 | 210 | | |
237 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
5 | | - | |
| 7 | + | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
0 commit comments