473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

storing samples of data in mysql

Hi,

I would like to store samples of data which are gathered from about
500 devices in mysql. Each device has its own data which changes over
time. The data may be integer or float.

The queries I'm interested in our something like: "show me when the
data from device X was greater than 70 and the data from device Y was
greater than 90".

The trivial way of doing so is to use one large table in which each
column represents a device plus a datetime column. In this table I can
gather each second a sample from each device and store in the table.
This way I can easily query the data I need.

However, as the rate of change of the data in each device is low
(about 1 every 20 minutes average), I would like to store each device
in a table of its own and only add a new record when the data changes.
That way I will have 500 tables but each will be small. The problem is
that I can't figure out how can I do the queries I need.
Doing something like:
select x,y from table_x,table_y where (x>70 && y>90 &&
table_x.time=table_y.time);

Won't work as I don't insert every second a record into every table.

I may have two tables such as:

X Y
time data time data
03:00:00 69 03:30:00 92
04:00:00 70 04:30:00 93
05:00:00 71 05:30:00 94

Is there a way to tell mysql that these are points of samples in time,
and so it will actually expand the tables for the queries, something
like:

X
time data
03:00:00 69
03:00:01 69
03:00:02 69
..
..
..
03:59:59 69
04:00:00 70
Please advise,

Thanks,

Ehud.
Jul 20 '05 #1
24 2102
Ehud Shabtai wrote:
Please advise,


Use one table and be sure to use indexes.

And when you do queries don't compare time of two different devices,
just use a time limit to get all values from wanted timeperiod.

Then in your program use this data and fill in the missing parts.

This is the "correct way" to do this IMHO. Because
1) Table structure is simple
2) Queries are simple and fast
3) Data which queries return is small ( This is usually a good thing.
Especially if you are using a network database. )

There might be some problems with this. You might need to be able to
retrieve the value before time limit(extra query something like 'select
* from table where time < timelimit limit 1'), unless you want to get
all data for devices.
Jul 20 '05 #2
Ehud Shabtai wrote:
Please advise,


Use one table and be sure to use indexes.

And when you do queries don't compare time of two different devices,
just use a time limit to get all values from wanted timeperiod.

Then in your program use this data and fill in the missing parts.

This is the "correct way" to do this IMHO. Because
1) Table structure is simple
2) Queries are simple and fast
3) Data which queries return is small ( This is usually a good thing.
Especially if you are using a network database. )

There might be some problems with this. You might need to be able to
retrieve the value before time limit(extra query something like 'select
* from table where time < timelimit limit 1'), unless you want to get
all data for devices.
Jul 20 '05 #3
Ehud Shabtai wrote:
Please advise,


Use one table and be sure to use indexes.

And when you do queries don't compare time of two different devices,
just use a time limit to get all values from wanted timeperiod.

Then in your program use this data and fill in the missing parts.

This is the "correct way" to do this IMHO. Because
1) Table structure is simple
2) Queries are simple and fast
3) Data which queries return is small ( This is usually a good thing.
Especially if you are using a network database. )

There might be some problems with this. You might need to be able to
retrieve the value before time limit(extra query something like 'select
* from table where time < timelimit limit 1'), unless you want to get
all data for devices.
Jul 20 '05 #4
With this approach I have to insert a new record every second even though it
will be the same as the last record (The data in the devices does not change
every second). Isn't there a way to insert new records only when something
changes and still be able to query the information I need?

Ehud.

"Aggro" <sp**********@yahoo.com> wrote in message
news:hT**************@read3.inet.fi...
Ehud Shabtai wrote:
Please advise,


Use one table and be sure to use indexes.

And when you do queries don't compare time of two different devices,
just use a time limit to get all values from wanted timeperiod.

Then in your program use this data and fill in the missing parts.

This is the "correct way" to do this IMHO. Because
1) Table structure is simple
2) Queries are simple and fast
3) Data which queries return is small ( This is usually a good thing.
Especially if you are using a network database. )

There might be some problems with this. You might need to be able to
retrieve the value before time limit(extra query something like 'select
* from table where time < timelimit limit 1'), unless you want to get
all data for devices.

Jul 20 '05 #5
With this approach I have to insert a new record every second even though it
will be the same as the last record (The data in the devices does not change
every second). Isn't there a way to insert new records only when something
changes and still be able to query the information I need?

Ehud.

"Aggro" <sp**********@yahoo.com> wrote in message
news:hT**************@read3.inet.fi...
Ehud Shabtai wrote:
Please advise,


Use one table and be sure to use indexes.

And when you do queries don't compare time of two different devices,
just use a time limit to get all values from wanted timeperiod.

Then in your program use this data and fill in the missing parts.

This is the "correct way" to do this IMHO. Because
1) Table structure is simple
2) Queries are simple and fast
3) Data which queries return is small ( This is usually a good thing.
Especially if you are using a network database. )

There might be some problems with this. You might need to be able to
retrieve the value before time limit(extra query something like 'select
* from table where time < timelimit limit 1'), unless you want to get
all data for devices.

Jul 20 '05 #6
With this approach I have to insert a new record every second even though it
will be the same as the last record (The data in the devices does not change
every second). Isn't there a way to insert new records only when something
changes and still be able to query the information I need?

Ehud.

"Aggro" <sp**********@yahoo.com> wrote in message
news:hT**************@read3.inet.fi...
Ehud Shabtai wrote:
Please advise,


Use one table and be sure to use indexes.

And when you do queries don't compare time of two different devices,
just use a time limit to get all values from wanted timeperiod.

Then in your program use this data and fill in the missing parts.

This is the "correct way" to do this IMHO. Because
1) Table structure is simple
2) Queries are simple and fast
3) Data which queries return is small ( This is usually a good thing.
Especially if you are using a network database. )

There might be some problems with this. You might need to be able to
retrieve the value before time limit(extra query something like 'select
* from table where time < timelimit limit 1'), unless you want to get
all data for devices.

Jul 20 '05 #7
es22 wrote:
With this approach I have to insert a new record every second even though it
will be the same as the last record (The data in the devices does not change
every second). Isn't there a way to insert new records only when something
changes and still be able to query the information I need?


I don't understand why would you need to insert it?

Simple example:

create table device_value(
device int unsigned,
value int,
insert_time datetime );

insert into device_value values(1,5,'2004-04-20 20:00:00');
insert into device_value values(2,3,'2004-04-20 20:05:00');
insert into device_value values(1,3,'2004-04-20 20:10:00');

mysql> select * from device_value;
+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 2 | 3 | 2004-04-20 20:05:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
+--------+-------+---------------------+
Let's imagine that you have new value(7) for device 1. And you want to
know wheter you should insert it or not.

select max(insert_time) from device_value where device=1 and value=7;

If this returns null, then you should insert the row. If it returns you
a datetime value, it means that you already have that value inserted for
that device and you shouldn't insert it. i.e.

mysql> select max(insert_time) from device_value where device=1 and value=3;
+---------------------+
| max(insert_time) |
+---------------------+
| 2004-04-20 20:10:00 |
+---------------------+

mysql> select max(insert_time) from device_value where device=1 and value=7;
+------------------+
| max(insert_time) |
+------------------+
| NULL |
+------------------+

Jul 20 '05 #8
es22 wrote:
With this approach I have to insert a new record every second even though it
will be the same as the last record (The data in the devices does not change
every second). Isn't there a way to insert new records only when something
changes and still be able to query the information I need?


I don't understand why would you need to insert it?

Simple example:

create table device_value(
device int unsigned,
value int,
insert_time datetime );

insert into device_value values(1,5,'2004-04-20 20:00:00');
insert into device_value values(2,3,'2004-04-20 20:05:00');
insert into device_value values(1,3,'2004-04-20 20:10:00');

mysql> select * from device_value;
+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 2 | 3 | 2004-04-20 20:05:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
+--------+-------+---------------------+
Let's imagine that you have new value(7) for device 1. And you want to
know wheter you should insert it or not.

select max(insert_time) from device_value where device=1 and value=7;

If this returns null, then you should insert the row. If it returns you
a datetime value, it means that you already have that value inserted for
that device and you shouldn't insert it. i.e.

mysql> select max(insert_time) from device_value where device=1 and value=3;
+---------------------+
| max(insert_time) |
+---------------------+
| 2004-04-20 20:10:00 |
+---------------------+

mysql> select max(insert_time) from device_value where device=1 and value=7;
+------------------+
| max(insert_time) |
+------------------+
| NULL |
+------------------+

Jul 20 '05 #9
es22 wrote:
With this approach I have to insert a new record every second even though it
will be the same as the last record (The data in the devices does not change
every second). Isn't there a way to insert new records only when something
changes and still be able to query the information I need?


I don't understand why would you need to insert it?

Simple example:

create table device_value(
device int unsigned,
value int,
insert_time datetime );

insert into device_value values(1,5,'2004-04-20 20:00:00');
insert into device_value values(2,3,'2004-04-20 20:05:00');
insert into device_value values(1,3,'2004-04-20 20:10:00');

mysql> select * from device_value;
+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 2 | 3 | 2004-04-20 20:05:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
+--------+-------+---------------------+
Let's imagine that you have new value(7) for device 1. And you want to
know wheter you should insert it or not.

select max(insert_time) from device_value where device=1 and value=7;

If this returns null, then you should insert the row. If it returns you
a datetime value, it means that you already have that value inserted for
that device and you shouldn't insert it. i.e.

mysql> select max(insert_time) from device_value where device=1 and value=3;
+---------------------+
| max(insert_time) |
+---------------------+
| 2004-04-20 20:10:00 |
+---------------------+

mysql> select max(insert_time) from device_value where device=1 and value=7;
+------------------+
| max(insert_time) |
+------------------+
| NULL |
+------------------+

Jul 20 '05 #10
First, thanks for your help.

I still have a couple of questions:
1. How can I query something like:'when - the data from device 1 is greater
than 4 and the data from device 2 is greater than 2'.
In your example I expect the query to return something like:
from 2004-04-20 20:05:00 to 2004-04-20 20:09:59

If I had samples each second this could have been easy to get however, it is
a waste of space t have the same record over and over. Is there a way to get
the result I want with the table you suggest?

2. What if the two devices have different data types (one integer and one
float), would you suggest to use two tables?
If I have 500 devices, should I still use 1 table for all devices or maybe
using 500 different tables is more efficient (as each table will be very
small)?

Thanks,

Ehud.

"Aggro" <sp**********@yahoo.com> wrote in message
news:%O**************@read3.inet.fi...
es22 wrote:
With this approach I have to insert a new record every second even though it will be the same as the last record (The data in the devices does not change every second). Isn't there a way to insert new records only when something changes and still be able to query the information I need?
I don't understand why would you need to insert it?

Simple example:

create table device_value(
device int unsigned,
value int,
insert_time datetime );

insert into device_value values(1,5,'2004-04-20 20:00:00');
insert into device_value values(2,3,'2004-04-20 20:05:00');
insert into device_value values(1,3,'2004-04-20 20:10:00');

mysql> select * from device_value;
+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 2 | 3 | 2004-04-20 20:05:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
+--------+-------+---------------------+
Let's imagine that you have new value(7) for device 1. And you want to
know wheter you should insert it or not.

select max(insert_time) from device_value where device=1 and value=7;

If this returns null, then you should insert the row. If it returns you
a datetime value, it means that you already have that value inserted for
that device and you shouldn't insert it. i.e.

mysql> select max(insert_time) from device_value where device=1 and

value=3; +---------------------+
| max(insert_time) |
+---------------------+
| 2004-04-20 20:10:00 |
+---------------------+

mysql> select max(insert_time) from device_value where device=1 and value=7; +------------------+
| max(insert_time) |
+------------------+
| NULL |
+------------------+

Jul 20 '05 #11
First, thanks for your help.

I still have a couple of questions:
1. How can I query something like:'when - the data from device 1 is greater
than 4 and the data from device 2 is greater than 2'.
In your example I expect the query to return something like:
from 2004-04-20 20:05:00 to 2004-04-20 20:09:59

If I had samples each second this could have been easy to get however, it is
a waste of space t have the same record over and over. Is there a way to get
the result I want with the table you suggest?

2. What if the two devices have different data types (one integer and one
float), would you suggest to use two tables?
If I have 500 devices, should I still use 1 table for all devices or maybe
using 500 different tables is more efficient (as each table will be very
small)?

Thanks,

Ehud.

"Aggro" <sp**********@yahoo.com> wrote in message
news:%O**************@read3.inet.fi...
es22 wrote:
With this approach I have to insert a new record every second even though it will be the same as the last record (The data in the devices does not change every second). Isn't there a way to insert new records only when something changes and still be able to query the information I need?
I don't understand why would you need to insert it?

Simple example:

create table device_value(
device int unsigned,
value int,
insert_time datetime );

insert into device_value values(1,5,'2004-04-20 20:00:00');
insert into device_value values(2,3,'2004-04-20 20:05:00');
insert into device_value values(1,3,'2004-04-20 20:10:00');

mysql> select * from device_value;
+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 2 | 3 | 2004-04-20 20:05:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
+--------+-------+---------------------+
Let's imagine that you have new value(7) for device 1. And you want to
know wheter you should insert it or not.

select max(insert_time) from device_value where device=1 and value=7;

If this returns null, then you should insert the row. If it returns you
a datetime value, it means that you already have that value inserted for
that device and you shouldn't insert it. i.e.

mysql> select max(insert_time) from device_value where device=1 and

value=3; +---------------------+
| max(insert_time) |
+---------------------+
| 2004-04-20 20:10:00 |
+---------------------+

mysql> select max(insert_time) from device_value where device=1 and value=7; +------------------+
| max(insert_time) |
+------------------+
| NULL |
+------------------+

Jul 20 '05 #12
First, thanks for your help.

I still have a couple of questions:
1. How can I query something like:'when - the data from device 1 is greater
than 4 and the data from device 2 is greater than 2'.
In your example I expect the query to return something like:
from 2004-04-20 20:05:00 to 2004-04-20 20:09:59

If I had samples each second this could have been easy to get however, it is
a waste of space t have the same record over and over. Is there a way to get
the result I want with the table you suggest?

2. What if the two devices have different data types (one integer and one
float), would you suggest to use two tables?
If I have 500 devices, should I still use 1 table for all devices or maybe
using 500 different tables is more efficient (as each table will be very
small)?

Thanks,

Ehud.

"Aggro" <sp**********@yahoo.com> wrote in message
news:%O**************@read3.inet.fi...
es22 wrote:
With this approach I have to insert a new record every second even though it will be the same as the last record (The data in the devices does not change every second). Isn't there a way to insert new records only when something changes and still be able to query the information I need?
I don't understand why would you need to insert it?

Simple example:

create table device_value(
device int unsigned,
value int,
insert_time datetime );

insert into device_value values(1,5,'2004-04-20 20:00:00');
insert into device_value values(2,3,'2004-04-20 20:05:00');
insert into device_value values(1,3,'2004-04-20 20:10:00');

mysql> select * from device_value;
+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 2 | 3 | 2004-04-20 20:05:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
+--------+-------+---------------------+
Let's imagine that you have new value(7) for device 1. And you want to
know wheter you should insert it or not.

select max(insert_time) from device_value where device=1 and value=7;

If this returns null, then you should insert the row. If it returns you
a datetime value, it means that you already have that value inserted for
that device and you shouldn't insert it. i.e.

mysql> select max(insert_time) from device_value where device=1 and

value=3; +---------------------+
| max(insert_time) |
+---------------------+
| 2004-04-20 20:10:00 |
+---------------------+

mysql> select max(insert_time) from device_value where device=1 and value=7; +------------------+
| max(insert_time) |
+------------------+
| NULL |
+------------------+

Jul 20 '05 #13
es22 wrote:
I still have a couple of questions:
1. How can I query something like:'when - the data from device 1 is greater
than 4 and the data from device 2 is greater than 2'.
In your example I expect the query to return something like:
from 2004-04-20 20:05:00 to 2004-04-20 20:09:59
What if the data is smaller in the middle of two edges? In that case you
can either ignore the middle part, or you can have two time limits. How
should it be in that case?

We could for example have test data like this:

+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
| 1 | 6 | 2004-04-20 20:14:00 |
| 1 | 2 | 2004-04-20 20:16:00 |
| 1 | 12 | 2004-04-20 20:20:00 |
| 1 | 2 | 2004-04-20 20:23:00 |
+--------+-------+---------------------+

2. What if the two devices have different data types (one integer and one
float), would you suggest to use two tables?
You could have two columns, one for each data type. And either use null
value in the other column to mark which is used, or use third column to
identify which column is used if that suits better for your needs.
If I have 500 devices, should I still use 1 table for all devices or maybe
using 500 different tables is more efficient (as each table will be very
small)?


If you use indexes, it doesn't matter.
Jul 20 '05 #14
es22 wrote:
I still have a couple of questions:
1. How can I query something like:'when - the data from device 1 is greater
than 4 and the data from device 2 is greater than 2'.
In your example I expect the query to return something like:
from 2004-04-20 20:05:00 to 2004-04-20 20:09:59
What if the data is smaller in the middle of two edges? In that case you
can either ignore the middle part, or you can have two time limits. How
should it be in that case?

We could for example have test data like this:

+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
| 1 | 6 | 2004-04-20 20:14:00 |
| 1 | 2 | 2004-04-20 20:16:00 |
| 1 | 12 | 2004-04-20 20:20:00 |
| 1 | 2 | 2004-04-20 20:23:00 |
+--------+-------+---------------------+

2. What if the two devices have different data types (one integer and one
float), would you suggest to use two tables?
You could have two columns, one for each data type. And either use null
value in the other column to mark which is used, or use third column to
identify which column is used if that suits better for your needs.
If I have 500 devices, should I still use 1 table for all devices or maybe
using 500 different tables is more efficient (as each table will be very
small)?


If you use indexes, it doesn't matter.
Jul 20 '05 #15
es22 wrote:
I still have a couple of questions:
1. How can I query something like:'when - the data from device 1 is greater
than 4 and the data from device 2 is greater than 2'.
In your example I expect the query to return something like:
from 2004-04-20 20:05:00 to 2004-04-20 20:09:59
What if the data is smaller in the middle of two edges? In that case you
can either ignore the middle part, or you can have two time limits. How
should it be in that case?

We could for example have test data like this:

+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
| 1 | 6 | 2004-04-20 20:14:00 |
| 1 | 2 | 2004-04-20 20:16:00 |
| 1 | 12 | 2004-04-20 20:20:00 |
| 1 | 2 | 2004-04-20 20:23:00 |
+--------+-------+---------------------+

2. What if the two devices have different data types (one integer and one
float), would you suggest to use two tables?
You could have two columns, one for each data type. And either use null
value in the other column to mark which is used, or use third column to
identify which column is used if that suits better for your needs.
If I have 500 devices, should I still use 1 table for all devices or maybe
using 500 different tables is more efficient (as each table will be very
small)?


If you use indexes, it doesn't matter.
Jul 20 '05 #16
"Aggro" <sp**********@yahoo.com> wrote in message
news:w_**************@read3.inet.fi...
es22 wrote:
I still have a couple of questions:
1. How can I query something like:'when - the data from device 1 is greater than 4 and the data from device 2 is greater than 2'.
In your example I expect the query to return something like:
from 2004-04-20 20:05:00 to 2004-04-20 20:09:59


What if the data is smaller in the middle of two edges? In that case you
can either ignore the middle part, or you can have two time limits. How
should it be in that case?

We could for example have test data like this:

+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
| 1 | 6 | 2004-04-20 20:14:00 |
| 1 | 2 | 2004-04-20 20:16:00 |
| 1 | 12 | 2004-04-20 20:20:00 |
| 1 | 2 | 2004-04-20 20:23:00 |
+--------+-------+---------------------+

I can't ignore any middle parts.
This table actully represents:
device 1 has:
value 5 from 20:00:00 to 20:09:59.
value 3 from 20:10:00 to 20:13:59.
value 6 from 20:14:00 to 20:15:59.
and so on.

So I need to be able to answer a query like: 'when device 1 has values
greater than 4'
and the result should be:
5 - from 20:00:00 to 20:09:59
6 - from 20:14:00 to 20:15:59

One way to solve it may be with adding a duplicated record which indicates
the last time a value existed:
+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 1 | 5 | 2004-04-20 20:09:59 |
| 1 | 3 | 2004-04-20 20:10:00 |
| 1 | 3 | 2004-04-20 20:13:59 |
| 1 | 6 | 2004-04-20 20:14:00 |
| 1 | 6 | 2004-04-20 20:15:59 |
| 1 | 2 | 2004-04-20 20:16:00 |
| 1 | 2 | 2004-04-20 20:19:59 |
+--------+-------+---------------------+

Here, the duplicated data is minimal.

Is there any other way?
2. What if the two devices have different data types (one integer and one float), would you suggest to use two tables?


You could have two columns, one for each data type. And either use null
value in the other column to mark which is used, or use third column to
identify which column is used if that suits better for your needs.
If I have 500 devices, should I still use 1 table for all devices or maybe using 500 different tables is more efficient (as each table will be very
small)?


If you use indexes, it doesn't matter.

So using 500 tables has the same performance of 1 big table with 500
different devices?
Jul 20 '05 #17
"Aggro" <sp**********@yahoo.com> wrote in message
news:w_**************@read3.inet.fi...
es22 wrote:
I still have a couple of questions:
1. How can I query something like:'when - the data from device 1 is greater than 4 and the data from device 2 is greater than 2'.
In your example I expect the query to return something like:
from 2004-04-20 20:05:00 to 2004-04-20 20:09:59


What if the data is smaller in the middle of two edges? In that case you
can either ignore the middle part, or you can have two time limits. How
should it be in that case?

We could for example have test data like this:

+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
| 1 | 6 | 2004-04-20 20:14:00 |
| 1 | 2 | 2004-04-20 20:16:00 |
| 1 | 12 | 2004-04-20 20:20:00 |
| 1 | 2 | 2004-04-20 20:23:00 |
+--------+-------+---------------------+

I can't ignore any middle parts.
This table actully represents:
device 1 has:
value 5 from 20:00:00 to 20:09:59.
value 3 from 20:10:00 to 20:13:59.
value 6 from 20:14:00 to 20:15:59.
and so on.

So I need to be able to answer a query like: 'when device 1 has values
greater than 4'
and the result should be:
5 - from 20:00:00 to 20:09:59
6 - from 20:14:00 to 20:15:59

One way to solve it may be with adding a duplicated record which indicates
the last time a value existed:
+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 1 | 5 | 2004-04-20 20:09:59 |
| 1 | 3 | 2004-04-20 20:10:00 |
| 1 | 3 | 2004-04-20 20:13:59 |
| 1 | 6 | 2004-04-20 20:14:00 |
| 1 | 6 | 2004-04-20 20:15:59 |
| 1 | 2 | 2004-04-20 20:16:00 |
| 1 | 2 | 2004-04-20 20:19:59 |
+--------+-------+---------------------+

Here, the duplicated data is minimal.

Is there any other way?
2. What if the two devices have different data types (one integer and one float), would you suggest to use two tables?


You could have two columns, one for each data type. And either use null
value in the other column to mark which is used, or use third column to
identify which column is used if that suits better for your needs.
If I have 500 devices, should I still use 1 table for all devices or maybe using 500 different tables is more efficient (as each table will be very
small)?


If you use indexes, it doesn't matter.

So using 500 tables has the same performance of 1 big table with 500
different devices?
Jul 20 '05 #18
"Aggro" <sp**********@yahoo.com> wrote in message
news:w_**************@read3.inet.fi...
es22 wrote:
I still have a couple of questions:
1. How can I query something like:'when - the data from device 1 is greater than 4 and the data from device 2 is greater than 2'.
In your example I expect the query to return something like:
from 2004-04-20 20:05:00 to 2004-04-20 20:09:59


What if the data is smaller in the middle of two edges? In that case you
can either ignore the middle part, or you can have two time limits. How
should it be in that case?

We could for example have test data like this:

+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 1 | 3 | 2004-04-20 20:10:00 |
| 1 | 6 | 2004-04-20 20:14:00 |
| 1 | 2 | 2004-04-20 20:16:00 |
| 1 | 12 | 2004-04-20 20:20:00 |
| 1 | 2 | 2004-04-20 20:23:00 |
+--------+-------+---------------------+

I can't ignore any middle parts.
This table actully represents:
device 1 has:
value 5 from 20:00:00 to 20:09:59.
value 3 from 20:10:00 to 20:13:59.
value 6 from 20:14:00 to 20:15:59.
and so on.

So I need to be able to answer a query like: 'when device 1 has values
greater than 4'
and the result should be:
5 - from 20:00:00 to 20:09:59
6 - from 20:14:00 to 20:15:59

One way to solve it may be with adding a duplicated record which indicates
the last time a value existed:
+--------+-------+---------------------+
| device | value | insert_time |
+--------+-------+---------------------+
| 1 | 5 | 2004-04-20 20:00:00 |
| 1 | 5 | 2004-04-20 20:09:59 |
| 1 | 3 | 2004-04-20 20:10:00 |
| 1 | 3 | 2004-04-20 20:13:59 |
| 1 | 6 | 2004-04-20 20:14:00 |
| 1 | 6 | 2004-04-20 20:15:59 |
| 1 | 2 | 2004-04-20 20:16:00 |
| 1 | 2 | 2004-04-20 20:19:59 |
+--------+-------+---------------------+

Here, the duplicated data is minimal.

Is there any other way?
2. What if the two devices have different data types (one integer and one float), would you suggest to use two tables?


You could have two columns, one for each data type. And either use null
value in the other column to mark which is used, or use third column to
identify which column is used if that suits better for your needs.
If I have 500 devices, should I still use 1 table for all devices or maybe using 500 different tables is more efficient (as each table will be very
small)?


If you use indexes, it doesn't matter.

So using 500 tables has the same performance of 1 big table with 500
different devices?
Jul 20 '05 #19
es22 wrote:
I can't ignore any middle parts. So I need to be able to answer a query like: 'when device 1 has values
greater than 4'
and the result should be:
5 - from 20:00:00 to 20:09:59
6 - from 20:14:00 to 20:15:59
You say that data amount for each device is quite small. One solution
would then be that you just select all data for that device and parse it
in your application.

If that is not possible, then there might be somekind of solution by
using joins or subselects (which can be used starting from MySQL version= 4.1). Anyway making such a query can be pretty hard. At least it

would be that for me, and I don't think I can help with that.
Jul 20 '05 #20
es22 wrote:
I can't ignore any middle parts. So I need to be able to answer a query like: 'when device 1 has values
greater than 4'
and the result should be:
5 - from 20:00:00 to 20:09:59
6 - from 20:14:00 to 20:15:59
You say that data amount for each device is quite small. One solution
would then be that you just select all data for that device and parse it
in your application.

If that is not possible, then there might be somekind of solution by
using joins or subselects (which can be used starting from MySQL version= 4.1). Anyway making such a query can be pretty hard. At least it

would be that for me, and I don't think I can help with that.
Jul 20 '05 #21
es22 wrote:
I can't ignore any middle parts. So I need to be able to answer a query like: 'when device 1 has values
greater than 4'
and the result should be:
5 - from 20:00:00 to 20:09:59
6 - from 20:14:00 to 20:15:59
You say that data amount for each device is quite small. One solution
would then be that you just select all data for that device and parse it
in your application.

If that is not possible, then there might be somekind of solution by
using joins or subselects (which can be used starting from MySQL version= 4.1). Anyway making such a query can be pretty hard. At least it

would be that for me, and I don't think I can help with that.
Jul 20 '05 #22
Thanks Aggro,

Can anyone else advise here?

"Aggro" <sp**********@yahoo.com> wrote in message
news:9E**************@read3.inet.fi...
es22 wrote:
I can't ignore any middle parts.

So I need to be able to answer a query like: 'when device 1 has values
greater than 4'
and the result should be:
5 - from 20:00:00 to 20:09:59
6 - from 20:14:00 to 20:15:59


You say that data amount for each device is quite small. One solution
would then be that you just select all data for that device and parse it
in your application.

If that is not possible, then there might be somekind of solution by
using joins or subselects (which can be used starting from MySQL version
>= 4.1). Anyway making such a query can be pretty hard. At least it

would be that for me, and I don't think I can help with that.

Jul 20 '05 #23
Thanks Aggro,

Can anyone else advise here?

"Aggro" <sp**********@yahoo.com> wrote in message
news:9E**************@read3.inet.fi...
es22 wrote:
I can't ignore any middle parts.

So I need to be able to answer a query like: 'when device 1 has values
greater than 4'
and the result should be:
5 - from 20:00:00 to 20:09:59
6 - from 20:14:00 to 20:15:59


You say that data amount for each device is quite small. One solution
would then be that you just select all data for that device and parse it
in your application.

If that is not possible, then there might be somekind of solution by
using joins or subselects (which can be used starting from MySQL version
>= 4.1). Anyway making such a query can be pretty hard. At least it

would be that for me, and I don't think I can help with that.

Jul 20 '05 #24
Thanks Aggro,

Can anyone else advise here?

"Aggro" <sp**********@yahoo.com> wrote in message
news:9E**************@read3.inet.fi...
es22 wrote:
I can't ignore any middle parts.

So I need to be able to answer a query like: 'when device 1 has values
greater than 4'
and the result should be:
5 - from 20:00:00 to 20:09:59
6 - from 20:14:00 to 20:15:59


You say that data amount for each device is quite small. One solution
would then be that you just select all data for that device and parse it
in your application.

If that is not possible, then there might be somekind of solution by
using joins or subselects (which can be used starting from MySQL version
>= 4.1). Anyway making such a query can be pretty hard. At least it

would be that for me, and I don't think I can help with that.

Jul 20 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steven | last post by:
Hi there, I am wanting to store price data as n.nn format, so if the user enters "1" the data that gets stored is "1.00" Is there a way to do this. Cheers Steven
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
0
by: Eben Goodman | last post by:
I am storing book isbn numbers in a table. isbn numbers are 10 digit numbers and many start with 0. The data type of the field I am storing this info in is a bigint(16) unsigned. It appears that...
8
by: Francesco Moi | last post by:
Hello. I'm parsing an XML file, and trying to store its contents into a MySQL database. But I've got problems. If I print the word before storing it, I get 'música' (OK). But if I store it...
3
by: hamvil79 | last post by:
I'm implementig a java web application using MySQL as database. The main function of the application is basically to redistribuite documents. Those documents (PDF, DOC with an average size around...
9
by: Brad | last post by:
I have written some code to manipulate data/records in a MASTER (order header) and DETAIL (order details) tables. What I have written is too extensive to post but essentially trying to: 1....
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
1
by: tomi.trescak | last post by:
Hi I have a problem with storing rich text in MySQL. I store rich text in MySQL (in column with type "text") which i get from Rich Textbox control. When i do reverse processing by trying to...
9
by: PI | last post by:
Hi Guys, I need some assistance with the following scenario please. I know this might be more of a MySQL post than a PHP one, but it is likely some developer has been here before and would be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.