gpt4 book ai didi

mysql查询优化

转载 作者:搜寻专家 更新时间:2023-10-30 20:58:23 24 4
gpt4 key购买 nike

我的任务是帮助加快此查询,我认为这些表上的某些索引设置不正确。我也相信它们不会全部用于 b/c 一个函数应用于我有索引的 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

最佳答案

我建议:

ALTER TABLE requests ADD INDEX (device_id, client_id, datetime);

此外,请注意,您不应在 SELECT 子句中包含 Request.datetimeRequest.ip_address,因为它们不属于GROUP BY 子句。这是因为 requests 中的几行可能具有相同的 device_idclient_id,并且为 datetime 选择了哪个值或 ip_address 有点随机。

关于mysql查询优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13080770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com