Mysql 对记录超过100万条的表进行有效查询

Mysql 对记录超过100万条的表进行有效查询,mysql,sql,Mysql,Sql,我真的很难回答以下两个问题。我有两张桌子,都有超过一百万张唱片。第一个查询运行30秒,第二个查询运行7分钟以上 基本上,我希望根据批次获得count class.id和scan_layers.id。最有效的方法是什么 选择count(class.id)、count(scan_layers.id)、Lot,从类左连接验证扫描层(class.ScanLayer=scan_layers.id)逐批分组 选择count(class.id)、count(distinct scan_layers.id)、L

我真的很难回答以下两个问题。我有两张桌子,都有超过一百万张唱片。第一个查询运行30秒,第二个查询运行7分钟以上

基本上,我希望根据批次获得count class.id和scan_layers.id。最有效的方法是什么

选择count(class.id)、count(scan_layers.id)、Lot,从类左连接验证扫描层(class.ScanLayer=scan_layers.id)逐批分组

选择count(class.id)、count(distinct scan_layers.id)、Lot,从类左侧验证按批次加入(class.ScanLayer=scan_layers.id)上的扫描层

我解释的时候,他们都给了我同样的解释

+----+-------------+--------------------+--------+---------------+------------------------+---------+----------------------------------+---------+----------------------------------------------+
| id | select_type | table              | type   | possible_keys | key                    | key_len | ref                              | rows    | Extra                                        |
+----+-------------+--------------------+--------+---------------+------------------------+---------+----------------------------------+---------+----------------------------------------------+
|  1 | SIMPLE      | class | index  | NULL          | defects_scan_layers_fk | 4       | NULL                             | 4417159 | Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | scan_layers        | eq_ref | PRIMARY       | PRIMARY                | 4       | cdb.class.ScanLayer |       1 |                                              |
+----+-------------+--------------------+--------+---------------+------------------------+---------+----------------------------------+---------+----------------------------------------------+

CREATE TABLE `class` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`ScanLayer` int(10) unsigned NOT NULL,
`Type` enum('Regular','Critical') NOT NULL DEFAULT 'Regular',
PRIMARY KEY (`id`),
UNIQUE KEY `Defect_UNIQUE` (`ScanLayer`),
KEY `class_scan_layers_fk` (`ScanLayer`),
CONSTRAINT `class_scan_layers_fk` FOREIGN KEY (`ScanLayer`) REFERENCES `scan_layers` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB


CREATE TABLE `scan_layers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`LayerInfo` int(10) unsigned NOT NULL,
`Lot` varchar(45) NOT NULL DEFAULT 'DEFAULT',
`PhysicalID` int(10) unsigned NOT NULL,
`Scanned` datetime DEFAULT NULL,
`ScannedMachine` int(10) unsigned DEFAULT NULL,
`DefectsCount` int(10) unsigned NOT NULL DEFAULT '0',
`MovesCount` int(10) unsigned NOT NULL DEFAULT '0',
`Verified` datetime DEFAULT NULL,
`VerifiedMachine` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ScanLayer_UNIQUE` (`LayerInfo`,`Lot`,`PhysicalID`),
KEY `scan_layers_layer_infos_fk` (`LayerInfo`),
KEY `scan_layers_scanned_machines_fk` (`ScannedMachine`),
KEY `scan_layers_verified_machines_fk` (`VerifiedMachine`),
KEY `scan_layers_verified` (`Verified`),
KEY `scan_layers_lot` (`Lot`),
CONSTRAINT `scan_layers_layer_infos_fk` FOREIGN KEY (`LayerInfo`) REFERENCES `layer_infos` (`id`) ON UPDATE CASCADE,
CONSTRAINT `scan_layers_scanned_machines_fk` FOREIGN KEY (`ScannedMachine`) REFERENCES `machines` (`id`) ON UPDATE CASCADE,
CONSTRAINT `scan_layers_verified_machines_fk` FOREIGN KEY (`VerifiedMachine`) REFERENCES `machines` (`id`) ON UPDATE CASCADE,
) ENGINE=InnoDB 

非常感谢

一个初始问题,为什么在一列上有两个索引?将
class\u id
添加到
scan\u layers
表中,并将其作为具有两个值的表的外键,而不是两个具有一百万个值的表,似乎更有意义values@KayNelson:这是导致搜索速度缓慢的原因吗?我得到了这个数据库结构,我不能改变它。@Laurence:恐怕我不能改变这个结构。是否只需更新select查询就可以缩短查询时间?