[英]Mysql BIgQuery to one to many relationship
I had create tables with one to many relationship , size of rows 11328441
. 我创建了具有一对多关系,行大小为
11328441
。 The table structure is 表结构是
CREATE TABLE `gameresnum` (
`ID` bigint(20) NOT NULL,
`DRAWDAY` date DEFAULT NULL,
`DRAWNO` int(11) DEFAULT NULL,
`DRAWTIME` time DEFAULT NULL,
`RESNUM` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `gameresnum`
ADD PRIMARY KEY (`ID`),
ADD KEY `RESNUM` (`RESNUM`);
. 。
I tried to make this query : 我试图使这个查询:
SELECT count(*) as count FROM gameresnum gone
JOIN gameresnum gtwo ON gone.DRAWNO=gtwo.DRAWNO WHERE gone.RESNUM=1 AND gtwo.RESNUM=2
But it is very slow up to 2 mint. 但这是非常缓慢的,直到2薄荷糖。
your drawno column does not have an index. 您的drawo列没有索引。 thats why.
这就是为什么。
Your query would benefit from 您的查询将受益于
INDEX(resnum, drawno) -- composite and covering
You could also experiment with 您也可以尝试
SELECT COUNT(*)
FROM (
SELECT 1
FROM gamesresnum
WHERE resnum IN (1, 2)
GROUP BY drawno
HAVING COUNT(*) = 2 -- to see if both are present
) AS x
It needs INDEX(drawno, resnum)
. 它需要
INDEX(drawno, resnum)
。
Caveat: This reformulation assumes that (drawno, resnum)
is unique. 注意事项:这种重新定义假定
(drawno, resnum)
是唯一的。 I don't know if this will be faster. 我不知道这样会不会更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.