473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Query Help.

I have this query that I need to speed up. I don't know where to
start. Any advise would be greatly apreciated

SELECT
centerID AS [Center ID], StoreID AS [Store Code], Region, City AS
City, Owner AS [Owner Name], Brand AS Brand,
(SELECT OrderDate FROM [ORDER] WHERE StoreID = S.StoreID AND revid =
0 AND programid = 4) AS [BS Enrollment Date],
(SELECT count(*) FROM production WHERE P_S_RecID = S.StoreID AND
P_PrintID like '%BrandSmart%' and P_PromoMonth = @Month and
P_PromoYear = @Year) AS [BS Quantity],
CASE S.Brand
WHEN 'Allegra Print & Imaging' THEN 75
WHEN 'American Speedy Printing' THEN 75
WHEN 'Insty-Prints' THEN 100
ELSE 0
END
As [BS HO Quantity],
((SELECT count(*) FROM production WHERE P_S_RecID = S.StoreID AND
P_PrintID like '%BrandSmart%' and P_PromoMonth = @Month and
P_PromoYear = @Year) - CASE
WHEN (SELECT count(*) FROM production WHERE P_S_RecID = S.StoreID
AND P_PrintID like '%BrandSmart%' and P_PromoMonth = @Month and
P_PromoYear = @Year) = 0 then 0
WHEN S.Brand = 'Allegra Print & Imaging' THEN 75
WHEN S.Brand = 'American Speedy Printing' THEN 75
WHEN S.Brand = 'Insty-Prints' THEN 100
ELSE 0
END)
as [Billable BS],
(SELECT count(*) FROM production WHERE P_DateResponded is not null
and P_S_RecID = S.StoreID AND P_PrintID like '%LeadSmart%' and
P_PromoMonth = @Month and P_PromoYear = @Year) AS [BS Leads],
(SELECT OrderDate FROM [ORDER] WHERE StoreID = S.StoreID AND revid =
0 AND programid = 5) AS [LS Enrollment Date],
(SELECT count(*) FROM production WHERE P_S_RecID = S.StoreID AND
P_PrintID like '%LeadSmart%' and P_PromoMonth = @Month and P_PromoYear
= @Year) AS [LS Quantity],
CASE
WHEN S.Brand = 'Insty-Prints' THEN 300
ELSE 0
END
As [LS HO Quantity],
((SELECT count(*) FROM production WHERE P_S_RecID = S.StoreID AND
P_PrintID like '%LeadSmart%' and P_PromoMonth = @Month and P_PromoYear
= @Year) - CASE
WHEN (SELECT count(*) FROM production WHERE P_S_RecID = S.StoreID
AND P_PrintID like '%LeadSmart%' and P_PromoMonth = @Month and
P_PromoYear = @Year) = 0 then 0
WHEN S.Brand = 'Insty-Prints' THEN 300
ELSE 0
END)
as [Billable LS],
'0' as [LS Newsletter Quantity],
'0' as [New Business Quantity],
(Select Original_Record_Count From User_Lists WHERE Type = 'Accudata'
and User_ID = S.StoreID and Order_ID is not null) as [Prospect List
Quantity],
1000 as [Allegra HO Quantity],
50 as [Enrollment Fee],
UCC.[Name] as [Name on CC],
UCC.Type as [CC Type],
UCC.Number as [CC Number],
UCC.Expiration_Date as [CC Exp Date],
SBI.SBI_Address1 as [Address 1],
SBI.SBI_ZipCode as [Zip Code],
(Case
When s.UserCC = 1 then 'No'
ELSE 'Yes'
END) as [Invoiced]
FROM dbo.Stores S LEFT JOIN User_Credit_Cards UCC
ON S.StoreID = UCC.User_ID LEFT JOIN StoresBillingInfo SBI
ON S.StoreID = SBI.SBI_S_StoreID
WHERE
(StoreID NOT LIKE '%test%') AND
(StoreID <'admin') ANd
((SELECT OrderDate FROM [ORDER] WHERE StoreID = S.StoreID AND revid =
0 AND programid = 5) is not null OR
(SELECT OrderDate FROM [ORDER] WHERE StoreID = S.StoreID AND revid =
0 AND programid = 4) is not null)

Apr 6 '07 #1
2 1396
Designing Solutions WD wrote:
I have this query that I need to speed up. I don't know where to
start. Any advise would be greatly apreciated
Indexes probably make a lot of difference. Which ones have you
defined? Which ones are more/less restrictive on the data? (e.g.
"30% of the records have StoreID = 'Southwest' but only 1% have
CenterID = 'Phoenix')

The pattern searches on production.P_PrintID and Stores.StoreID
can't be helping matters. How many of each do you have? Assuming
there's another table Printers with P_PrintID as its primary key,
you may be able to speed things up by using the following:

create view BS_Printers as
select P_PrintID from Printers where P_PrintID like '%BrandSmart%'

create view LS_Printers as
select P_PrintID from Printers where P_PrintID like '%LeadSmart%'

Here's a first stab at refactoring. Note the comments. Less redundancy
means less opportunity to screw things up by changing one block but
forgetting to change another. Pre-loading the #quantities temp table
may make the speed better / worse / about the same; unless it makes it
significantly worse, I recommend sticking with it.

-- Column types are educated guesses. Insert "post DDL" rant here.
create table #quantities (
StoreID varchar(10),
PromoMonth int,
PromoYear int,
BS_Quantity int,
LS_Quantity
)
go

insert into #quantities (
StoreID,
PromoMonth,
PromoYear,
BS_Quantity,
LS_Quantity
)
select
P_S_RecID,
P_PromoMonth,
P_PromoYear,
sum(case when P_PrintID like '%BrandSmart%' then 1 else 0 end)
as BS_Quantity,
sum(case when P_PrintID like '%LeadSmart%' then 1 else 0 end)
as LS_Quantity,
-- Did you really mean to combine LeadSmart with BS like this?
sum(case when P_PrintID like '%LeadSmart%'
and P_DateResponded is not null then 1 else 0 end) as BS_Leads
from production
group by P_S_RecID, P_PromoMonth, P_PromoYear
go

SELECT
centerID AS [Center ID],
StoreID AS [Store Code],
Region,
City AS City,
Owner AS [Owner Name],
Brand AS Brand,
o_bs.OrderDate as [BS Enrollment Date],
q.BS_Quantity as [BS Quantity],
CASE S.Brand
WHEN 'Allegra Print & Imaging' THEN 75
WHEN 'American Speedy Printing' THEN 75
WHEN 'Insty-Prints' THEN 100
ELSE 0
END As [BS HO Quantity],
(q.BS_Quantity - case
WHEN q.BS_Quantity = 0 then 0
WHEN S.Brand = 'Allegra Print & Imaging' THEN 75
WHEN S.Brand = 'American Speedy Printing' THEN 75
WHEN S.Brand = 'Insty-Prints' THEN 100
ELSE 0
END) as [Billable BS],
q.BS_Leads AS [BS Leads],
o_ls.OrderDate as [LS Enrollment Date],
q.LS_Quantity AS [LS Quantity],
CASE
WHEN S.Brand = 'Insty-Prints' THEN 300
ELSE 0
END As [LS HO Quantity],
(q.LS_Quantity - CASE
WHEN q.LS_Quantity = 0 then 0
WHEN S.Brand = 'Insty-Prints' THEN 300
ELSE 0
END) as [Billable LS],
'0' as [LS Newsletter Quantity],
'0' as [New Business Quantity],
ul.Original_Record_Count as [Prospect List Quantity],
1000 as [Allegra HO Quantity],
50 as [Enrollment Fee],
UCC.[Name] as [Name on CC],
UCC.Type as [CC Type],
UCC.Number as [CC Number],
UCC.Expiration_Date as [CC Exp Date],
SBI.SBI_Address1 as [Address 1],
SBI.SBI_ZipCode as [Zip Code],
(Case
When s.UserCC = 1 then 'No'
ELSE 'Yes'
END) as [Invoiced]
FROM dbo.Stores S
left join #quantities q
on S.StoreID = q.StoreID and @Month = q.PromoMonth
and @Year = q.PromoYear
LEFT JOIN User_Credit_Cards UCC ON S.StoreID = UCC.User_ID
LEFT JOIN StoresBillingInfo SBI ON S.StoreID = SBI.SBI_S_StoreID
join ORDER o_bs
on S.StoreID = o_bs.StoreID and o_bs.revid = 0
and o_bs.programid = 4
join ORDER o_ls
on S.StoreID = o_ls.StoreID and o_ls.revid = 0
and o_ls.programid = 5
left join User_Lists ul
on S.StoreID = ul.User_ID and ul.Type = 'Accudata'
and ul.Order_ID is not null
WHERE StoreID not like '%test%'
and StoreID <'admin'
and o_bs.OrderDate is not null
and o_ls.OrderDate is not null
go

drop table #quantities
go
Apr 6 '07 #2
Ed,

You are a my hero.

Thank you so much I will give your code a detailed look but that
worked wonders 33 seconds down to 3 second.

Michael

Apr 6 '07 #3

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

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
7
by: Simon Bailey | last post by:
How do you created a query in VB? I have a button on a form that signifies a certain computer in a computer suite. On clicking on this button i would like to create a query searching for all...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
36
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I...
5
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the...
10
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
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...
0
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
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,...

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.