A
A
Alexander2011-03-22 13:45:39
MySQL
Alexander, 2011-03-22 13:45:39

MySQL Select every nth row

Friends, tell me please.
There is a table with three fields - id (auto_increment), date (timestamp) and value .
Every 10 seconds, one value entry is added to the database. The rest of the fields are filled in automatically.

It is necessary to build graphs for an hour, a day, etc.
To build a graph for an hour, you need to select 60 records out of 360. That is, one value is taken for every minute (every 6). Accordingly, per day - 24 records out of 8640 (each 360). And so on…

Is it possible to implement such a query using MySQL using id or date?

Answer the question

In order to leave comments, you need to log in

11 answer(s)
H
Horzerus, 2011-03-22
@lumenous

If you need to sort data, you can use grouping by date field format:
For example, to group by minutes:
GROUP BY DATE_FORMAT (`date`, "%y-%m-%d-%H-%i")
"%y% j%H-%i" is the same, but it will be faster this way.

A
AlexeyK, 2011-03-22
@AlexeyK

And how are you going to build a graph for every 6th value, if you have 6 different (probably?) values, and you will take only one (not even the arithmetic mean)?

Z
Zakharov Alexander, 2011-03-22
@AlexZaharow

select * from tbl where id%10=0 and datetime>=addtime(now(), '-0:1:00');
Select data for the last minute, in which the id is 10. Something similar will happen in a day.

D
Dzuba, 2011-03-22
@Dzuba

Since such a table can have a very decent volume, which will inevitably suffer speed, I would advise you to implement all the logic at the time of inserting a new record, namely:
1. Create an additional. tables for records of the hour, day, etc., in which to store only records for the corresponding points in time. Then access to them will be very fast. True, the volume will increase.
2. If there is access to the software that writes to the database, check the value of the corresponding interval in it. If not, or if the requirement to “implement using MySQL” is important, then use a trigger to insert into the source table, in which to check the condition and duplicate the record in the tables for the hour, day, etc.
At the same time, it is enough to insert into the additional tables not the entry itself, but only its id.

L
lashtal, 2011-03-22
@lashtal

Select every nth as follows (example for every 60th):
SELECT @row := @row + 1 row, t.id
FROM table t, (SELECT @row := 0) row
GROUP BY t.id
HAVING row % 60 = 0
The problem is obvious: all rows are selected, and then filtering by having. Yes, according to the index, everything should work quickly and filtering at the database level will be faster than on the used jap, but this is clearly not the case.
It is wrong to do this on mysql, there is an orthodox rrdtool , which is designed to work with sequences of values ​​in time, automatically receiving data in the context of some time (for example, a day, a week, a month, a year).

J
Jazzist, 2011-03-22
@Jazzist

Solution 1. Is the minute timestamp evenly divisible by 60? You can write a comparison condition for MySQL.
Solution 2. Is it possible to write an additional key? If yes, then there is another resource-saving solution.

R
rvller, 2011-03-22
@rvller

Select all records for the required interval and from them, as mentioned above, calculate the arithmetic mean of several values, which will be the point of the graph.

A
Anatoly, 2011-03-22
@taliban

Judging by the data task, you will have a lot. I would advise you to add fields and operate on them

That is, if the time values ​​in the
database
records look
like this : :10 then we only take 22:19:00 22:20:00
Here the like will die, it is worth adding the field “Is 00 at the end?” and choose from it.
Again, if the selection is by date, then break the dates into parts and select these parts.
These options are more bulky, but add speed for sure.

S
Scioner, 2011-03-22
@Scioner

select field1, field2 from table
where mod(id, 60)=0
select field1, field2 from table
where mod(id, 360)=0
select field1, field2 from table
where mod(id, 1880)=0
etc…

A
Alexander, 2011-03-22
@lumenous

To paraphrase more precisely ...
The value value is a temperature reading.
When building an hourly chart, 60 readings are required. From the database, we take one value for every minute of this hour. That is, if the time values ​​in the database records
look like this : :10 then we take only 22:19:00 22:20:00 What happened during this minute can be ignored.

Z
Zakharov Alexander, 2011-03-22
@AlexZaharow

I really liked about rrdtools. Found the link fairly quickly: Using rrdtool to monitor a network . Mega item!!! Did not know. Thanks for the tip.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question