Build SQLAlchemy queries that are both efficient and deterministic to prevent unpredictable results and improve performance. When writing database queries:
Build SQLAlchemy queries that are both efficient and deterministic to prevent unpredictable results and improve performance. When writing database queries:
query.where((DagFavorite.dag_id == DagModel.dag_id) & (DagFavorite.user_id == self.user_id))
query.where(and_(DagFavorite.dag_id == DagModel.dag_id, DagFavorite.user_id == self.user_id))
2. **Build queries with explicit control flow** for complex conditions:
```python
# Instead of adding a complex boolean expression:
query = ...
since_running = Log.dttm > last_running_time if last_running_time else True
query = query.where(Log.event == EVENT, since_running)
# Prefer explicit conditional logic:
query = query.where(Log.event == EVENT)
if last_running_time:
query = query.where(Log.dttm > last_running_time)
query.order_by(AssetEvent.timestamp.asc())
query.order_by(AssetEvent.timestamp.asc(), AssetEvent.id.asc()) ```
Enter the URL of a public GitHub repository