Computed Columns

  • Comments posted to this topic are about the item Computed Columns

  • This is a nice question but I have to disagree with your answer. Actually just one part of it: "Will definitely use more resources." which I can't believe is true. I looked at your links and I didn't see anything that stated that they will always use more resources. With that statement it means that there can never be a case where they use less resources, which I can come up with an example where they would. If you had said they will use more CPU resources, maybe, but not always... In this case I think it is like Paul Randal says: "It depends!"

    * Right off the bat a non-persisted computed column uses less storage space/time than a persisted computed column.

    * If a column can be calculated from other columns in the table, but is rarely used by not actually storing it you could save the CPU of calculating it, the I/O and space of storing it, as well as the I/O of reading it each time the record it relates to is read.

    * If you have very slow storage it might be faster to have the CPU calculate it every time it is used instead of storing and reading it.

    * I'm not sure but I assume since it is calculated on use it wouldn't be stored in the buffer pool, so non-persisted computed columns probably use less RAM.

  • i agree on the '"Will definitely use more resources." being a dodgy part of the question.

    which resources are we talking about? it will most likely save developer resources as the calc is done once, probably save maintenance resources as its easy to fix.

    if the calculation is a+b will that use more resources that including a+b in a view?

  • Now this doesn't hold true:

    Will definately use more resources.

    I am very pleased that UMG developer made a thorough explanation in the previous comment as it was exactly my thought but I couldn't had written it on my phone.

    And to add some value to it I think other than being more precise with the resources option you could have thrown in an option relating to indexes or inserts (w/ or wo/ instead of triggers), language dependent functions...

    I myself have written a calendar table w/ one physical date column, one computed persisted integer column and a whole bunch of non-persisted columns calculated from the date column.

    It

    Hrvoje Piasevoli

  • The other thing that comes to mind is that is an incomplete statement. It doesn't say what it will use more resources than.

    * Using a persisted computed column instead: It depends.

    * Calculating in a view instead: probably not.

    * Calculating in an SP instead: probably not.

    * Calculating in the client app instead: probably not. (It may even use less as the client may only need the computed column so there would be less network traffic if the computed column was smaller than the components that make it up.)

    What else could it be compared to?

  • hrvoje.piasevoli (7/21/2010)


    I myself have written a calendar table w/ one physical date column, one computed persisted integer column and a whole bunch of non-persisted columns calculated from the date column.

    It

    I'm curious did you test and find that using non-persisted columns was faster than pre-calculating and storing all the permutations that you needed? (I guess it would probably depend on the storage system speed and a number of other variables.)

  • ...And off it goes posted unfinished (sorry about that).

    I think it might be a good idea to have a peer-review of ones Q and A before submitting a QoD to avoid frustration we've all whitnessed when people encounter inprecise or ambiguous answers.

    I personaly don't mind having some points not awarded, but find that questionable QoDs yield more contention in quality of the discussion that follows.

    Regards,

    Hrvoje

    Hrvoje Piasevoli

  • I'm curious did you test and find that using non-persisted columns was faster than pre-calculating and storing all the permutations that you needed? (I guess it would probably depend on the storage system speed and a number of other variables.)

    I find the aproach more manageable than using an SP and cleaner from the design point of view. And I can always make some other column persisted by a simple alter statement. (well not realy - has to be deterministic which is not the case with all columns). It is used primarily in warehouse solutions and therefore performance was never really an issue as it is loaded in an SSAS cube and persisted there. It also has a neat effect of being culture and language aware out of the box.

    Hrvoje Piasevoli

  • A good question, apart from the ambiguity surrounding the resources question. Since it's calculated at runtime, it doesn't use any disk resources, and I can't see how having a select statement along the lines of SELECT a + b AS C, <other_columns> FROM <table> would use less resources than SELECT C, <other_columns> FROM <table> when C is a computed column from a + b.

    Obviously, if you're using scalar UDF's in non-persisted computed columns, there's a pretty good chance you'll be hit by performance problems somewhere down the line - as I've just discovered on one of our systems, so perhaps that was it?

    Duncan

  • This was removed by the editor as SPAM

  • One more vote for the definitly use more ressource "issue thingie".

    Using C or a+b in the query will have no difference at all. There will be a small hit if you have a udf that does (a+b) without selecting from the base tables. But you'll get a massive hit if you query data from other tables.

    So, again, definitly is iffie at best without context.

    Other than that awesome question.

  • Sorry about the ambiguous answer.

    What I was trying to get to is...

    Retrieving data from a normal column is much faster then having to calculate the value every time a computed field is called.

    Can you imagine, if you are calling 100 000 rows including a computed column? It would definitely take more resources specially if the computed column comes from other tables via UDFs...

    I have to admit that the "Always" in the answer might have made the answer ambiguous... maybe it should have said "in most cases"

    We now use the computed columns here with caution... We only reference computed columns in procedures for which we know will never return many many rows.

  • I also would like to add.

    The QoD is a great way to learn... I've learned so much answering and reading answers.

    I would like to thank SQLServerCentral for this great service.

    I say this because many people are very fast on the trigger in blasting the questions and answers not reading what has already been written in the comments section... It isn't easy to write a question and far more difficult to please everyone... I don't mind the Instructive comments I actually like them very much as we learn a lot more there too... So I would like to thank all of your comments... they were instructive to some point.

    In my case, I don't really care about points, there's nothing more interesting than feeding the brain... 😉

    I love it!!! :w00t:

    Thanks again for all your comments and take care 🙂

  • That's one of the biggest challenges when writting a question. Everything depends in sql server when talking about performance. And if you give out too many details then the right answer(s) becomes extremely obvious.

  • How can one get it right without taking all morning...? I have other things to do too. 🙂

    Percentage that got it right when I took it was a handsome 3%. Though, I think most know what a computed column is all about.

    Take it with a grain of salt. I still respect anyone who takes the time to put a question together.

Viewing 15 posts - 1 through 15 (of 45 total)

You must be logged in to reply to this topic. Login to reply