473,505 Members | 14,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL2005 Aggregate Multiple Update optimazation: How-To?

3 New Member
I have the need to optimize a piece of code. It updates a table, based on a detail table. The detail table (vpmod) contains 3*10^6 records, the base table (volcdet) contains 20 relevant records. Here's my code:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @plannummer VarChar(20)
  2. DECLARE    @MaxPrijswijziging Float
  3. DECLARE    @MinPrijswijziging Float
  4.  
  5. SET @MaxPrijswijziging = 4.0
  6. SET @MinPrijswijziging = 10.0
  7.  
  8. SET    @plannummer = '20080219'
  9. UPDATE mcmain.volcdet
  10.     SET vd_qbmaxed = (SELECT count(*) FROM mcmain.vpmod
  11.                         WHERE vm_updnmr = @plannummer
  12.                         AND    vm_volc = vd_volcnmr
  13.                         AND    vm_bprperc > @MaxPrijsWijziging)
  14.     WHERE vd_upnmr = @plannummer
  15. UPDATE mcmain.volcdet
  16.     SET vd_qbmined = (SELECT count(*) FROM mcmain.vpmod
  17.                         WHERE vm_updnmr = @plannummer
  18.                         AND    vm_volc = vd_volcnmr
  19.                         AND    vm_bprperc < @MinPrijsWijziging)
  20.     WHERE vd_upnmr = @plannummer
  21.  
  22. UPDATE mcmain.volcdet
  23.     SET vd_qnmaxed = (SELECT count(*) FROM mcmain.vpmod
  24.                         WHERE vm_updnmr = @plannummer
  25.                         AND    vm_volc = vd_volcnmr
  26.                         AND    vm_nprperc > @MaxPrijsWijziging)
  27.     WHERE vd_upnmr = @plannummer
  28. UPDATE mcmain.volcdet
  29.     SET vd_qnmined = (SELECT count(*) FROM mcmain.vpmod
  30.                         WHERE vm_updnmr = @plannummer
  31.                         AND    vm_volc = vd_volcnmr
  32.                         AND    vm_nprperc < @MinPrijsWijziging)
  33.     WHERE vd_upnmr = @plannummer
It essentailly tries to count the number of priceraises above and below a border value.
Does anyone have an idea how to decrease the processing time? I would think less code would make it faster. But.... ????


Cees Cappelle
Feb 22 '08 #1
1 1348
ck9663
2,878 Recognized Expert Specialist
I have the need to optimize a piece of code. It updates a table, based on a detail table. The detail table (vpmod) contains 3*10^6 records, the base table (volcdet) contains 20 relevant records. Here's my code:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @plannummer VarChar(20)
  2. DECLARE    @MaxPrijswijziging Float
  3. DECLARE    @MinPrijswijziging Float
  4.  
  5. SET @MaxPrijswijziging = 4.0
  6. SET @MinPrijswijziging = 10.0
  7.  
  8. SET    @plannummer = '20080219'
  9. UPDATE mcmain.volcdet
  10.     SET vd_qbmaxed = (SELECT count(*) FROM mcmain.vpmod
  11.                         WHERE vm_updnmr = @plannummer
  12.                         AND    vm_volc = vd_volcnmr
  13.                         AND    vm_bprperc > @MaxPrijsWijziging)
  14.     WHERE vd_upnmr = @plannummer
  15. UPDATE mcmain.volcdet
  16.     SET vd_qbmined = (SELECT count(*) FROM mcmain.vpmod
  17.                         WHERE vm_updnmr = @plannummer
  18.                         AND    vm_volc = vd_volcnmr
  19.                         AND    vm_bprperc < @MinPrijsWijziging)
  20.     WHERE vd_upnmr = @plannummer
  21.  
  22. UPDATE mcmain.volcdet
  23.     SET vd_qnmaxed = (SELECT count(*) FROM mcmain.vpmod
  24.                         WHERE vm_updnmr = @plannummer
  25.                         AND    vm_volc = vd_volcnmr
  26.                         AND    vm_nprperc > @MaxPrijsWijziging)
  27.     WHERE vd_upnmr = @plannummer
  28. UPDATE mcmain.volcdet
  29.     SET vd_qnmined = (SELECT count(*) FROM mcmain.vpmod
  30.                         WHERE vm_updnmr = @plannummer
  31.                         AND    vm_volc = vd_volcnmr
  32.                         AND    vm_nprperc < @MinPrijsWijziging)
  33.     WHERE vd_upnmr = @plannummer
It essentailly tries to count the number of priceraises above and below a border value.
Does anyone have an idea how to decrease the processing time? I would think less code would make it faster. But.... ????


Cees Cappelle

DECLARE @plannummer VarChar(20)
DECLARE @MaxPrijswijziging Float
DECLARE @MinPrijswijziging Float

SET @MaxPrijswijziging = 4.0
SET @MinPrijswijziging = 10.0

SET @plannummer = '20080219'


First, run this:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.    vd_qbmaxed = 
  3.          sum(case when vm_bprperc > @MaxPrijsWijziging then 1 else 0 end),
  4.    vd_qbmined  = 
  5.          sum(case when vm_bprperc < @MaxPrijsWijziging then 1 else 0 end),
  6.    vd_qnmaxed   = 
  7.          sum(case when vm_nprperc > @MaxPrijsWijziging) then 1 else 0 end),
  8.    vd_qnmined    = 
  9.          sum(case when vm_nprperc > @MaxPrijsWijziging then 1 else 0 end)
  10. WHERE vd_upnmr = @plannummer
  11.          and vm_updnmr = @plannummer
  12.          AND    vm_volc = vd_volcnmr
And see if the numbers are correct by running each individual subquery from your UPDATE statement and comparing the result.

We're doing this so as you don't overwrite the value of your q?mined and q?maxed while you're doing some testing on your query. Or you can always have a backup.

If you're statisfied with the result, you can now run this:

UPDATE mcmain.volcdet
set
vd_qbmaxed = summary_list.vd_qbmaxed,
vd_qbmined = summary_list.vd_qbmined,
vd_qnmaxed = summary_list.vd_qnmaxed,
vd_qnmined = summary_list.vd_qnmined
from
(
SELECT vd_upnmr,
vd_qbmaxed =
sum(case when vm_bprperc > @MaxPrijsWijziging then 1 else 0 end),
vd_qbmined =
sum(case when vm_bprperc < @MaxPrijsWijziging then 1 else 0 end),
vd_qnmaxed =
sum(case when vm_nprperc > @MaxPrijsWijziging) then 1 else 0 end),
vd_qnmined =
sum(case when vm_nprperc > @MaxPrijsWijziging then 1 else 0 end)
from mcmain.vpmod
WHERE vm_updnmr = @plannummer
AND vm_volc = vd_volcnmr
group by vm_updnmr
) summary_list
where vd_upnmr = vm_updnmr

This query will update your vd_q?maxed and vd_q?mined whether there's a valid condition (<, >) or not. The subquery (summary_list) will always return a row for < and >, not for =. If the condition is false, it will 0 if true, it will be the count. If you don't want your vd_q?maxed and vd_q?mined overwritten add a CASE..WHEN function to check if the value you're using to update is zero, otherwise retain the value of the column.

Good luck.

-- CK
Feb 22 '08 #2

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

Similar topics

2
303
by: MrMike | last post by:
I'm having an issue with an aggregate function in line 6 of the code below. The error is: "An aggregate may not appear in the set list of an UPDATE statement." How can i SUM this value...
1
2232
by: A P | last post by:
How can I go about updating multiple records or deleting multiple records from a DB at a time?
1
1812
by: Tom Hebbron | last post by:
Thought others might find this useful (PostgreSQL 7.4+ only) When used, it outputs an array of the inputs, in order. CREATE OR REPLACE FUNCTION aggregate_array(ANYARRAY,ANYELEMENT) RETURNS...
5
6046
by: Jim | last post by:
Need help with aggregate function...for each unique product, I need the provider with the cheapest cost factor Here't the table (Table1) ID product provider factor 1 123456 abc...
2
1050
by: ad | last post by:
Hi, As title, After I install Sql2005 SepCTP, can I install VS2005 Beta2 in the same machine?
22
6389
by: Mal Ball | last post by:
I hope I have the right forum for this question. I have an existing Windows application which uses a SQL Server database and stored procedures. I am now developing a web application to use the same...
1
1606
by: royashish | last post by:
#include<iostream> using namespace std; // Counter - Class class counter{ private: int m_data; public: counter(){
1
9143
by: codeWarrior007 | last post by:
Hi, i'm running a query and everytime I run the query i get the error: 'Aggregate Type' is not a valid name. Make sure that it does not include invalid characters, punctuation, or is not too...
0
1315
by: ArunDhaJ | last post by:
Hi Friends, What is the equivalent function of pwdcompare() in Sql2005? Thanks, ArunDhaJ
1
1823
by: clickingwires | last post by:
How do you consecutively number rows in an aggregate query?
0
7216
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,...
0
7098
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...
0
7303
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,...
1
7018
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...
0
7471
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...
1
5028
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...
0
3187
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...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
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.