Computed column with/with out PERSISTED

  • Hi All,

    While reading about Computed column if found that if we mentioned PERSISTED Key word while creating/altering table then we can create index. But i'm able to create index with out that key word also.

    By marking a computed column as PERSISTED, you can create an index on a computed column that is deterministic but not precise.

    What i was thinking on difference between Computed column/Persisted computed column as if we create index on computed column then we can say that computed column as persisted column.

    What is the need of computed column if we can do the same thing in the query/application?

    Can anybody give me such scenario where we must use this Computed column only?

    🙂

  • SQL* (1/23/2013)


    What is the need of computed column if we can do the same thing in the query/application?Can anybody give me such scenario where we must use this Computed column only?

    Lets take example of TAX table where

    following are the column

    Basic_salary

    HRA_Exemption

    SAving_80C

    saving_80CCC

    calulated_tax (computed column)

    So whenever "Basic_salary ,HRA_Exemption,SAving_80C,saving_80CCC" gets populated the formula attached with caluated_tax column , will populated this column. NO application or query intervention required

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • 🙂

    I got the importance of the Computed column, but what about other questions? PERSISTED/Creating Index on Calculated column.

    🙂

  • SQL* (1/23/2013)


    Hi All,

    While reading about Computed column if found that if we mentioned PERSISTED Key word while creating/altering table then we can create index. But i'm able to create index with out that key word also.

    By marking a computed column as PERSISTED, you can create an index on a computed column that is deterministic but not precise.

    There's no need to make the column persisted prior to indexing it.

    I'm assuming you're referring to the text here. It reads like it's been copied out of context from somewhere else and the real meaning has been lost as a result.

    I suspect it's really referring to computed columns that are deterministic, but imprecise (usually based on CLR functions), but I can see why you're confused.

    This documentation has a more complete explanation of the imprecise computed column requirements :

    http://msdn.microsoft.com/en-us/library/ms189292.aspx#BKMK_persisted

    You can create an index on a computed column that is defined with a deterministic, but imprecise, expression if the column is marked PERSISTED in the CREATE TABLE or ALTER TABLE statement. This means that the Database Engine stores the computed values in the table, and updates them when any other columns on which the computed column depends are updated. The Database Engine uses these persisted values when it creates an index on the column, and when the index is referenced in a query. This option enables you to create an index on a computed column when Database Engine cannot prove with accuracy whether a function that returns computed column expressions, particularly a CLR function that is created in the .NET Framework, is both deterministic and precise.

  • HowardW (1/24/2013)


    SQL* (1/23/2013)


    Hi All,

    While reading about Computed column if found that if we mentioned PERSISTED Key word while creating/altering table then we can create index. But i'm able to create index with out that key word also.

    By marking a computed column as PERSISTED, you can create an index on a computed column that is deterministic but not precise.

    There's no need to make the column persisted prior to indexing it.

    That's not necessarily always true. You might persist a column if its requires significant overhead to generate the value.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who give you the shirt off *someone else's* back.

  • ScottPletcher (1/24/2013)


    HowardW (1/24/2013)


    There's no need to make the column persisted prior to indexing it.

    That's not necessarily always true. You might persist a column if its requires significant overhead to generate the value.

    I think you've misunderstood my comment. You need to read the whole sentence, including the bit I've highlighted. Whether it's persisted via an index or in the column definition, it's still only calculated on modification.

  • HowardW (1/24/2013)


    ScottPletcher (1/24/2013)


    HowardW (1/24/2013)


    There's no need to make the column persisted prior to indexing it.

    That's not necessarily always true. You might persist a column if its requires significant overhead to generate the value.

    I think you've misunderstood my comment. You need to read the whole sentence, including the bit I've highlighted. Whether it's persisted via an index or in the column definition, it's still only calculated on modification.

    I don't think you're thinking clearly about this. If it's not persisted, it has to be computed every time it's SELECTed or otherwise referenced. If that computation is a lot of overhead, it can be worthwhile to instead persist the computed value, especially if it's referenced frequently.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who give you the shirt off *someone else's* back.

  • ScottPletcher (1/24/2013)


    HowardW (1/24/2013)


    ScottPletcher (1/24/2013)


    HowardW (1/24/2013)


    There's no need to make the column persisted prior to indexing it.

    That's not necessarily always true. You might persist a column if its requires significant overhead to generate the value.

    I think you've misunderstood my comment. You need to read the whole sentence, including the bit I've highlighted. Whether it's persisted via an index or in the column definition, it's still only calculated on modification.

    I don't think you're thinking clearly about this. If it's not persisted, it has to be computed every time it's SELECTed or otherwise referenced. If that computation is a lot of overhead, it can be worthwhile to instead persist the computed value, especially if it's referenced frequently.

    Nope, I think you're not thinking clearly. How an earth would an index be able to be used if the key column was calculated on the fly during a select? Indexing a computed column persists it regardless of whether the column is marked as persisted in its definition.

  • HowardW (1/24/2013)


    ScottPletcher (1/24/2013)


    HowardW (1/24/2013)


    ScottPletcher (1/24/2013)


    HowardW (1/24/2013)


    There's no need to make the column persisted prior to indexing it.

    That's not necessarily always true. You might persist a column if its requires significant overhead to generate the value.

    I think you've misunderstood my comment. You need to read the whole sentence, including the bit I've highlighted. Whether it's persisted via an index or in the column definition, it's still only calculated on modification.

    I don't think you're thinking clearly about this. If it's not persisted, it has to be computed every time it's SELECTed or otherwise referenced. If that computation is a lot of overhead, it can be worthwhile to instead persist the computed value, especially if it's referenced frequently.

    Nope, I think you're not thinking clearly. How an earth would an index be able to be used if the key column was calculated on the fly during a select? Indexing a computed column persists it regardless of whether the column is marked as persisted in its definition.

    Of course indexing requires persisting. I thought everyone was clear on that.

    But you claimed, in your first quote above, there was no (never a) need to persist except to index. That's simply not true. Even if a computed column is not indexed in any way, there are times when it's still worthwhile to persist it to avoid re-computing it.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who give you the shirt off *someone else's* back.

  • ScottPletcher (1/24/2013)


    Of course indexing requires persisting. I thought everyone was clear on that.

    But you claimed, in your first quote above, there was no (never a) need to persist except to index. That's simply not true. Even if a computed column is not indexed in any way, there are times when it's still worthwhile to persist it to avoid re-computing it.

    Right, OK, I see where the confusion is now, even if I don't know where you think I said there's never a need to persist a computed column, except to index it. I certainly didn't claim that, hence why I pointed out that you misunderstood my comment. If that was what I wanted to say, I would have said:

    "There's no need to make the column persisted except to index it"

    But what I said (and highlighted again after your comment) was:

    There's no need to make the column persisted prior to indexing it.

    As in, there's no requirement to persist the column before indexing it.

  • i thought it was clear: i read it as howard saying there is no need to mark an calculated column as persisted in order to index it...it's not a requirement.

    simple example:

    --drop table products

    CREATE TABLE Products(

    id INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,

    pname VARCHAR(30),

    CostPerItem MONEY,

    ItemsPerCase INT,

    ValuePerCase AS CostPerItem * ItemsPerCase

    )

    INSERT INTO Products

    SELECT 'Bananas',.49,40 UNION ALL

    SELECT 'Apples',.49,36 UNION ALL

    SELECT 'Pineapples',3.49,5

    SELECT * FROM Products where ValuePerCase > 18

    CREATE INDEX IX_Product_ValuePerCase ON Products(ValuePerCase)

    SELECT * FROM Products where ValuePerCase > 18

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (1/24/2013)


    i thought it was clear: i read it as howard saying there is no need to mark an calculated column as persisted in order to index it...it's not a requirement.

    simple example:

    --drop table products

    CREATE TABLE Products(

    id INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,

    pname VARCHAR(30),

    CostPerItem MONEY,

    ItemsPerCase INT,

    ValuePerCase AS CostPerItem * ItemsPerCase

    )

    INSERT INTO Products

    SELECT 'Bananas',.49,40 UNION ALL

    SELECT 'Apples',.49,36 UNION ALL

    SELECT 'Pineapples',3.49,5

    SELECT * FROM Products where ValuePerCase > 18

    CREATE INDEX IX_Product_ValuePerCase ON Products(ValuePerCase)

    SELECT * FROM Products where ValuePerCase > 18

    Seems an odd way to read it in context here, since the OP stated in his original post that he could index a computed column w/o explicitly persisting it. I guess I just didn't understand why that needed to be restated separately.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who give you the shirt off *someone else's* back.

  • SQL* (1/23/2013)

    What is the need of computed column if we can do the same thing in the query/application?

    The two big advantages of computed columns are:

    1) a consistent definition;

    2) defined in only one place.

    1) If you define a computed column in the table, you know that all queries are using the same computation to generate the new value. If you specify the computation itself in different queries and views, some of them could be different from each other, possibly even to the extent of making the value incorrect.

    2) Say you need to change/update the computation. With a computed column, you simply drop the old definition and add the new one -- all queries automatically now refer to the new definition. If you've done the computation in multiple queries/views, you must find them all and change them -- NOT fun.

    For example:

    ALTER TABLE dbo.tablename ADD

    full_name AS

    first_name +

    ISNULL(' ' + middle_name, '') +

    last_name

    --requirement comes in to add a possible "salutation" to full_name

    ALTER TABLE dbo.tablename

    DROP COLUMN full_name

    ALTER TABLE dbo.tablename ADD

    full_name AS

    ISNULL(salutation + ' ', '') +

    first_name +

    ISNULL(' ' + middle_name, '') +

    last_name

    Voila: every use of full_name from then automatically includes the required "salutation" with no other coding changes required.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who give you the shirt off *someone else's* back.

  • ScottPletcher (1/24/2013)


    Seems an odd way to read it in context here, since the OP stated in his original post that he could index a computed column w/o explicitly persisting it. I guess I just didn't understand why that needed to be restated separately.

    I was addressing the statement the OP quoted from the MS documentation that, out of context, suggested that you were required to mark a column as persisted before indexing it.

  • HowardW (1/25/2013)


    ScottPletcher (1/24/2013)


    Seems an odd way to read it in context here, since the OP stated in his original post that he could index a computed column w/o explicitly persisting it. I guess I just didn't understand why that needed to be restated separately.

    I was addressing the statement the OP quoted from the MS documentation that, out of context, suggested that you were required to mark a column as persisted before indexing it.

    Is there any specific technical reason for this suggestion. As we are able to create index without making the computed column as PERSIST. Only the difference is the execution plan for the both is different, there is extra computed column icon in execution plan for without PERSIST case.

    🙂

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

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