我的任务是帮助加快这个查询,我认为这些表上的一些索引没有正确设置。我也相信它们不会全部被使用 b/ca 函数应用于我有索引的 col。谁能看到我如何优化这些表或查询?该requests
表将是 3 个表中最大的表,将有超过 20 万条记录。devices
目前有约 500 条记录,并且clients
也会更小。
询问:
explain extended SELECT MAX(Request.datetime) AS datetime, Device.id,
Device.client_id, Device.mac_address, Device.type, Device.manufacturer,
Device.model_number, Client.id, Client.email_address,
Request.device_id, Request.datetime, Request.ip_address
FROM livefi.devices AS Device
LEFT JOIN livefi.clients AS Client
ON (Client.id = Device.client_id)
INNER JOIN livefi.requests AS Request
ON (Request.device_id = Device.id)
GROUP BY Request.device_id, Request.client_id
+----+-------------+---------+--------+---------------------------------------------------------+---------------+---------+-------------------------+------+----------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+--------+---------------------------------------------------------+---------------+---------+-------------------------+------+----------+---------------------------------+
| 1 | SIMPLE | Device | ALL | PRIMARY | NULL | NULL | NULL | 617 | 100.00 | Using temporary; Using filesort |
| 1 | SIMPLE | Client | eq_ref | PRIMARY | PRIMARY | 4 | livefi.Device.client_id | 1 | 100.00 | |
| 1 | SIMPLE | Request | ref | idx_device_id,inx_requests_deviceId_datetime_ip_address | idx_device_id | 5 | livefi.Device.id | 144 | 100.00 | Using where |
+----+-------------+---------+--------+---------------------------------------------------------+---------------+---------+-------------------------+------+----------+---------------------------------+
3 rows in set, 1 warning (0.04 sec)
表:
CREATE TABLE `clients` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email_address` varchar(100) DEFAULT NULL,
`mac_address` varchar(17) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email_address` (`email_address`),
KEY `idx_mac_address` (`mac_address`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `devices` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`client_id` int(10) unsigned DEFAULT NULL,
`mac_address` varchar(17) DEFAULT NULL,
`type` varchar(25) DEFAULT NULL,
`manufacturer` varchar(100) DEFAULT NULL,
`model_number` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `mac_address` (`mac_address`),
KEY `idx_mac_address` (`mac_address`),
KEY `fk_devices_clients1` (`client_id`),
CONSTRAINT `fk_devices_clients1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=492 DEFAULT CHARSET=utf8
CREATE TABLE `requests` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`client_id` int(10) unsigned DEFAULT NULL,
`device_id` int(10) unsigned DEFAULT NULL,
`domain_id` int(10) unsigned DEFAULT NULL,
`ip_address` varchar(15) DEFAULT NULL,
`datetime` datetime DEFAULT NULL,
`gmt_offset` time DEFAULT NULL,
`request_method` varchar(15) DEFAULT NULL,
`url` text,
`http_protocol` varchar(20) DEFAULT NULL,
`http_status_code` varchar(20) DEFAULT NULL,
`request_size` int(10) unsigned DEFAULT '0',
`referer` text,
`user_agent` text,
`squid_cache_response` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_client_id` (`client_id`),
KEY `idx_datetime` (`datetime`),
KEY `idx_device_id` (`device_id`),
KEY `idx_domain_id` (`domain_id`),
KEY `idx_id` (`id`),
KEY `idx_request_size` (`request_size`),
KEY `inx_requests_deviceId_datetime_ip_address` (`device_id`,`datetime`,`ip_address`),
CONSTRAINT `fk_requests_clients` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_requests_devices1` FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_requests_domains1` FOREIGN KEY (`domain_id`) REFERENCES `domains` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=200523 DEFAULT CHARSET=utf8