473,699 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with writting a Report (SQL slow running)

Hi

I am trying to write a report that calculates the average number of sales
over 7, 14, 31 and 365 days for each hourly period of the day. the problem
is it takes over 4 minutes to run.

e.g.
Average Xactions per Hour
7 Days 14 Days 31 Days 365 Days
00:00 - 01:00 1,141.6 579.2 261.6 28.8
01:00 - 02:00 1,298.0 649.6 293.4 30.0
The report was use to be purely ASP running SQL Statements.
I then changed it to ASP Running a SP 24 times - this reduced running time
by about 1 minute.
I then changed it so that the stored proc looped internally 24 times and
returns the data.

I have ran the Index Tuning Wizard on the SQL and Implemented the indexes
suggested - this actually increase execution time by 20 seconds.
Below is the stored procedure I am currently using that loops internally 24
times and returns the data.

Can anyone suggest a better way / any improvements I could make ?
Many Thanks

Steve

----------------------------------------------------------------------------
-------------------------

CREATE procedure ams_RPT_Gen_Sta ts
@strResult varchar(8) = 'Failure' output,
@strErrorDesc varchar(512) = 'SP Not Executed' output,
@strTest varchar(1),
@strCurrency varchar(3),
@strVFEID varchar(16)
as
declare @strStep varchar(32)

set @strStep = 'Start of Stored Proc'

/* start insert sp code here */
create table ##Averages (
TheHour varchar(2),
Day7Avge float ,
Day14Avge float ,
Day31Avge float ,
Day365Avge float
)
declare @numHour varchar(2)
declare @strSQL varchar(2000)
declare @Wholesalers varchar(64)

declare MyHours cursor FORWARD_ONLY READ_ONLY for
select convert(char(2) , timestamp,14) as TheHour
from xactions
group by convert(char(2) , timestamp,14)
order by convert(char(2) , timestamp,14)
if @strTest = 'Y'
select @Wholesalers = VALUE FROM BUSINESSRULES WHERE NAME =
'TEST_Wholesale rs'
open MyHours

fetch next from MyHours into @numHour

while @@fetch_status = 0

begin
set @strSQL = 'insert into ##Averages (TheHour, Day7Avge) ( select ''' +
@numHour + ''', ' +
'count(*) / 7.00 ' +
'FROM ' +
'XACTIONS INNER JOIN ' +
'RETAILER ON XACTIONS.RETAIL ERID = RETAILER.RETAIL ERID ' +
'WHERE ' +
'(DATEDIFF(DAY , xactions.timest amp , GETDATE() ) < 8) and ' +
'xactions.xacti ontotal <> 0 and ' +
' convert(char(2) , timestamp, 14) = ''' + @numHour + ''' '

if @strTest = 'Y'
set @strSQL = @strSQL + ' and retailer.BillOr gID not in (' +
@Wholesalers + ') '

if @strCurrency <> '*'
set @strSQL = @strSQL + ' and xactions.XACTIO NCURRENCY = ''' +
@strCurrency + ''' '

if @strVFEID <> '*'
set @strSQL = @strSQL + ' and xactions.VFEID = ''' + @strVFEID + ''''
set @strSQL = @strSQL + ')'

exec ( @strSQL )
set @strSQL = 'update ##Averages set Day14Avge = ( select ' +
'count(*) / 14.00 ' +
'FROM ' +
'XACTIONS INNER JOIN ' +
'RETAILER ON XACTIONS.RETAIL ERID = RETAILER.RETAIL ERID ' +
'WHERE ' +
'(DATEDIFF(DAY , xactions.timest amp , GETDATE() ) < 15) and ' +
'xactions.xacti ontotal <> 0 and ' +
' convert(char(2) , timestamp, 14) = ''' + @numHour + ''' '

if @strTest = 'Y'
set @strSQL = @strSQL + ' and retailer.BillOr gID not in (' +
@Wholesalers + ') '

if @strCurrency <> '*'
set @strSQL = @strSQL + ' and xactions.XACTIO NCURRENCY = ''' +
@strCurrency + ''' '

if @strVFEID <> '*'
set @strSQL = @strSQL + ' and xactions.VFEID = ''' + @strVFEID + ''' '

set @strSQL = @strSQL + ') where TheHour = ''' + @numHour + ''' '

exec ( @strSQL )
set @strSQL = 'update ##Averages set Day31Avge = ( select ' +
'count(*) / 31.00 ' +
'FROM ' +
'XACTIONS INNER JOIN ' +
'RETAILER ON XACTIONS.RETAIL ERID = RETAILER.RETAIL ERID ' +
'WHERE ' +
'(DATEDIFF(DAY , xactions.timest amp , GETDATE() ) < 32) and ' +
'xactions.xacti ontotal <> 0 and ' +
' convert(char(2) , timestamp, 14) = ''' + @numHour + ''' '

if @strTest = 'Y'
set @strSQL = @strSQL + ' and retailer.BillOr gID not in (' +
@Wholesalers + ') '

if @strCurrency <> '*'
set @strSQL = @strSQL + ' and xactions.XACTIO NCURRENCY = ''' +
@strCurrency + ''' '

if @strVFEID <> '*'
set @strSQL = @strSQL + ' and xactions.VFEID = ''' + @strVFEID + ''' '

set @strSQL = @strSQL + ' ) where TheHour = ''' + @numHour + ''' '

exec ( @strSQL )
set @strSQL = 'update ##Averages set Day365Avge = ( select ' +
'count(*) / 365.00 ' +
'FROM ' +
'XACTIONS INNER JOIN ' +
'RETAILER ON XACTIONS.RETAIL ERID = RETAILER.RETAIL ERID ' +
'WHERE ' +
'(DATEDIFF(DAY , xactions.timest amp , GETDATE() ) < 366) and ' +
'xactions.xacti ontotal <> 0 and ' +
' convert(char(2) , timestamp, 14) = ''' + @numHour + ''' '

if @strTest = 'Y'
set @strSQL = @strSQL + ' and retailer.BillOr gID not in (' +
@Wholesalers + ') '

if @strCurrency <> '*'
set @strSQL = @strSQL + ' and xactions.XACTIO NCURRENCY = ''' +
@strCurrency + ''' '

if @strVFEID <> '*'
set @strSQL = @strSQL + ' and xactions.VFEID = ''' + @strVFEID + ''' '

set @strSQL = @strSQL + ' ) where TheHour = ''' + @numHour + ''' '
exec ( @strSQL )

fetch next from MyHours into @numHour

end -- while fetch

close MyHours
deallocate MyHours

select * from ##Averages order by TheHour

drop table ##Averages
/* end insert sp code here */

if (@@error <> 0)
begin
set @strResult = 'Failure'
set @strErrorDesc = 'Fail @ Step :' + @strStep + ' Error : ' +
CONVERT(VARCHAR ,@@Error)
return -1969
end
else
begin
set @strResult = 'Success'
set @strErrorDesc = ''
end

return 0

GO

Jul 20 '05 #1
1 3706
Many Thanks

I'll have a look at making those changes.

"Erland Sommarskog" <so****@algonet .se> wrote in message
news:Xn******** **************@ 127.0.0.1...
Steve Thorpe (st***********@ nospam.hotmail. com) writes:
I am trying to write a report that calculates the average number of
sales over 7, 14, 31 and 365 days for each hourly period of the day.
the problem is it takes over 4 minutes to run.

Troubleshooting performance problems over newsgroups is often difficult
because, without access to the database it's not possible to test
various scenarios. And without complete knowledge about the tables
involved it is even more difficult. Just seeing the procedure code,
may sometimes be sufficient, but not always.

Anyway, I have two suggestions for your procedure, although none of
them are likely to improve performance radically.

The first is that you use a global temptable. Change this to a local
temp table. This avoids problems if two users run this procedure
simultaneously.

The other is that you rewrite the procedure to not use dynamic SQL.
As far as I can see, the only reason that you use dynamic SQL, is
that you intended to have a comma-separated list in @Wholesalers.

I would suggest that you handle the list like this:

CREATE TABLE #wholesalers (id int NOT NULL)
INSERT #wholesalers (id)
SELECT number FROM iter_intlist_to _table(@wholesa lers)

You find the code for iter_intlist_to _table at
http://www.algonet.se/~sommar/arrays...st-of-integers.

Armed with this temp table, you can, as far as I can see, rip out the
dynamic SQL and replace it with static. This may give some performance
benefit, but only some single second.

However, once you have rewritten the code into static SQL, it will be
more pleasant to take a look at it.

It also helps if you post CREATE TABLE and CREATE INDEX statements
for the involved table. Some hints about data sizes is also good.

--
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

0
2754
by: Steve Thorpe | last post by:
Hi I am trying to write a report that calculates the average number of sales over 7, 14, 31 and 365 days for each hourly period of the day. the problem is it takes over 4 minutes to run. e.g. Average Xactions per Hour 7 Days 14 Days 31 Days 365 Days 00:00 - 01:00 1,141.6 579.2 261.6 28.8
4
7208
by: Mee Yamo | last post by:
Fellas!! This is a very complicated one and it took me a few days to figure out exactly what's going on, but here's the final story: I have a production environment running on .NET with a SQL Server (2000, SP3). The SQL Server is on a dedicated Proliant computer with 2GB RAM (the actual SQLServer.exe process has dynamic memory assignment and can reach up to 1.6GB RAM). Nothing else is running on that specific computer.
15
1498
by: Eric J. Holtman | last post by:
I feel like the answer to this should be blinding obvious. I also feel like it's probably an exercise in an undergraduate database design course. I'm off to google for an answer, but I figure I'd throw it out here to see if there's a quick and dirty solution. I have in my database a table of every position we have here called Holdings. It would be equivalent to an Inventory table in every basic design course. There are many users...
4
5372
by: sherkozmo | last post by:
SQL2000 - AccessXP I built an adp file with a stored procedure from SQL as follows: SELECT * FROM Z_mis_sjk_job_code_access WHERE job_code=@JobCode UNION ALL SELECT * FROM Z_mis_sjk_job_code_access_mkey WHERE job_code=@JobCode ORDER BY app_only, submenu_number, menu_routine_number,
2
1447
by: Ron | last post by:
Hi everyone. Windows XP Pro/Access2000/2.4gig chip/512meg RAM/200GigDrive. I'm stumped. I've been involved in other things lately, but came back to the program I'm preparing and zipped into a report and it took FOREVER to load and run (well, about 12 seconds, which SEEMED like "forever"). I'm almost sure it didn't do this before I took my break--can reports get upset at you if you leave them alone?
8
1927
by: billmiami2 | last post by:
I'm experiencing a strange problem that I believe is related to ADO.NET but I can't say for sure. I have a simple ASP.NET reporting interface to a SQL Server 2000 database. One report that we run returns a listing of community members and their contact information using a stored procedure. Depending on the selected community, this can return from a hundred to over 1000 rows. Occasionally, the report stops running when a community with...
9
5758
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just call it Express for simplicity). I have, to try to simplify things, put the exact same DB on two systems, one running MSDE and one running Express. Both have 2 Ghz processors (one Intel, one AMD), both have a decent amount of RAM (Intel system...
9
2142
by: Salad | last post by:
I have access, for testing at my client's site, a Win2000 computer running A2003 retail. He recently upgraded all of his other machines to DualCore Pentiums with 2 gig ram and run A2003 runtime. I believe all current SPs for Windows and Office are installed on the fast machines. I have 1 process/subroutine that has worked for a couple of years without a problem. It works fine on the testing (slow) machine. The process checks a folder...
6
1601
by: Steve | last post by:
I have a 2 unmatched queries Table1_view (records in table1 not in table2) Table2_view (records in table2 not in table1) Both table contain fields ReportID ReportName ReportOwner In_Table1 (True or False) In_Table2 (True or False)
0
8613
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
9172
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
8908
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
8880
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...
1
6532
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
4374
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
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
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
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.