下面是表格结构。 MySql表包含1100万个数据。所有数据可以仅在一天内插入表中。 我尝试执行以下查询,需要5-7分钟才能完成。我已经根据查询创建了索引,甚至需要3-5分钟才能完成。正在使用索引。
我该怎么做才能提高查询性能?有什么建议吗?
CREATE TABLE `service_collection_data` (
`service_name` varchar(512) NOT NULL,
`device_id` bigint(20) NOT NULL,
`dataset_name` varchar(1024) NOT NULL,
`dataset_title` varchar(1024) NOT NULL,
`dataset_type` varchar(32) NOT NULL,
`dataset_command` varchar(4000) DEFAULT NULL,
`tag_name` varchar(4000) DEFAULT NULL,
`command_status` varchar(256) NOT NULL,
`command_result` longtext,
`command_error` longtext,
`command_context` longtext,
`collection_time` timestamp NOT NULL
DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
KEY `device_id` (`device_id`),
KEY `service_tag_command_index` (`service_name`(300),`tag_name`(200),
`command_status`(200),`device_id`),
CONSTRAINT `service_collection_data_ibfk_1`
FOREIGN KEY (`device_id`)
REFERENCES `nodes` (`id`) ON DELETE CASCADE
)
ENGINE=InnoDB DEFAULT CHARSET=latin1
以下是执行查询
SELECT COUNT(DISTINCT device_id)
FROM service_collection_data
WHERE service_name='NOS'
AND tag_name='Config'
AND command_status = 'Successful'
答案 0 :(得分:0)
作为调试方案,速度是多少:
1) SELECT COUNT(device_id) 来自service_collection_data
2) SELECT COUNT(DISTINCT device_id) 来自service_collection_data
3)
SELECT COUNT(*)
FROM service_collection_data
WHERE service_name ='NOS'
AND tag_name ='Config'
AND command_status ='成功'
4)
SELECT COUNT(DISTINCT device_id)
从
(SELECT service_name,tag_name,command_status,device_id
FROM service_collection_data
GROUP BY service_name,tag_name,command_status,device_id
WHERE service_name ='NOS'
AND tag_name ='Config'
AND command_status ='成功')
其他: