posted 3/6/2010 by MikeMollenhour - Views: [860]
Many times we are forced to use functions in a where clause to filter results. The issue is here that rarely can SQL take advantage of an index if a function is used. SQL can perform OK in most of these scenarios, but what if you have a query used over and over again and you need it to perform as fast as possible. For the sake of this Blog let’s assume we have a query that needs to filter rows by month. In our query we would typical have something like this “where month(salesdate) =3”. So what are our alternatives? We could add a field to the table for month and calculate it as we insert but many times we don’t have control over the statements that add the data. An option other than this is something that I think is much underutilized the calculated table column. Just create a column and add the formula to the calculated column and like magic it manages itself. Now there is a cost to this on the inserts and updates but it can be minimal compared to the overhead of many searches for month against this table. This calculated column should be persisted (check box) and can also optionally be indexed at that point.