How do you optimize a slow-running SQL query, and what role do indexes play in database performance testing?