I have a relatively large 4-deep relational data setup like this:
client_applications: (potentially 1,000's of records)
- ...
- account_id
- deleted_at
client_application_versions: (potentially 10,000's of records)
- ...
- client_application_id
- deleted_at
cloud_logs: (potentially 1,000,000's of records)
- ...
- client_application_version_id
- deleted_at
logs: (potentially 1,000,000,000's of records)
- ...
- cloud_log_id
- time_stamp
- deleted_at
I am still in development so the structure and setup is not set in stone, but I think it is setup ok. Using Rails 3.2.11 and InnoDB MySQL. The database is filled with a small (compared to the eventual db size) set of data (logs only has 700,000 rows) I have 4 queries, 3 of which are problematic, to retrieve logs.
- Grab first page of logs, ordered by timestamp, limited by
account_id,client_application_id,client_application_version_id(Over 100 seconds) - Grab first page of logs, ordered by timestamp, limited by
account_id,client_application_id(Over 100 seconds) - Grab first page of logs, ordered by timestamp, limited by
account_id(Over 100 seconds) - Grab first page of logs, ordered by timestamp (~2 seconds)
Here are the EXPLAIN statements. I have indexes on all applicable fields already. Would it be better to duplicate the various
...id fields on the logs table to prevent joins? Or is there some magic sauce I am missing when making these queries? I have never dealt with this quantity of data before so perhaps my standard way of approaching the setup and queries just doesn't scale? How can I alter my setup or statements to make these queries return in a reasonable time?
UPDATE
I have added a few combined indexes on the Logs table and shaved a tiny bit off of the time. Here are the explains for that. I have concluded that the ORDER method is the source of the delay. Removing the
order by timestamp desc causes the query to return in a second or so. So the new question is why, when indexed on timestamp, does it still take over a minute to run this query?
UPDATE 2
Using a subquery to fetch the 100 id's increased performance to 14 seconds, but that is still far too long. The optimizations I have tried so far have all shortened the time a bit, but I feel that they are not getting at the root of the problem. Here is the EXPLAIN for the subquery approach.

