-1

Having sensors spread around my house, they all send data to a mysql DB. Sensors send multiple samples per communication with the DB that occur every 5 minutes.

On table 'sensors' I have generic sensor info (primary key SensorId)

I need to pull the most recent SampleTime from the table 'sensordata' and also the most recent LastConnection from the table 'sensormonitoring' for each Distinct SensorId on table 'sensor'. So I can compare them with PHP.

Right now I'm doing this with whiles in PHP but I'm guessing there has to be an easier and more efficient way to do this with SQL (everytime I run the page 21 queries are done)

I have this:

SELECT SensorId FROM sensors

And then inside the while:

$query1 = "SELECT LastConnection
             FROM sensormonitoring
             WHERE SensorId =" . $row["SensorId"] . "
             ORDER BY LastConnection desc
             LIMIT 1";

$query2 = "SELECT SampleTime
             FROM sensordata
             WHERE SensorId =" . $row["SensorId"] ."
             ORDER BY SampleTime desc
             LIMIT 1";

Is there a way to do this with a single SQL query (perhaps with JOINS?) avoiding long PHP code?

Final result would be something like this

SensorId   LastConnection        SampleTime
----------------------------------------------------
01         2014-04-20 09:38:56   2014-04-20 09:30:00
02         2014-04-20 09:35:05   2014-04-19 10:15:00
03         2014-04-20 09:33:27   2014-04-20 09:26:00
04         2014-04-20 09:45:12   NULL

Just an extra explanation of my project: In this case I could see that Sensor 02 is having problems because the most recent samples are older than 15 minutes but is communicating fine with the DB; Sensor 04 is communicating but isn't collecting samples at all.

This also to say that: sometimes SampleTime might be NULL.

1
  • Consider providing proper DDLs (and/or an sqlfiddle) TOGETHER WITH THE DESIRED RESULT SET
    – Strawberry
    Apr 21, 2014 at 17:20

3 Answers 3

1

You should be able to do this all in one query by using MAX:

SELECT s.SensorId, MAX(m.LastConnection) AS LastConnection, MAX(d.SampleTime) AS SampleTime
FROM sensors s
  LEFT JOIN sensormonitoring m on (s.SensorId = m.SensorId)
  LEFT JOIN sensordata d on (s.SensorId = d.SensorId)
GROUP BY s.SensorId;
5
  • ...as long as you don't select any other columns
    – Strawberry
    Apr 21, 2014 at 17:22
  • That doesn't work for the NULL possibility. You need LEFT OUTER JOIN in that case. Apr 21, 2014 at 17:27
  • I ran the query and it froze mySQL while 'Loading' ... is it because sensordata was more than 100.000 lines? Without pulling most recent sensor data works great with LastConnection values being given... Apr 21, 2014 at 17:41
  • @AfonsoGomes: Make sure you have indexes on all the SensorId fields. In general whenever you join, you want to join only on indexed fields. Also adding an index to the LastConnection and SampleTime will probably help too.
    – Senseful
    Apr 21, 2014 at 17:56
  • I should have add more detail to my question. :( On sensordata table, SensorId is not an index. This because each sensor is composed of channels (Temperature, humidity, etc)... Each time a sensor communicates data, this table gets a new line for each channel that sensor has. Let's imagine a sensor has 2 channels, 2 new rows are added to sensordata, both with the same SensorId and both with the same SampleTime... I think the DB design has flaws but I can't do much about it as I can't change it. Apr 21, 2014 at 18:19
0

This should be a working SQL statement that gives you all info you request: SELECT SensorId, (SELECT MAX(LastConnection) FROM sensormonitoring sm WHERE sm.SensorId = s.SensorId) AS LastConnection, (SELECT MAX(SampleTime) FROM sensordata sd WHERE sd.SensorId = s.SensorId) AS SampleTime FROM Sensors s

6
  • Why the downvote? Using subselects for this kind of query is more apt than joining tables. Apr 21, 2014 at 17:39
  • I just upvoted. The query you provided worked great! But it took 0.4537 secs to run... don't know if using JOINS is more efficient or not Apr 21, 2014 at 17:54
  • Subselects arnt great in mysql. Much more efficient in ms sql for some reason. As long as you don't have huge tables you should be fine though... Apr 21, 2014 at 17:59
  • Also, I hope you have an index on SensorId for all tables?! If not, make sure you set it up... Apr 21, 2014 at 18:01
  • Create an Index on sensormonitoring for columns (SensorId,LastConnection) and an Index on sensordata for columns (SensorId, SampleTime). That will make my statement most efficient. Apr 21, 2014 at 18:08
-2

Try this

SELECT sensormonitoring.SensorId, sensormonitoring.LastConnection, sensordata.SampleTime
FROM sensormonitoring
INNER JOIN sensordata
ON sensormonitoring.SensorId=sensordata.SensorId;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.