Tim.Lucas
Tim.Lucas

Reputation: 271

Optimizing a WHERE clause with a dateadd function

For the business that I am working in I would like to get information on our customers. The base information I have on these customers is as follows:

What I would like to know is the following, I would like to know how much each customer has paid on 1, or 2, or 3, etc. months after his Activation_Date. So the query would look something like the following (slightly pseudo-code, the more important part is the WHERE clause).

SELECT
cldai.Sum_Paid_To_Date,
cldai.ActivityDate,
cldai.Customer_Account_Number
FROM
CustomerLoanDailyActivity_Information cldai
INNER JOIN 
Loans l ON l.Customer_Account_Number = cldai.Customer_Account_Number
WHERE
  (cldai.ActivityDate = CAST(l.Activation_Date AS date)
   OR
   cldai.ActivityDate = DATEADD(month, 1, CAST(l.Activation_Date AS date)) 
   OR
   cldai.ActivityDate = DATEADD(month, 2, CAST(l.Activation_Date AS date))
   OR
   cldai.ActivityDate = DATEADD(month, 3, CAST(l.Activation_Date AS date)) 
   )
ORDER BY
l.Customer_Account_Number, cldai.ActivityDate ASC

So the problem is that this query is really really slow (because of the WHERE clause and because the cldai table is big (~6 GB)) and exits before any data is retrieved. A couple of problems that I have heard, and possible solutions, but haven't worked so far.

Any ideas on how to make this query perform more quickly? Any help would be greatly appreciated.

Upvotes: 1

Views: 456

Answers (3)

Tim.Lucas
Tim.Lucas

Reputation: 271

So a massive speedup was obtained by using a LEFT JOIN instead of an INNER JOIN and by not ordering the data on the server but on the client side. This reduced the query time from about an hour and 10 minutes to about 1 minute. It seems unbelievable but it's what happened.

Regards,

Tim.

Upvotes: 1

Hedley
Hedley

Reputation: 1092

How about splitting this up into two steps? Step one - build a table with the four dates for each customer. Then step two, join this to your main CustomerLoanDailyActivity_Information table on date and customer account number. The second step would have a much simpler join, just an = between the ActivityDate and date entry in the table you have built.

Upvotes: 0

Hedley
Hedley

Reputation: 1092

If you are guaranteed to have a record for each day, you could apply use the row_number() function to apply row numbers for each group of customer loan repayment records, and then retrieve rows 1,31,61 and 91? This would avoid any date manipulation.

Upvotes: 0

Related Questions