Walk us through how you would optimize a highly complex SQL query that is timing out on a large dataset.