How to Optimize a Slow Query
Analyze the slow execution plan with EXPLAIN
Use the database’s EXPLAIN or EXPLAIN ANALYZE (PostgreSQL), EXPLAIN PLAN (Oracle), or SHOW PLAN (SQL Server) to see how the database executes the query...