Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:51:15+00:00 2026-05-27T01:51:15+00:00

I have 3 tables with the following schema: CREATE TABLE `devices` ( `device_id` int(11)

  • 0

I have 3 tables with the following schema:

CREATE TABLE  `devices` (
  `device_id` int(11) NOT NULL auto_increment,
  `name` varchar(20) default NULL,
  `appliance_id` int(11) default '0',
  `sensor_type` int(11) default '0',
  `display_name` VARCHAR(100),
  PRIMARY KEY  USING BTREE (`device_id`)
) 

CREATE TABLE  `channels` (
  `channel_id` int(11) NOT NULL AUTO_INCREMENT,
  `device_id` int(11) NOT NULL,
  `channel` varchar(10) NOT NULL,
  PRIMARY KEY (`channel_id`),
  KEY `device_id_idx` (`device_id`)
) 

CREATE TABLE  `historical_data` (
  `date_time` datetime NOT NULL,
  `channel_id` int(11) NOT NULL,
  `data` float DEFAULT NULL,
  `unit` varchar(10) DEFAULT NULL,
  KEY `devices_datetime_idx` (`date_time`) USING BTREE,
  KEY `channel_id_idx` (`channel_id`)
)

The setup is that a device can have one or more channels and each channel has many (historical) data.

I use the following query to get the last historical data for one device and all it’s related channels:

SELECT c.channel_id, c.channel, max(h.date_time), h.data 
FROM devices d 
INNER JOIN channels c ON c.device_id = d.device_id 
INNER JOIN historical_data h ON h.channel_id = c.channel_id 
WHERE d.name = 'livingroom' AND d.appliance_id = '0'
AND d.sensor_type = 1 AND ( c.channel = 'ch1') 
GROUP BY c.channel
ORDER BY h.date_time, channel

The query plan looks as follows:

+----+-------------+-------+--------+-----------------------+----------------+---------+---------------------------+--------+-------------+
| id | select_type | table | type   | possible_keys         | key            | key_len | ref                       | rows   | Extra       |
+----+-------------+-------+--------+-----------------------+----------------+---------+---------------------------+--------+-------------+
|  1 | SIMPLE      | c     | ALL    | PRIMARY,device_id_idx | NULL           | NULL    | NULL                      |     34 | Using where |
|  1 | SIMPLE      | d     | eq_ref | PRIMARY               | PRIMARY        | 4       | c.device_id               |      1 | Using where |
|  1 | SIMPLE      | h     | ref    | channel_id_idx        | channel_id_idx | 4       | c.channel_id              | 322019 |             |
+----+-------------+-------+--------+-----------------------+----------------+---------+---------------------------+--------+-------------+
3 rows in set (0.00 sec)

The above query is currently taking approximately 15 secs and I wanted to know if there are any tips or way to improve the query?

Edit:
Example data from historical_data

+---------------------+------------+------+------+
| date_time           | channel_id | data | unit |
+---------------------+------------+------+------+
| 2011-11-20 21:30:57 |         34 | 23.5 | C    |
| 2011-11-20 21:30:57 |          9 |   68 | W    |
| 2011-11-20 21:30:54 |         34 | 23.5 | C    |
| 2011-11-20 21:30:54 |          5 |  316 | W    |
| 2011-11-20 21:30:53 |         34 | 23.5 | C    |
| 2011-11-20 21:30:53 |          2 |   34 | W    |
| 2011-11-20 21:30:51 |         34 | 23.4 | C    |
| 2011-11-20 21:30:51 |          9 |   68 | W    |
| 2011-11-20 21:30:49 |         34 | 23.4 | C    |
| 2011-11-20 21:30:49 |          4 |  193 | W    |
+---------------------+------------+------+------+
10 rows in set (0.00 sec)

Edit 2:
Mutliple channel SELECT example:

SELECT c.channel_id, c.channel, max(h.date_time), h.data 
FROM devices d 
INNER JOIN channels c ON c.device_id = d.device_id 
INNER JOIN historical_data h ON h.channel_id = c.channel_id 
WHERE d.name = 'livingroom' AND d.appliance_id = '0'
AND d.sensor_type = 1 AND ( c.channel = 'ch1' OR c.channel = 'ch2' OR c.channel = 'ch2') 
GROUP BY c.channel
ORDER BY h.date_time, channel

I’ve used OR in the c.channel where clause because it was easier to generated pro grammatically but it can be changed to use IN if necessary.

Edit 3:
Example result of what I’m trying to achieve:

+-----------+------------+---------+---------------------+-------+
| device_id | channel_id | channel | max(h.date_time)    | data  |
+-----------+------------+---------+---------------------+-------+
|        28 |          9 | ch1     | 2011-11-21 20:39:36 |     0 |
|        28 |         35 | ch2     | 2011-11-21 20:30:55 | 32767 |
+-----------+------------+---------+---------------------+-------+

I have added the device_id to the example but my select will only need to return channel_id, channel, last date_time i.e max and the data. The results should be the last record from the historical_data table for each channel for one device.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:51:15+00:00Added an answer on May 27, 2026 at 1:51 am

    It seems that removing an re-creating the index on date_time by deleting and creating it again sped up my original SQL up to around 2secs

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
I have two tables with the following schema: CREATE TABLE sales_data ( sales_time date
I have got the following simplified schema: CREATE TABLE [file] ( id UNIQUEIDENTIFIER NOT
In a table I have the following schema table1: playerID int primary key, nationalities
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
Given the following schema: CREATE TABLE players ( id BIGINT PRIMARY KEY, name TEXT
I have the following schema, which I've simplified slightly: CREATE TABLE [dbo].[Header] ( [HeaderId]
I have a table created with the following schema: CREATE TABLE [dbo].[Visualizations] ( VisualizationID
Say I have a table with the following schema: CREATE TABLE tasks (taskID TEXT,
My Schema I have the following tables table notes/example values ------------------------------------------------ users ( id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.