gpt4 book ai didi

mysql - 简化自连接 SQL

转载 作者:行者123 更新时间:2023-11-29 04:25:27 24 4
gpt4 key购买 nike

我目前正在执行以下连接,执行查询需要很长时间,有没有办法让它运行得更快?我在表中有 23 个字段。我在字段 'id' (int) 5 上有一个主键注册 (varchar) 9

SELECT a1.*, ( SELECT COUNT(reg) FROM auction a2 WHERE reg = a1.reg) AS c1 FROM auction a1


CREATE TABLE IF NOT EXISTS `db1` (
`id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`adate` varchar(10) NOT NULL,
`lnu` int(4) NOT NULL,
`reg` varchar(9) NOT NULL,
`mk` varchar(50) NOT NULL,
`mod` varchar(50) NOT NULL,
`type` varchar(25) NOT NULL,
`ree` varchar(50) NOT NULL,
`co` varchar(50) NOT NULL,
`fu` varchar(20) NOT NULL,
`tran` varchar(20) NOT NULL,
`mt` varchar(9) NOT NULL,
`mile` int(6) NOT NULL,
`ree` int(6) NOT NULL,
`apv` int(6) NOT NULL,
`ds` int(2) NOT NULL,
`sr` varchar(50) NOT NULL,
`sen` int(10) NOT NULL,
`keep` int(2) unsigned NOT NULL,
`tmp` char(1) DEFAULT NULL,
`rk` varchar(100) DEFAULT NULL,
`st` varchar(3) DEFAULT NULL,
`dd` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `date` (`adate`)

最佳答案

从您当前的模式来看,您应该在 reg 列上提供 index

SELECT a1.*, a2.totalCount
FROM auction a1
INNER JOIN
(
SELECT reg, COUNT(*) totalCount
FROM auction a2
GROUP BY reg
) a2
ON a2.reg = a1.reg

关于mysql - 简化自连接 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12699062/

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