Erik Darling takes us through the scenic route:
I’m going to use the example from yesterday’s post to show you what you can do to further optimize queries like this.
To make the code fit in the post a little better, I’m going to skip the IF branch for the Posts table and go straight to Votes. Using dynamic SQL here will get you the same behavior at stored procedures, though.
Read on for more detail and a wrap-up of Erik’s series on conditional branching logic and performance tuning.