Problem
I have the following table definition:
id BIGINT
resource_id INT
timestamp DATETIME
data JSON
The primary key is id and I have an index on (resource_id, timestamp). The data is a json object (3KB), there are 5 parameters in this object of type FLOAT (among other things) that are fetched regularly. The table size is roughly 60GB and 15,000,000 rows. Data is recorded per minute for each resource id.
I want to optimize the following query:
SELECT
resource_id
timestamp
data->'$.p1' /* can also include the other parameters in the select p2-p5 */
FROM table
WHERE resource_id = ? AND timestamp BETWEEN ? AND ?;
What I tried
- Creating a virtual column per parameter and creating an index for each of them (resource_id, timestamp, pX). This improves the query significantly from 70s to 8s (for fetching 6 months) but I'm worried about the number of indexes slowing down my inserts. Also, I can no longer include multiple parameters in the query since only one index can be used.
I also looked into partitioning by the resource_id but I'm not sure if that will help since mysql limits partitions to 8192 and the resources could easily exceed that.
Any idea on how to fetch this data as fast as mysql allows?