Premature Optimization and LINQ to SQL
Don’t count your chickens before they hatch. Let’s call that adage the “count-no” principle. I promise that will be funny later.
The common interpretation of this adage is that you should not rely on future results when making decisions in the present. It is generally an admonishment to not be too cocky about the future. What might the corollary be? Perhaps to concern ourselves with the details of the present moment rather than thinking about the future. We’ll call this corollary “nocount”. One specific example of this corollary is the programmer’s maxim about premature optimization. In fact, it was Donald Knuth who said, “"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil,” in a response to Dijkstra’s “Go to statement considered harmful”.
So, I’m debugging a particularly pernicious problem today. In production (and only production) we were getting a ChangeConflictException when updating a row in the database. Unfortunately, the logs indicated that there were no MemberChangeConflict objects. In other words we weren’t violating optimistic concurrency. So, I profiled the SQL statements and it appeared to be just what I expected, a simple update supporting optimistic concurrency.
Did you know you can eek out an exceedingly miniscule amount of performance by using the “SET NOCOUNT ON” option with SQL Server? Did you know that you can actually set this option at the server level? Do you know what happens when your LINQ to SQL DataContext uses the row count returned from your optimistically concurrent update statement to determine if one and only one row was updated?
Yes dear reader, premature optimization can cause the most insidious and difficult to discover problems. Please, don’t ever violate the “nocount princple”!