views:

110

answers:

3

When adding the line:

LEFT JOIN core_records_sales as sales ON DATE(appointments.date) = DATE(sales.date_sold)

To my query, it boosts the time for the script to run from about 8 seconds, to 2-3 minutes.

Would there be some data causing this problem or am I not implementing the function correctly?

I need to use DATE() because I need them to be the same day but the date fields are DATETIME

+1  A: 

Do you have an index on the date column? Joining without an index requires a full table scan, so that's likely the cause of the performance issue.

Run your query through an EXPLAIN to see what the query optimizer comes up with for the query plan.

Ben S
Seeing as how OP's underlying columns are DATETIME, index won't really help.
ChssPly76
A: 

It's probably because you're forcing a full table scan on both tables whereas previously you were using an index as part of the query. You need to examine the plan (and provide us with a little more detail too) so run it through EXPLAIN to see what's happening.

Cruachan
+2  A: 

This is almost certainly an issue with the appointments.date field being indexed. Adding the use of the DATE() function makes it so that index cannot be used, forcing a table scan.

I've had to deal with this before, and the best way I've found to solve the issue is to have a separate column with just the date part (no time) in it or to store the date and time in two separate columns.

I'd love to hear if others have a better way of dealing with that though.

Eric Petroelje
Unfortunately might have to go this route. Thanks!
kilrizzy