Solved

Computed Columns

Posted on 2012-04-11
10
430 Views
Last Modified: 2012-04-14
hi experts, can yo explain me with transact sql code this

"You may be asked if non-persisted computed columns can improve performance. Unfortunately the answer to this covers material that is advanced for this point in the course but included here for trainer reference. Computed columns can help with performance, even when they are not persisted and not referenced directly. If you have a predicate like: unitprice * quantity > 2500 Sql Server cannot make any prediction on rows returned but if you create a computed column on unitprice * quantity, even if it is not persisted, SQL Server can and will create a statistic on it, which can lead to a much better plan. And this works, even when you not directly reference the computed column, as the query optimizer recognizes that it is the same expression. "
0
Comment
Question by:enrique_aeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 200 total points
ID: 37836255
SQL Server keeps statistics that are used for optimizing queries.  Computed columns add information to the statistics that may produce better execution plans in some cases.   In the example given, the WHERE clause includes an expression that can be duplicated in a non-persistent computed column.  Declaring the computed column means better statistics even if the computed column is not referenced in the query.

I would describe it as a quirky and idiomatic optimization technique that might be useful about once in a career.
0
 

Author Comment

by:enrique_aeo
ID: 37836883
can you explaine me with transact sql code
0
 
LVL 42

Accepted Solution

by:
dqmq earned 200 total points
ID: 37838934
Not sure what your looking for, but here's the best I can do.

create  table t1 (id int, price int, tax int)
create  table t2 (id int, price int, tax int, Total as price + tax )
create  table t3 (id int, price int, tax int, Total as price + tax )

select ID from t1 where price + tax > 2
select ID from t2 where price + tax > 2
select ID from t3 where price  > 2

exec sp_helpstats 't1', 'all'
exec sp_helpstats 't2', 'all'
exec sp_helpstats 't3', 'all'

Open in new window



T1
_WA_Sys_00000002_0169315C            price
_WA_Sys_00000003_0169315C            tax

T2
_WA_Sys_00000002_025D5595            price
_WA_Sys_00000003_025D5595            tax
_WA_Sys_00000004_025D5595            Total

T3
_WA_Sys_00000002_035179CE            price




Note the additional statistics produced for the computed column T2.Total even though it is not used in the query.  Note that no statistics are produced for the similar computed column T3.Total.    

That proves the assertion that statistics may be used for non-persisted computed columns when the underlying expression is referenced in the predicate.  I have no way to prove if that results in a better execution plan, but I expect the cases where it does are rare.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37839287
As always, it depends on the specific queries.

Under the right conditions, this will have a significant beneficial impact on the SQL plans generated.

In other cases, it will have virtually no impact.
0
 
LVL 42

Expert Comment

by:dqmq
ID: 37839419
As always, it depends on the specific queries

Absolutely.  And the data population.  But, in this case we are talking about non-persisted columns and to me that implies non-indexed, as well.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37839428
Non-persisted doesn't "imply" non-indexed, it guarantees it.  You can't possibly index a non-persisted column.

Not being indexed doesn't prevent statistics on it from affecting the final query plan, though.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 200 total points
ID: 37840846
Non-persisted doesn't "imply" non-indexed, it guarantees it.  You can't possibly index a non-persisted column.
Actually in some cases you can index a computed column that is not Persisted.  Here is an example:
CREATE TABLE MyTable
    (
     ID integer,
     Price integer,
     Tax integer,
     Total AS Price + Tax                -- PERSISTED
    )
go
CREATE INDEX IX_MyTable_Total ON MyTable (Total)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37842766
Then SQL will persist it for you.  You cannot index a non-persisted column.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 100 total points
ID: 37843369
Well, SQL will have to persist the column in the index itself; it may or may not have to persist the column in the original table.  But it cannot index a column that it does not store somewhere.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 200 total points
ID: 37844108
Well, SQL will have to persist the column in the index itself; it may or may not have to persist the column in the original table
Exactly.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question