Skip to content
Advertisement

Indexing an SQL table by datetime that is scaling

I have a large table that gets anywhere from 1-3 new entries per minute. I need to be able to find records at specific times which I can do by using a SELECT statement but it’s incredibly slow. Lets say the table looks like this:

I’m trying to get data like this:

I also need to get data like this:

But I don’t know what the Date-Time will be until requested. In this case, I will have to search the entire table for these times. Can I index the start of the day so I know where dates are?

Is there a way to index this table in order to dramatically decrease the queries? Or is there a better way to achieve this?

I have tried indexing the Date-Time column but I did not decrease the query time at all.

Advertisement

Answer

For this query:

You want an index on mytable(date_time, device). This matches the columns that come into play in the WHERE clause, so the database should be able to lookup the matching rows efficiently.

Note that I removed the single quotes around the literal value given to device: if this is an integer, as it looks like, then it should be treated as such.

The ordering of the column in the index matters; generally, you want the most restrictive column first – from the description of your question, this would probably be date_time, hence the above suggestion. You might want to try the other way around as well (so: mytable(device, date_time)).

Another thing to keep in mind from performance perspective: you should probably enumerate the columns you want in the SELECT clause; if you just want a few additional columns, then it can be useful to add them to the index as well; this gives you a covering index, that the database can use to execute the whole query without even looking back at the data.

Say:

Then consider:

Or:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement