› 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
0.32D
V2EX  ›  MySQL

count 的语句优化问题

  •  
  •   supersf · Sep 11, 2025 · 4558 views
    This topic created in 384 days ago, the information mentioned may be changed or developed.
    最近在 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 replies  •  2025-09-12 09:17:15 +08:00
    supersf
        1
    supersf  
    OP
       Sep 11, 2025 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  
       Sep 12, 2025
    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  
       Sep 12, 2025
    数据有多少?按 2 楼的方法,给 abc 加个索引试试
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   788 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 20:47 · PVG 04:47 · LAX 13:47 · JFK 16:47
    ♥ Do have faith in what you're doing.