このクエリを高速化するように依頼されましたが、これらのテーブルの一部のインデックスが正しく設定されていないと思います。また、すべてが使用されるわけではないと思います。b/ ca関数は、インデックスを作成している列に適用されます。これらのテーブルやクエリを最適化する方法を誰かが理解できますか?このrequests
テーブルは3つの中で最大のテーブルになり、200k以上のレコードが含まれます。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