|
9 | 9 | //! Uses the ste_vec_vast table (500 rows) from migration 005_install_ste_vec_vast_data.sql |
10 | 10 |
|
11 | 11 | use anyhow::Result; |
12 | | -use eql_tests::{analyze_table, assert_uses_index, create_jsonb_gin_index, get_ste_vec_encrypted}; |
| 12 | +use eql_tests::{ |
| 13 | + analyze_table, assert_uses_index, assert_uses_seq_scan, create_jsonb_gin_index, |
| 14 | + explain_query, get_ste_vec_encrypted, |
| 15 | +}; |
13 | 16 | use sqlx::PgPool; |
14 | 17 |
|
15 | 18 | // Constants for ste_vec_vast table testing |
@@ -181,3 +184,38 @@ async fn jsonb_array_count_with_index(pool: PgPool) -> Result<()> { |
181 | 184 |
|
182 | 185 | Ok(()) |
183 | 186 | } |
| 187 | + |
| 188 | + |
| 189 | +#[sqlx::test] |
| 190 | +async fn jsonb_containment_uses_seq_scan_without_index(pool: PgPool) -> Result<()> { |
| 191 | + // Test: Verify sequential scan is used BEFORE GIN index is created |
| 192 | + // |
| 193 | + // This test demonstrates that the GIN index actually makes a difference: |
| 194 | + // 1. Without index: query uses Seq Scan |
| 195 | + // 2. After creating index: query uses Index Scan |
| 196 | + // |
| 197 | + // This is a "before and after" test to prove the index improves query execution. |
| 198 | + |
| 199 | + // Run ANALYZE first to ensure planner has accurate statistics |
| 200 | + analyze_table(&pool, STE_VEC_VAST_TABLE).await?; |
| 201 | + |
| 202 | + let id = 1; |
| 203 | + let row = get_ste_vec_encrypted(&pool, STE_VEC_VAST_TABLE, id).await?; |
| 204 | + |
| 205 | + let sql = format!( |
| 206 | + "SELECT 1 FROM {} WHERE eql_v2.jsonb_array(e) @> eql_v2.jsonb_array('{}'::eql_v2_encrypted) LIMIT 1", |
| 207 | + STE_VEC_VAST_TABLE, row |
| 208 | + ); |
| 209 | + |
| 210 | + // BEFORE: Without index, should use Seq Scan |
| 211 | + let explain_before = explain_query(&pool, &sql).await?; |
| 212 | + assert_uses_seq_scan(&explain_before); |
| 213 | + |
| 214 | + // Create the GIN index |
| 215 | + setup_ste_vec_vast_gin_index(&pool).await?; |
| 216 | + |
| 217 | + // AFTER: With index, should use the GIN index |
| 218 | + assert_uses_index(&pool, &sql, STE_VEC_VAST_GIN_INDEX).await?; |
| 219 | + |
| 220 | + Ok(()) |
| 221 | +} |
0 commit comments