V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
supersf
V2EX  ›  MySQL

count 的语句优化问题

  •  
  •   supersf · 1 天前 · 1045 次点击
    最近在 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
    3 条回复    2025-09-12 09:17:15 +08:00
    supersf
        1
    supersf  
    OP
       1 天前 via iPhone
    Gemini 反馈可以用 min 和 max SELECT
    count(*)
    FROM
    (
    SELECT
    a,
    b
    FROM
    table
    GROUP BY
    a,
    b
    HAVING
    min(c) <> max(c)
    ) AS t;
    lyis
        2
    lyis  
       23 小时 29 分钟前
    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
    )
    andykuen959595
        3
    andykuen959595  
       22 小时 49 分钟前
    数据有多少?按 2 楼的方法,给 abc 加个索引试试
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3359 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 00:06 · PVG 08:06 · LAX 17:06 · JFK 20:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.