How would you optimize a slow-running SQL query containing multiple complex window functions and nested joins?