I want to get the number of unique mobile phone entries per day that have been logged to a database and have never appeared in the log. I thought it was a trivial query but shock when the query took 10 minutes on a table with about 900K entries. A sample Select is getting the number of unique mobile phones that were logged on the 9th of April 2015 and had never been logged before. Its like getting who are the truly new visitors to you site on a specific day. SQL Fiddle Link
SELECT COUNT(DISTINCT mobile_number)
FROM log_entries
WHERE created_at BETWEEN '2015-04-09 00:00:00'
AND '2015-04-09 23:59:59'
AND mobile_number NOT IN (
SELECT mobile_number
FROM log_entries
WHERE created_at < '2015-04-09 00:00:00'
)
I have individual indexes on created_at
and on mobile_number
.
Is there a way to make it faster? I see a very similar question here on SO but that was working with two tables.