Let’s say I have the following SQL query
SELECT id, name, title, description, time
FROM entity
WHERE UNIXTIMESTAMP(CONCAT(date_end, time_end)) > UNIXTIMESTAMP(NOW())
AND UNIXTIMESTAMP(CONCAT(next_date, next_time)) < UNIXTIMESTAMP(NOW())
I was looking into views and other ways to make that query more efficient. The problem is if I have over 10,000 entities to process; then the query will prob take a long time.
What does MySQL provide as tools to make the query above more efficient?
If you’re just after general techniques, there are already SO questions on the topic, not to mention the MySQL manual section on optimization.
If you’re after specific recommendations for your query, note that MySQL can’t apply indices if a column is passed through a function. You’ll need to get rid of the
UNIXTIMESTAMPandCONCATcalls around the *_end and next_* columns. One approach would be to change the table schema: combine the columns into “end” and “next” columns of typeDATETIMEorTIMESTAMP. Another would be to separate the current time into a date and time, and use that to compare to the columns separately.