Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询视图性能低下_Mysql_Sql_View_Query Performance - Fatal编程技术网

MySQL查询视图性能低下

MySQL查询视图性能低下,mysql,sql,view,query-performance,Mysql,Sql,View,Query Performance,我在视图中使用了以下查询: 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_

我在视图中使用了以下查询:

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`
选择'a`.'device\u id`作为'device\u id`,
`a`.`alias`作为`alias`,
`a`.`freq`作为`freq`,
`a`.`gateway`作为`gateway`,
`a`.`device\u lat`作为`device\u lat`,
`a`.`device\u long`作为`device\u long`,
`a`.`device\u disabled`作为`device\u disabled`,
将(`b`.`msg\u id`)计数为`total\u messages`,
avg(`b`.`rssi`)作为`avg_rssi`,
max(`b`.`db\u timestamp`)作为`last\u active`,
(现在()尝试添加索引

 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`,

因为它应该在您的查询中返回相同的结果

谢谢!我已经添加了索引,现在查询将在0.05秒后运行。