Best way to optimize query

The best way to optimize a query is to understand how the query planner is understanding your query. You may have created indexes on your table but the query planner may not be using it.