Re: Computed columns vs Dynamically calculated columns (for celko)

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



(sqlguru@xxxxxxxx) writes:
Take a large forum for an example. Is it better to use a computed
column or a dynamically calculated column for statistics like post
count, topic count etc.

You could have a separate table that hold this information, and which
you would need to keep updated through triggers or similar.

Whether this is a good idea depends. Querying the data will be faster, but
there will be an overhead to maintain it. And in either case, there is
a risk that you do not code correctly, or miss an update in some place,
that the summary table will not hold accurate information.

In the example you give, I suspect that the answer is negative. But say
that you have transactions and cash holdings for all accounts in a large
bank. The cash holdings is the sum of all transactions, but you would
recompute these everytime from the dawn of time, but have the cash holdings
in a separate table.

An alternative is to have an indexed view. This relieves you from writing
the update logic. The extra cost for the updates will still be there though.
And unless you use Entprise Edition, you would need to use the NOEXPAND
hint when you query the view to actually use it.

Also, what is the difference between a computed column vs a calculated
column? I know you can persist a computed column but would it have a
performance benefit?

There are some situations when you have to persist it. For instance if
you want that column in the clustered index.

Whether to persist a computed column solely for the performance of that
column? If the expression is very complex, maybe.



--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

.



Relevant Pages

  • Re: SQL 2005 Best Practice vs SQL 2000: Application Files Separate from data (and log) files
    ... We are physically separating log and data files on separate disks. ... the binn folder which contains the sql server executable among other things. ...
    (microsoft.public.sqlserver.setup)
  • Re: SQL installation recommendations
    ... a lot of the advantages being gained by having separate arrays/drives..... ... This will usually require two RAID adapters. ... One with two internal channels ... > Jasper Smith (SQL Server MVP) ...
    (microsoft.public.sqlserver.setup)
  • Re: Client Form
    ... confusion with two separate discussions. ... As mentioned in my other reply, let's have a look at the SQL of both the ... > Is the name of the main form 'Client'? ... Please go to the query design and from ...
    (microsoft.public.access.macros)
  • RE: Advise needed on Single user to multi-user database change
    ... the Database and you need to connect to the SQL Server using that account ... Do you plan to use the "sa" account or create separate accounts for each ... different connection to the database. ...
    (microsoft.public.sqlserver.server)
  • Re: local temp tables
    ... Just remember to add a column to keep separate ... And create a clustered index on that column, ... Links for SQL Server Books Online: ...
    (microsoft.public.sqlserver.programming)