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: -
SELECT
-
[invoice number],
-
-
CASE WHEN [Item Number] = 'A' THEN [Price]) END
-
AS APRICE,
-
-
CASE WHEN [Item Number] = 'B' THEN [Price]) END
-
AS BPRICE
-
FROM
-
[Invoice Lineitems]
-
-
GROUP BY
-
[invoice number]
-
-
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: - SELECT
-
[invoice number],
-
-
AVG(CASE WHEN [Item Number] = 'A' THEN [Price]) END)
-
AS APRICE,
-
-
AVG(CASE WHEN [Item Number] = 'B' THEN [Price]) END)
-
AS BPRICE,
-
-
AVG(CASE WHEN [Item Number] = 'C' THEN [Price]) END)
-
AS CPRICE
-
FROM
-
[Invoice Lineitems]
-
-
GROUP BY
-
[invoice number]
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. -
SELECT
-
i.[invoice number] AS [Invoice Number],
-
[i].[Paid] as [Paid],
-
-
SUM([li1].[quantity] * [li1].[Price]) AS TransportPrice,
-
SUM([li2].[quantity] * [li2].[Price]) AS InopPrice,
-
SUM([li3].[quantity] * [li3].[Price]) AS FeeAdvancePrice
-
-
FROM
-
[Invoice] i LEFT OUTER JOIN
-
[Invoice Lineitems] li1 ON [i].[invoice number] = [li1].[invoice number] and [li1].[Item Number] = 'A' LEFT OUTER JOIN
-
[Invoice Lineitems] li2 ON [i].[invoice number] = [li2].[invoice number] and [li2].[Item Number] = 'B' LEFT OUTER JOIN
-
[Invoice Lineitems] li3 ON [i].[invoice number] = [li3].[invoice number] and [li3].[Item Number] = 'C'
-
-
GROUP BY
-
[i].[invoice number], [i].[Paid]
-
Thanks,
Michael C. Gates
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.
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. -
AVG(CASE WHEN [Item Number] = 'A' THEN [Price] END) AS A,
-
AVG(CASE WHEN [Item Number] = 'B' THEN [Price] END) AS B,
-
AVG(CASE WHEN [Item Number] = 'C' THEN [Price] END) AS C
-
Best regards,
Michael C. Gates
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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:
|
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,...
|
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.
| |
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.
|
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
|
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)-
|
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.
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |