Hi
i have an SQL query you may be able to help me with, on my website the
mailshots are stored in a table and then every recipient is stored in
a cross referenced table, most days there about 10 - 20000 people to
email, obviously over a long period of time the 'recipients' table has
grown to a large number of rows (over a million) however on a daily
basis my only request to the table is to collect the users who belong
to the mails for that day via a stored procedure (about 20000
results). However as time goes by this daily sql command is getting
slower and slower (over 2 minutes) Is this normal, or is there any way
i can keep the data in the table but speed up my request to those that
i need?? I also use count to get stats about how many have to b sent
and how many have been sent?? My SP is below, any ideas??
CREATE PROCEDURE gp_MLists_GetNewslettersWithSchedule
AS
DECLARE @TotalCount int,
@TotalSent int
set nocount on
SELECT h.NewsletterID, Approved, TemplateFile, Subject, Body,
SentDate , u.Firstname + ' ' + u.LastName AS SentBy,
(Select COUNT(ScheduleID) from MLists_Schedule WHERE NewsletterID =
h.newsletterid) AS TotalCount,
(Select COUNT(ScheduleID) from MLists_Schedule WHERE NewsletterID =
h.newsletterid AND Sent = 1) AS TotalSent,
(Select COUNT(ScheduleID) from MLists_Schedule WHERE NewsletterID =
h.newsletterid AND Sent = 0 AND SendAttempts > 0) AS TotalFailed
FROM MLists_History h
INNER JOIN Accounts_Users u ON h.SentBy = u.UserID
WHERE SentDate > Dateadd(day, -14,getdate()) AND
(Select COUNT(ScheduleID) from MLists_Schedule WHERE NewsletterID =
h.newsletterid AND (Sent = 1 OR SendAttempts > 1)) <
(Select COUNT(ScheduleID) from MLists_Schedule WHERE NewsletterID =
h.newsletterid)
ORDER BY SentDate ASC
GO
John Bell - 30 May 2008 09:22 GMT
> Hi
>
[quoted text clipped - 33 lines]
> ORDER BY SentDate ASC
> GO
Hi
You should post DDL and example data see
http://www.aspfaq.com/etiquette.asp?id=5006 if you wanted a full solution
Using a derived table to calculate the totals for MLists_Schedule should
help, I would assume there is a NewsletterID in this table and a means to
restrict the date e.g.
( Select NewsletterID , COUNT(*) AS TotalCount,
SUM(Sent) AS TotalSent,
SUM(CASE WHEN (Sent = 1 OR SendAttempts > 1) THEN 1 ELSE 0 END) AS
SomeOtherTotal
from MLists_Schedule
WHERE SentDate > Dateadd(day, -14,getdate())
GROUP BY NewsletterID
) t
You could join this on NewsletterID to the other tables and eliminate the
subqueries in the SELECT and WHERE clauses.
TotalFailed = TotalCount - TotalSent
John
Tom Cooper - 30 May 2008 17:39 GMT
I agree with John. It would help if we had the DDL. Particularly, any
indexes, primary and foreign keys.
In your post you talk about the 'recipients' table as the table that is
growing. I am guessing you mean the MLists_History table. If that is the
table with the million rows, my suggestion would be to make the first (or
only) column in the clustered index for that table to be the SentDate
column.
Of course, since you only get to have one clustered index per table, you may
find that you improve the performance of this query, but make some other
queries worse. The only way to know is to test it.
Tom
> Hi
>
[quoted text clipped - 33 lines]
> ORDER BY SentDate ASC
> GO