473,811 Members | 3,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tuning the sort step of execution plan

hi
i got a query that takes about 14 mins
here it is
select BDProduct.Produ ctCode,BDProduc t.ProductName,S ALTerritory.Ter ritoryID
,SALTerritory.T erritoryName,SA LAccount.Accoun tID,SALAccount. AccountName
,sum(SalesNetFa ct.Qty2) as Quantity
,sum(SalesNetFa ct.bonus) as Bonus
from SalesNetFact
inner join BDProduct
on BDProduct.Produ ctID=SalesNetFa ct.ProductID
inner join SALAccount
on SALAccount.Acco untID=SalesNetF act.AccountID
and SALAccount.Bran chID=SalesNetFa ct.branchid
inner join SALTerritory
on dbo.SALAccount. TerritoryID = dbo.SALTerritor y.TerritoryID
and dbo.SALAccount. BranchID = dbo.SALTerritor y.BranchID
group by BDProduct.Produ ctCode,BDProduc t.ProductName
,SALTerritory.T erritoryID,SALT erritory.Territ oryName,SALAcco unt.AccountID
,SALAccount.Acc ountName
the SalesNetFact table has BranchID,Transa ctionLineID as primary key
the BDProduct table has ProductID as primary key
the SALAccount table has AccountID,Branc hID as primary key
the SALTerritory table has TerritoryID,Bra nchID as primary key
i have no other indices in any of these tables
the execution plan shows that the sort step takes 96% cost,that is the
most expensive step,it is done after all the joining steps and before
the group by step
for the sort step:the estimated row count is 1552242,the arguments
are:ORDER BY [BDProduct].[ProductCode]
asc,[SALTerritory].[TerritoryID] asc,[SALTerritory].[TerritoryName]
asc,[SalesNetFact].[AccountID] asc,[SALAccount].[AccountID] asc)
any ideas about how to improve this sort step
Jul 20 '05 #1
1 8471
[posted and mailed, please reply in news]

web developer (o_*****@yahoo. com) writes:
i got a query that takes about 14 mins
here it is
...
the execution plan shows that the sort step takes 96% cost,that is the
most expensive step,it is done after all the joining steps and before
the group by step
for the sort step:the estimated row count is 1552242,the arguments
are:ORDER BY [BDProduct].[ProductCode]
asc,[SALTerritory].[TerritoryID] asc,[SALTerritory].[TerritoryName]
asc,[SalesNetFact].[AccountID] asc,[SALAccount].[AccountID] asc)
any ideas about how to improve this sort step


First of all, I would point out information about the estimates of the
various steps are just that: estimates. They may be right, but personally,
I pay little attention to them.

Next, I like to point out that it may be difficult enough even you know
the schema and indexes and the data distribution. In this case, I only
know the primary keys, but I don't know they relate.

Nevertheless, here is a suggestion that *might* perform better in the case
the ORDER BY for the grouping indeed is the problem. But since know how
data maps, I don't know whether this query gives the same result. So please
accept my suggestion as a shot in the dark.

As reference: here is you original query reformated according to my
preferences:

SELECT bd.ProductCode, bd.ProductName, st.TerritoryID, st.TerritoryNam e,
sa.AccountID, sa.AccountName,
sum(snf.Qty2) as Quantity, sum(snf.bonus) as Bonus
FROM SalesNetFact snf
JOIN BDProduct bd ON bd.ProductID = snf.ProductID
JOIN SALAccount sa ON sa.AccountID = snf.AccountID
AND sa.BranchID = snf.branchid
JOIN SALTerritory ON sa.TerritoryID = st.TerritoryID
AND sa.BranchID = st.BranchID
GROUP BY bd.ProductCode, bd.ProductName, st.TerritoryID,
st.TerritoryNam e, sa.AccountID, sa.AccountName

By the use of aliases, there is a considerable noice reduction in my
opinion. And here is a rewrite:

SELECT bd.ProductCode, bd.ProductName, st.TerritoryID, st.TerritoryNam e,
sa.AccountID, sa.AccountName, snf.Quantity, snf.Bonus
FROM (SELECT ProductId, AccountID, BranchID,
SUM(snf.Qty2) AS Quantity, SUM(snf.bonus) AS Bonus
FROM SalesNetFact
GROUP BY ProductId, AccountID, BranchID) AS snf
JOIN BDProduct bd ON bd.ProductID = snf.ProductID
JOIN SALAccount sa ON sa.AccountID = snf.AccountID
AND sa.BranchID = snf.BranchID
JOIN SALTerritory ON sa.TerritoryID = st.TerritoryID
AND sa.BranchID = st.BranchID

What I've done is that I have used derived table, which is a sort of
temp table in the middle of the query. Logically, that is. It is never
materialized, and SQL Server may change the computation order, as long
as the result is the same. The idea is that the grouping is now performed
only on the SalesNetFact table, and first after this the other tables
are added. But if different snf.ProductId:s can map to the same tuple
of (bd.ProductCode , bd.ProductName) , you will not get the same result
as the first query.

--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7210
by: hrishy | last post by:
Hi All I have the following query to be tuned.. Select distinct PA.PersonAddress_IDX, AT.Name AddressType, A.Line1 Address1, A.Line2 Address2, A.City, A.State, A.County, A.Country, A.PostalCode, A.AllowPostalSoftYN, PA.ChangedBy, PA.ChangedDT, PA.DeletedYN ,PA.Person_Key, PA.Address_Key, PA.AddressType_Key FROM PersonAddress_h PA,Address_h A,AddressType_h AT
1
4691
by: Daniel Roy | last post by:
Hi gurus, I just started to look at a very slow-running SQL statement generated by an application (Siebel). I spooled the SQL from the application, replaced the bind variables by their values, and tuned from the Query Analyser. But after awhile, I realized that the statement using bind variables and the same statement using the values instead of the bind variables often have completely different execution plans! Is that normal? Can...
12
8356
by: serge | last post by:
I have an SP that is big, huge, 700-800 lines. I am not an expert but I need to figure out every possible way that I can improve the performance speed of this SP. In the next couple of weeks I will work on preparing SQL statements that will create the tables, insert sample record and run the SP. I would hope people will look at my SP and give me any hints on how I can better write the SP.
2
3740
by: Ina Schmitz | last post by:
Hi NG, does IBM Universal Database 8.2 make any difference between actual and estimated execution plans like in SQL Server ("set showplan_all on" for estimated execution plan and "set statistics profile on" for actual execution plan)? Does "explain plan selection for" generate the *estimated* execution plan? Didn't find any distinction of actual or estimated execution plans in the information center. Thanks for help,
3
2037
by: mleal | last post by:
Does anyone have some more detailed information about how Oracle and MS implement / allow Tuning on Oracle 10g and SQL Server 2005 and the differences between them? Which of them, In a deep comparison about it, allow better tuning and why. Regards, Marcio Evangelista
0
20354
by: Medhatithi | last post by:
Hi, I have been in several ways benefiited from this site. I would like to share some sql tuning techniques(simple, but effective) with you all. SQL Tuning Tips Oracle Tips Session #6 3/31/98 Kathy Gleeson ________________________________________
1
1993
by: codefragment | last post by:
Hi I've heard 2 things recently, can I confirm if their true/false? (1) If you have a stored procedure and you want to optimise it you can call exec proc1, you could also use define/set for each of the variables and copy the code into query analyser, this then makes it easier to tune. However the optimiser works differently for these variables than it does for variables passed into the query via exec and will produce a less optimal
1
6235
by: Vinod Sadanandan | last post by:
A Roadmap To Query Tuning ============================ For each SQL statement, there are different approaches that could be used to retrieve the required data. Optimization is the process of choosing the most efficient way to retrieve this data based upon the evaluation of a number of different criteria. The CBO bases optimization choices on pre-gathered table and index statistics while the RBO makes it's decisions based on a set of ...
1
3098
by: deepu03 | last post by:
Hi All I have the following query to be tuned.. SELECT distinct a.EMPLID ,a.PER_ORG ,a.HIRE_DT ,a.grade
0
9734
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
10652
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10408
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,...
0
10137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9211
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7673
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
6895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5561
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...
3
3026
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.