count 的语句优化问题

13 小时 43 分钟前
 supersf
最近在 mysql 中做了一个查询

select count(*) from
(select a,b,count(distinct c) from table group by a,b having count(distinct c)>=2) as t

把 mysql 库跑死掉了

请教下有什么可以优化的空间?
目前想到的是下面

select count(*) from
(select a,b from table group by a,b having count(distinct c)>=2) as t
380 次点击
所在节点    MySQL
2 条回复
supersf
13 小时 27 分钟前
Gemini 反馈可以用 min 和 max SELECT
count(*)
FROM
(
SELECT
a,
b
FROM
table
GROUP BY
a,
b
HAVING
min(c) <> max(c)
) AS t;
lyis
8 分钟前
1.加索引 (a,b,c)
2.试试
SELECT a,b FROM table T1 WHERE EIXSTS(
SELECT 1 FRO table T2 WHERE T1.a=T2.a and T1.b=T2.b and T1.c<>T2.c
)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1158628

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX