Execute a SELECT query using read-only optimizations
(
&self,
primary_conn: &Connection,
query: &str,
schema_cache: &SchemaCache,
)
| 72 | |
| 73 | /// Execute a SELECT query using read-only optimizations |
| 74 | pub fn execute_read_only_query( |
| 75 | &self, |
| 76 | primary_conn: &Connection, |
| 77 | query: &str, |
| 78 | schema_cache: &SchemaCache, |
| 79 | ) -> Result<Option<DbResponse>, rusqlite::Error> { |
| 80 | let start_time = Instant::now(); |
| 81 | |
| 82 | // Update stats |
| 83 | { |
| 84 | let mut stats = self.stats.write().unwrap(); |
| 85 | stats.total_queries += 1; |
| 86 | } |
| 87 | |
| 88 | // Check if this query can use read-only optimization |
| 89 | if !self.can_use_read_only_optimization(query) { |
| 90 | return Ok(None); |
| 91 | } |
| 92 | |
| 93 | // Generate cache key |
| 94 | let cache_key = self.generate_cache_key(query); |
| 95 | |
| 96 | // Check cache for query plan |
| 97 | if let Some(mut plan) = self.get_cached_plan(&cache_key) { |
| 98 | // Cache hit - execute with cached plan |
| 99 | debug!("Read-only cache hit for query: {}", query); |
| 100 | |
| 101 | // Update access stats |
| 102 | plan.access_count += 1; |
| 103 | plan.last_used = Instant::now(); |
| 104 | self.update_cached_plan(cache_key, plan.clone()); |
| 105 | |
| 106 | // Update stats |
| 107 | { |
| 108 | let mut stats = self.stats.write().unwrap(); |
| 109 | stats.cache_hits += 1; |
| 110 | } |
| 111 | |
| 112 | // Execute using cached plan |
| 113 | return self.execute_with_cached_plan(primary_conn, &plan, query); |
| 114 | } |
| 115 | |
| 116 | // Cache miss - analyze and create new plan |
| 117 | debug!("Read-only cache miss, analyzing query: {}", query); |
| 118 | |
| 119 | if let Some(plan) = self.analyze_and_create_plan(query, schema_cache, primary_conn)? { |
| 120 | // Cache the new plan |
| 121 | self.cache_query_plan(cache_key, plan.clone()); |
| 122 | |
| 123 | // Update stats |
| 124 | { |
| 125 | let mut stats = self.stats.write().unwrap(); |
| 126 | stats.cache_misses += 1; |
| 127 | } |
| 128 | |
| 129 | // Execute with new plan |
| 130 | let result = self.execute_with_cached_plan(primary_conn, &plan, query); |
| 131 |