How would you optimize a slow-running SQL query that aggregates millions of rows of manufacturing test data?