ColtenOuO opened a new pull request, #66222:
URL: https://github.com/apache/airflow/pull/66222

   ## Summary
   
   Eliminates an N+1 query in the bulk pool delete endpoint by reusing the ORM 
objects already loaded by 
[`BulkPoolService.categorize_pools()`](https://github.com/apache/airflow/blob/main/airflow-core/src/airflow/api_fastapi/core_api/services/public/pools.py#L116)
 instead of re-fetching each pool one at a time inside the delete loop.
   
   ## Why
   
   
[`categorize_pools()`](https://github.com/apache/airflow/blob/main/airflow-core/src/airflow/api_fastapi/core_api/services/public/pools.py#L116)
 already loads every requested pool with a single batched `SELECT ... WHERE 
pool IN (...)` and returns them as `existing_pools_dict: dict[str, Pool]`. 
[`handle_bulk_delete()`](https://github.com/apache/airflow/blob/main/airflow-core/src/airflow/api_fastapi/core_api/services/public/pools.py#L188)
 was discarding that dict (`_, matched, not_found = ...`) and then re-querying 
each pool individually inside the loop:
   
   ```python
   for pool_name in delete_pool_names:
       existing_pool = self.session.scalar(select(Pool).where(Pool.pool == 
pool_name).limit(1))
       ...
   ```
   
   For a request that deletes N pools, this produced 2N + 1 round-trips to the 
database (1 batched SELECT in categorize_pools + N redundant per-pool SELECTs + 
N DELETEs). The redundant per-pool SELECTs return objects we already have in 
memory.
   
   ## Change
   
   Behavior is preserved exactly: `existing_pools_dict.get(pool_name)` returns 
the same `Pool` instance the per-pool SELECT would have returned (it's the same 
object loaded by `categorize_pools` in the same session), and is `None` when 
the pool doesn't exist — matching the original `session.scalar(...).limit(1)` 
semantics.
   
   ## Impact
   
   | Bulk size | Before (queries) | After (queries) | Saved |
   | :--- | :--- | :--- | :--- |
   | 10 pools | 21 | 11 | -48% |
   | 100 pools | 201 | 101 | -50% |
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to