MYSQL DATE function running insanely slow in LEFT JOIN

心已入冬 提交于 2019-12-23 20:00:32

问题


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:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1765667/mysql-date-function-running-insanely-slow-in-left-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!