Solved

Computed Columns

Posted on 2012-04-11
10
426 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
  • 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
 
LVL 69

Expert Comment

by:ScottPletcher
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 69

Expert Comment

by:ScottPletcher
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:ScottPletcher
ID: 37842766
Then SQL will persist it for you.  You cannot index a non-persisted column.
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

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 …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now