بهینهساز کوئری SQL

چرا این پرامپت اهمیت دارد
Query optimization requires understanding execution plans. This prompt forces the AI to think about data volume and access patterns.
ما از آن برای چه استفاده میکنیم
Database optimization
پرامپت
Analyze the following SQL query for performance issues. Provide: 1) What makes it slow, 2) An optimized version with explanation, 3) Recommended indexes, 4) Estimated improvement. Consider table sizes of 1M+ rows.
نتیجه
**Problem**: The subquery in the WHERE clause executes for every row (correlated subquery).
**Optimized Query**: ```sql SELECT o.id, o.total, u.name FROM orders o JOIN users u ON o.user_id = u.id JOIN (SELECT user_id, MAX(created_at) as latest FROM orders GROUP BY user_id) latest_o ON o.user_id = latest_o.user_id AND o.created_at = latest_o.latest WHERE o.total > 100; ```
**Recommended Index**: `CREATE INDEX idx_orders_user_created ON orders(user_id, created_at DESC);`
Query optimization requires understanding execution plans. This prompt forces the AI to think about data volume and access patterns.
This prompt demonstrates a structured approach to database optimization, producing consistent, high-quality results that can be iterated upon.