Speed up 'last used' query by only checking table when needed
Only check FactNotificationStatus table for last used date if there are any notifications at all for that template in that table.
Ticket: https://trello.com/c/RyEMRpQw/719-find-out-why-template-last-use-query-is-slow-and-fails
Possible other solutions: we could only search through data from last 6 months or last year. Then we would have to change current warning text: "This template has never been used." to something like: "This template has not been used within last 6 months."
Here is the screenshot with the warning text for context:
I think changing the text would make it a bit more confusing - do we mean the template was used before last 6 months, or that we just don't know (that second interpretation would be more true).
So I vote for just making this little change for now and keeping an eye on if the problem with slow query keeps happening. Then we could either try the above solution, or maybe fiddle with a custom index for this query if we think that would be useful? fact_notification_status table is only updated periodically, so maybe an extra index on it, on bst_date and template_id columns, could be a good fit?
Note from chat with Karl:
It will be ok to restrict query to last year, as long as we get the content right.