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
Solved

Optimising Table SELECTs - whether to use a view and UDF or a computed column

Posted on 2008-10-06
4
152 Views
Last Modified: 2010-03-19
Hi,

I have a relatively complex database for use in a web application.

The tables themselves are fairly simple, with only a few columns of standard datatypes.  However, I am unsure of the most suitable way to proceed with regard to optimisation.

For example, imagine I have a table which stores prices. The table might have columns such as:

ID
UnitPrice
TaxPercentage
Discount

Rather than calculate the "actual" price in the web application, I would rather do this at the database level. In effect, I would like a column such as "TaxPrice" (i.e. UnitPrice * TaxPercentage) or "SalePrice" (UnitPrice - Discount).

As I can see, there are two ways to go.

1. Use a computed column in the table itself.
2. Use a view which has a subquery which returns the necessary value.

At the moment I am using option 2. I have views set up anyway as I am performing various JOINs. I also thought it best to move the subquery to a UDF which returns a scalar value. This has particular attraction since I can abstract out some commonly used functions.

Which is the better approach?  The table rows will be in the region of thousands of rows at most rather than tens of thousands. The selects will generally only be returning 1 - 10 rows at a time, and in the case of large result sets, these will be paged.

For a simple calculation (e.g A - B or A*B) then is the computed column more efficient?

I also have subqueries which reference other tables - obviously I cannot put these into computed columns, but I could use the UDF in the computed column. I would prefer not to do this as it strikes me as probably having high overhead.


0
Comment
Question by:gw5434
4 Comments
 
LVL 77

Accepted Solution

by:
arnold earned 75 total points
ID: 22653970
You have to balance out resource usage.  It is good to calculate and store the data in a view if that will save you from performing repetitive tasks.

However, using a view and the server to calculate the price of buying two units of A at price B for one users while tabulating the price of ten units of A at Price B for another, is a waste of resources.  A calculation of number of item times the price is not an intensive enough transaction to dedicate the SQL server's resources for it can be performed on the server when the data is retrieved.

Creating Views to simplify/speed up repetitive queries are a good use of resources.
I.e. instead of generating join queries, creating an aggregate view of those joins and then querying the view with a limiting where clause could speed up the performance.  Note however, that views often use up system resources when changes to underlying tables occur.

0
 
LVL 15

Assisted Solution

by:jorge_toriz
jorge_toriz earned 75 total points
ID: 22674720
I preffer to use a computed persisted column, I think your data won't be volatile (a lot of changes)
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL replication over high latency link 10 60
SQL Activity Monitor detail 2 28
SQL Quer 4 22
SQL Group By Question 4 20
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

829 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