Solved

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

Posted on 2008-10-06
4
153 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
[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 Comments
 
LVL 78

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard 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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

726 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