Explain how you would optimize a slow-running SQL query that involves multiple joins over tables with millions of rows.