我在视图中使用了以下查询:
select `a`.`device_id` AS `device_id`,
`a`.`alias` AS `alias`,
`a`.`freq` AS `freq`,
`a`.`gateway` AS `gateway`,
`a`.`device_lat` AS`device_lat`,
`a`.`device_long` AS `device_long`,
`a`.`device_disabled` AS `device_disabled`,
count(`b`.`msg_id`) AS `total_messages`,
avg(`b`.`rssi`) AS `avg_rssi`,
max(`b`.`db_timestamp`) AS `last_active`,
(now() <= (max(`b`.`db_timestamp`) + interval 3 hour)) AS `device_status`
from `demo`.`lora_device` `a`
left join `demo`.`lora_message` `b` on `a`.`device_id` = `b`.`eui`
group by `a`.`device_id`
此查询大约需要4秒钟加载,有关标签的信息:
lora_message:25k行约20列
lora_device:520行约10列
通常我会说这对mysql来说不是问题,但由于某种原因它会变得很慢。
答案 0 :(得分:1)
尝试添加索引
create index ix_loramessage_rssi on lora_message(eui, rssi)
create index ix_loramessage_db_timestamp on lora_message(eui, db_timestamp)
并使用
count(`b`.`rssi`) AS `total_messages`,
而不是
count(`b`.`msg_id`) AS `total_messages`,
因为它应该在您的查询中返回相同的结果