Execute a query with optimized statement caching
(
&self,
conn: &Connection,
query: &str,
params: P,
)
| 26 | |
| 27 | /// Execute a query with optimized statement caching |
| 28 | pub fn execute_with_optimization<P: rusqlite::Params>( |
| 29 | &self, |
| 30 | conn: &Connection, |
| 31 | query: &str, |
| 32 | params: P, |
| 33 | ) -> Result<usize, rusqlite::Error> { |
| 34 | if !self.enabled { |
| 35 | return conn.execute(query, params); |
| 36 | } |
| 37 | |
| 38 | // Analyze query for optimization opportunities |
| 39 | let optimization_result = self.optimization_manager |
| 40 | .analyze_query(query) |
| 41 | .map_err(|e| rusqlite::Error::SqliteFailure( |
| 42 | rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_MISUSE), |
| 43 | Some(format!("Query optimization failed: {e}")) |
| 44 | ))?; |
| 45 | |
| 46 | // Use enhanced statement pool for intelligent caching |
| 47 | if self.should_use_statement_cache(&optimization_result.pattern, &optimization_result.hints) && |
| 48 | self.supports_binary_protocol(query) { |
| 49 | debug!("Using enhanced statement cache for query pattern: {:?}", optimization_result.pattern); |
| 50 | let (mut stmt, _metadata) = self.statement_pool.prepare_and_cache_enhanced(conn, query)?; |
| 51 | let result = stmt.execute(params)?; |
| 52 | |
| 53 | // Log performance information if this is a significant query |
| 54 | if matches!(optimization_result.pattern, |
| 55 | QueryPattern::BatchInsert | |
| 56 | QueryPattern::JoinWithWhere | |
| 57 | QueryPattern::GroupByAggregation |
| 58 | ) { |
| 59 | let cache_info = self.statement_pool.get_cache_info(); |
| 60 | debug!("Statement cache stats - Size: {}/{}, Hit rate: {:.2}%", |
| 61 | cache_info.0, cache_info.1, cache_info.2 * 100.0); |
| 62 | } |
| 63 | |
| 64 | Ok(result) |
| 65 | } else { |
| 66 | // Execute without caching for queries that don't benefit |
| 67 | debug!("Executing without statement cache: {:?}", optimization_result.pattern); |
| 68 | conn.execute(query, params) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// Query with optimized statement caching |
| 73 | pub fn query_with_optimization<P: rusqlite::Params>( |
nothing calls this directly
no test coverage detected