Solved

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

Posted on 2008-10-06
4
154 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

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

717 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