简体   繁体   English

MySQL查询查看性能下降

[英]MySQL query for view slow performance

I have the following query used in a view:我在视图中使用了以下查询:

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`

This query takes about 4 seconds to load, info about the tabes:此查询需要大约 4 秒才能加载,有关标签的信息:

lora_message : 25k rows about 20 columns lora_message : 25k 行约 20 列

lora_device : 520 rows about 10 columns lora_device :520 行约 10 列

Usually I would say this would not be a problem for mysql, but for some reason it is going very slow.通常我会说这对 mysql 来说不是问题,但由于某种原因,它运行得很慢。

Try to add indexes尝试添加索引

 create index ix_loramessage_rssi on lora_message(eui, rssi)
 create index ix_loramessage_db_timestamp on lora_message(eui, db_timestamp)

and use并使用

 count(`b`.`rssi`) AS `total_messages`,

instead of代替

 count(`b`.`msg_id`) AS `total_messages`,

since it should return the same result in your query因为它应该在您的查询中返回相同的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM