Analyze a query and return optimization recommendations
(&self, query: &str)
| 49 | |
| 50 | /// Analyze a query and return optimization recommendations |
| 51 | pub fn analyze_query(&self, query: &str) -> Result<QueryOptimizationResult, PgSqliteError> { |
| 52 | if !self.enabled { |
| 53 | return Ok(QueryOptimizationResult::no_optimization()); |
| 54 | } |
| 55 | |
| 56 | let start_time = Instant::now(); |
| 57 | |
| 58 | // Update statistics |
| 59 | { |
| 60 | let mut stats = self.optimization_stats.write().unwrap(); |
| 61 | stats.total_queries += 1; |
| 62 | } |
| 63 | |
| 64 | // Pattern recognition |
| 65 | let (pattern, hints) = { |
| 66 | let mut pattern_optimizer = self.pattern_optimizer.write().unwrap(); |
| 67 | let result = pattern_optimizer.analyze_query(query); |
| 68 | |
| 69 | // Update stats |
| 70 | { |
| 71 | let mut stats = self.optimization_stats.write().unwrap(); |
| 72 | stats.pattern_recognition_hits += 1; |
| 73 | } |
| 74 | |
| 75 | result |
| 76 | }; |
| 77 | |
| 78 | // Generate optimization result |
| 79 | let result = QueryOptimizationResult { |
| 80 | pattern, |
| 81 | should_use_fast_path: hints.use_fast_path, |
| 82 | should_cache_result: hints.cache_result, |
| 83 | should_use_batch_processing: hints.use_batch_processing, |
| 84 | should_skip_translation: hints.skip_translation, |
| 85 | should_use_prepared_statement: hints.use_prepared_statement, |
| 86 | estimated_complexity: hints.complexity, |
| 87 | recommended_execution_strategy: self.recommend_execution_strategy(&hints), |
| 88 | hints, |
| 89 | }; |
| 90 | |
| 91 | // Update timing statistics |
| 92 | { |
| 93 | let mut stats = self.optimization_stats.write().unwrap(); |
| 94 | stats.total_optimization_time_ms += start_time.elapsed().as_millis() as u64; |
| 95 | } |
| 96 | |
| 97 | debug!("Query optimization analysis completed in {}ms: {:?}", |
| 98 | start_time.elapsed().as_millis(), result); |
| 99 | |
| 100 | Ok(result) |
| 101 | } |
| 102 | |
| 103 | /// Get schema for a table using lazy loading |
| 104 | pub fn get_table_schema(&self, conn: &Connection, table_name: &str) -> Result<Option<crate::cache::schema::TableSchema>, rusqlite::Error> { |