mysql: taking many recurring averages with different intervals using events in - mysql

i have around 500 tables and each table is for a specific device. every table is getting full with data that comes every 3 minutes . now i need to store averages of different timeframes of one same column(data) in tables .
timeframes are 10 minutes, 1 hour , 12 hour , 24 hour ,1 month.
is mysql events good for this purpose? if yes how many of them is required , i mean should i create a table called 10 minute averages with 500 columns with one event or divide them into 5 tables with 100 column and with 5 events? in second case there would be 25 events
if not what is the better solution? as you see these are datacentric jobs.
thanks a lot.

Related

How to parse a column with minutes to two different ones (one with hour and the other one with minutes)

I'm using mysql and I need a query that solves my problem:
I actually have a column called length from a table called film that has the number of minutes example: 181.
And I would like to have two new ones: One that displays the hours 3 and the other one the minutes 1
I have been trying with "case" but I don't know how to use a value into another that doesn't exist
example 2: column length has 159 and I need to show two columns, the column hours will have 2 and the minutes column 39
You can use the div and mod operators to do integer division and modulo.
SELECT *,
length div 60 hours,
length mod 60 minutes
FROM film;
Hours = integer portion of minutes/ 60
Minutes =remainder of minutes/60
Mysql has div and module functions

Prune mysql table - averaging records

I have a MySQL table which stores real-time data from different devices. Data is recorded about every 20 seconds.
The table looks like this:
report_dt device value1 value2
2015-10-16 10:32:15 solar 34.4 67.8
2015-10-16 10:32:15 grid 56.9 23.5
2015-10-16 10:32:35 solar 45.6 34.3
Queries to get the recorded values per device are pretty CPU consuming. I have (report_dt,device) as primary key. Besides, after a few days I do not need the 20 second data anymore, but just the 10 minute average.
So either the same table setup or a table per device would be satisfactory, with this contents:
report_dt device value1
2015-10-16 10:00:00 solar avg(value1 over 10 mins)
2015-10-16 10:10:10 grid avg(value1 over 10 mins)
2015-10-16 10:20:20 solar avg(value1 over 10 mins)
The idea is to run the pruning action once every 24 hours.
The reason is that data containing averages must be retrieved fast. And because the data in a 20 second interval is extremely fluctuating, I must use averaging or the data is difficult to assess.
I could do this in an application. Retrieve the average values for 10 minutes at each 10 minute interval and write it back to a different table or different tables, one for each device.
But would something also be possible with a database function?
You can do this by Events in mysql. Suppose i have 2 tables in mysql. Table_1 store the data continiously per second or miunte as your application generate. Table_2 is your main table which contains the 10 min avg data. Now you need to create a stored procedure which calls once in 10 minutes (this can be done by Events or CRON in linux ). So whats contains the stored procedure ? These steps perform in Stored Procedure
BEGIN
SELECT avg(value) of last 10 min from Table_1
INSERT into Table_2 the avg record.
DELETE avg(value) of last 10 min from Table_1
END
Caution- I am not developing mysql code, i am just show the approach

Date Difference in MySql via Codeigniter

I'm a little lost on how I should do this any help or guidance would be appreciated. I have a table that has 3 columns with multiple values inserted into it basically a log_book table of events. We can say there is an order_id, event_status, and datetime. I need to get the difference of days between the datetime columns and sum them together where order_id=? and event_status=? I know how to limit my queries to get the data I want. But what would be the best way to get the difference in days and add them together. Essentially there could be only one entry with the same order_id and event_status or there could be multiple entries with the same order_id and event_status.
Event Status codes
1 = initially assigned
2 = submitted for review
3 = sent back for more work
because 2015-01-01 to 2015-01-15 is 15 days
and 2015-01-17 to 2015-01-18 is 1 day
so the total days would be: 16 Days

get average interarrival time for service requests by timestamp

I have partly the following MySQL schema
ServiceRequests
----------
id int
RequestDateTime datetime
This is what a typical collection of records might look like.
1 | 2009-10-11 14:34:22
2 | 2009-10-11 14:34:56
3 | 2009-10-11 14:35:01
In this case the average request time is (34+5)/2 = 19.5 seconds, being
14:34:22 ---> (34 seconds) ----> 14:34:56 ------> (5 seconds) -----> 14:35:01
Basically I need to work out the difference in time between consecutive records, sum that up and divide by the number of records.
The closest thing I can think of is to convert the timestamp to epoch time and start there. I can add a field to the table to precalculate the epoch time if necessary.
How do I determine 19.5 using a sql statement(s)?
You don't really need to know the time difference of each record to get the average. You have x data points ranging from some point t0 to t1. Notice that the the last time - first time is also 39 sec. (max-min)/(count-1) should work for you
select max(RequestDateTime)-min(RequestDateTime) / (count(id)-1) from ServiceRequests;
Note: This will not work if the table is empty, due to a divide by zero.
Note2: Different databases handle subtraction of dates differently so you may need to turn that difference into seconds.
Hint: maybe using TIMEDIFF(expr1,expr2) and/or TIME_TO_SEC(expr3)

Whats the best way to store a time duration in a MySQL larger than the TIME range?

I'm in need of a method to store a time duration in a db field. I'm building a website where customers should be able to choose how long they would like an advert to display from a particular start date.
I had thought about using TIME but that has a max of '838:59:59' which works out at about 34 days. Its possible that a client would want an advert to exist for longer than that.
So what would be the best way to deal with this? Just a really large INT?
If you intend to have a column for start time and one for duration, I think you can store it in seconds. So, I assume you will have something like this;
+-----------+--------------------------+------------------+
| advert_id | start_time | duration_seconds |
+-----------+--------------------------+------------------+
| 2342342 |'2012-11-12 10:23:03' | 86400 |
+-----------+--------------------------+------------------+
(For the sake of the example, we will call this table adverts)
advert_id - a key pointing to your advert
start_time - the time the advert should start (data type - TIMESTAMP)
duration_seconds - Time in seconds that the advert is supposed to "live" (INTEGER(11)
SELECT TIME_TO_SEC(timediff(now(),start_time)) as 'time_difference_in_seconds_since_advert_started' FROM adverts;
If you want to get only adverts that have not expired, you will run a query like this;
SELECT * FROM `adverts` WHERE TIME_TO_SEC(timediff(now(),start_time))<=`duration_seconds`;
That's one way I would do it if I were to go with the "duration" field.
Yes, you can store time as INT data type (or another big integer: MEDIUMINT, LONGINT). Then use you can easily get days and time part from this, e.g. -
SELECT time DIV 86400 AS days, SEC_TO_TIME(column1 MOD 86400) AS time FROM table
Where 86400 is a number of seconds in 24h (60 * 60 * 24 = 86400).
not the best solution but you can add one column in your db, and check when time is more than 24 hours, calculate it as 1 day and write in that column, and all the rest time write in time column. But selecting from db you should calculate also that column of days