473,757 Members | 5,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Views / Performance - 2 Rows to 2 Columns

gateshosting
25 New Member
Good day,

I am programming my Stored Procedures and Views for performance, since we are dealing with millions of records. I know this question is going to sound like I do NOT want to do it this way, but in this case, it may work...


I have an Invoice LineItems table which will only ever contain up to 3 items. It will NEVER contain any more than that. We only deal with 3 items/services, and it will never change. So let's call them A, B, and C.

I have two options and I need to know if number (1) is doable, and/or if it will be faster, and more efficient to do (2).

(1)
Create a view that has 4 columns: Invoice Number, Product A Price, Product B Price, and Product C Price. I want to do it this way because it will work perfectly for a Stored Procedure I wrote which is running very quickly and does exactly what I need...

(2)
Create 3 views that get Product A, Product B Price, and Product C Price, and then just join them in another view. Is this going to be too much overhead? I mean, it will be running 4 views essentially... What do you think?

Here is something I wrote hoping it would work... but no go. The result was not what was expected:


Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     [invoice number],
  3.  
  4.     CASE WHEN [Item Number] = 'A' THEN [Price]) END
  5.     AS APRICE,
  6.  
  7.     CASE WHEN [Item Number] = 'B' THEN [Price]) END
  8.     AS BPRICE
  9. FROM
  10.     [Invoice Lineitems]
  11.  
  12. GROUP BY
  13.     [invoice number]
  14.  
  15.  
Dec 6 '06 #1
4 1552
almaz
168 Recognized Expert New Member
...
(1)
Create a view that has 4 columns: Invoice Number, Product A Price, Product B Price, and Product C Price. I want to do it this way because it will work perfectly for a Stored Procedure I wrote which is running very quickly and does exactly what I need...

(2)
Create 3 views that get Product A, Product B Price, and Product C Price, and then just join them in another view. Is this going to be too much overhead? I mean, it will be running 4 views essentially... What do you think?
...
Looking at your code: you've almost done your task, just need to make a final step. You know that there will be only one record per Item Number for each invoice number, but SQL Server doesn't. So you have to specify what it should do with potentially multiple values:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     [invoice number],
  3.  
  4.     AVG(CASE WHEN [Item Number] = 'A' THEN [Price]) END)
  5.     AS APRICE,
  6.  
  7.     AVG(CASE WHEN [Item Number] = 'B' THEN [Price]) END)
  8.     AS BPRICE,
  9.  
  10.     AVG(CASE WHEN [Item Number] = 'C' THEN [Price]) END)
  11.     AS CPRICE
  12. FROM
  13.     [Invoice Lineitems]
  14.  
  15. GROUP BY
  16.     [invoice number]
Dec 6 '06 #2
gateshosting
25 New Member
Awesome... I will give it a try.

Do you think that script will perform better than joining the line items table three times to create the values? I did a script that works, but think (logically) that it will be too much overhead... but maybe not. I don't know much about specifics of performance in SQL Server.
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     i.[invoice number] AS [Invoice Number],
  3.     [i].[Paid] as [Paid],
  4.  
  5.     SUM([li1].[quantity] * [li1].[Price]) AS TransportPrice,
  6.     SUM([li2].[quantity] * [li2].[Price]) AS InopPrice,
  7.     SUM([li3].[quantity] * [li3].[Price]) AS FeeAdvancePrice
  8.  
  9. FROM
  10.     [Invoice] i LEFT OUTER JOIN
  11.     [Invoice Lineitems] li1 ON [i].[invoice number] = [li1].[invoice number] and [li1].[Item Number] = 'A' LEFT OUTER JOIN
  12.     [Invoice Lineitems] li2 ON [i].[invoice number] = [li2].[invoice number] and [li2].[Item Number] = 'B' LEFT OUTER JOIN
  13.     [Invoice Lineitems] li3 ON [i].[invoice number] = [li3].[invoice number] and [li3].[Item Number] = 'C'
  14.  
  15. GROUP BY
  16.     [i].[invoice number], [i].[Paid]
  17.  
Thanks,

Michael C. Gates
Dec 6 '06 #3
iburyak
1,017 Recognized Expert Top Contributor
Just a note:
When you do case in select statement search goes once through the table using indexes if possible.

If you have 3 separate views it goes 3 times through the same table and then you join all of them which is work on the background where each view doesn't have an index I assume.

You have to check a showplan to be sure how it works.
Dec 6 '06 #4
gateshosting
25 New Member
Just FYI for anyone reading... I ran tests:

1. Joining the same table three times, to get the 3 different products was a little slower than the script almaz gave me.
Expand|Select|Wrap|Line Numbers
  1. AVG(CASE WHEN [Item Number] = 'A' THEN [Price] END) AS A,
  2. AVG(CASE WHEN [Item Number] = 'B' THEN [Price] END) AS B,
  3. AVG(CASE WHEN [Item Number] = 'C' THEN [Price] END) AS C
  4.  
Best regards,

Michael C. Gates
Dec 7 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2426
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish, Swedish or English). There are about a dozen tables with columns that need localization. Doing this in the application level was a no-goer. It would have taken far too much time (there is a *lot* of code and unfortunately most of the...
3
2443
by: teddysnips | last post by:
This from a SQL Server manual: "Complex queries, however, such as those in decision support systems, can reference large numbers of rows in base tables and aggregate large amounts of information into relatively concise aggregates (such as sums or averages). SQL Server 2000 supports creating a clustered index on a view that implements such a complex query. When the CREATE INDEX statement is executed, the result set of the view SELECT is...
14
5419
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB 7.2 environment, the choices the optimizer makes often seem flaky. But this last example really floored me. I was hoping someone could explain why I get worse response time when the optimizer uses two indexes, than when it uses one. Some context:
8
5260
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
15
3072
by: rod.weir | last post by:
Fellow database developers, I would like to draw on your experience with views. I have a database that includes many views. Sometimes, views contains other views, and those views in turn may contain views. In fact, I have some views in my database that are a product of nested views of up to 6 levels deep! The reason we did this was. 1. Object-oriented in nature. Makes it easy to work with them.
33
6662
by: Peter | last post by:
People are telling me it is bad to put select * from <atable> in a view. I better should list all fields of the table inside the definition of the view. I dont know exactly why but some say: A select * from makes sql server does a table scan.
6
1693
by: Jim Devenish | last post by:
I have 3 views, two of which depend on the other: CREATE VIEW dbo.CustomerListQueryAccounts AS SELECT dbo.CustomerListQuery.* FROM dbo.CustomerListQuery WHERE (isProspect = 0) CREATE VIEW dbo.CustomerListQueryProspects AS
1
2595
by: sp | last post by:
Hello I have a problem with the refresh performance in datagrid – when datagrid is being shown it is so slow that I can see one by one cells is drawn -datagrid contains about 35x40 of cells - they are generated through the odswierz_frekfencje() function (listed below)-
10
4301
by: shsandeep | last post by:
The ETL application loaded around 3000 rows in 14 seconds in a Development database while it took 2 hours to load in a UAT database. UAT db is partitioned. Dev db is not partitioned. the application looks for existing rows in the table...if they already exist then it updates otherwise inserts them. The table is pretty large, around 6.5 million rows.
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.