AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 7763
Accepted
gbn
gbn
Asked: 2011-11-12 02:16:04 +0800 CST2011-11-12 02:16:04 +0800 CST 2011-11-12 02:16:04 +0800 CST

MySQL聚合异常

  • 772

我正在检查某些列对索引的选择性。
这种“忽略我给你的”行为记录在哪里?

这给出了 4,851,908、4,841,060 和 1,000,052

SELECT
     COUNT(*), 
     COUNT(DISTINCT Col1), COUNT(DISTINCT Col2)
FROM Sometable;

根据 MySQL 扩展,这给出了 4,843,634 个唯一对

SELECT COUNT(DISTINCT Col1, Col2) FROM Sometable

以下是错误的:无论任何填充列或表达式顺序如何,单个 COUNT(DISTINCT colx) 都给出 4,843,634 个唯一对计数。

我预计COUNT(DISTINCT Col1) = 4,841,060,和COUNT(DISTINCT Col1) = 1,000,052。

SELECT COUNT(DISTINCT Col1), COUNT(DISTINCT Col2) FROM Sometable

SELECT COUNT(DISTINCT Col2), COUNT(DISTINCT Col1) FROM Sometable

SELECT COUNT(DISTINCT Col1), 1 AS Filler, COUNT(DISTINCT Col2) FROM Sometable

但这又用另一个聚合给出了正确的值(就像COUNT(*)上面一样)

SELECT COUNT(DISTINCT Col1), MAX(col1) AS Filler, COUNT(DISTINCT Col2) FROM Sometable

问题,如果不清楚:

  • 为什么COUNT(DISTINCT Col1), COUNT(DISTINCT Col2)表现得像COUNT(DISTINCT Col1, Col2)
  • 为什么需要另一个聚合才能使其工作?
mysql aggregate
  • 2 2 个回答
  • 295 Views

2 个回答

  • Voted
  1. Best Answer
    Jack Douglas
    2011-11-13T08:00:22+08:002011-11-13T08:00:22+08:00

    看起来您遇到了这个回归错误:

    select count(distinct N1), count(distinct N2) from test.AA” 工作不正确
    ......
    “当存在唯一索引时会发生此错误”

    建议的解决方法之一是使用sql_buffer_result

    • 7
  2. Aaron Brown
    2011-11-13T05:21:04+08:002011-11-13T05:21:04+08:00

    没有看到您的确切结果,我不确定我是否了解问题所在。我在我机器上的随机表上尝试了这个,并得到了我预期的结果。

    mysql> select count(*), count(distinct location_country), count(distinct referer_name) from piwik_log_visit;
    +----------+----------------------------------+------------------------------+
    | count(*) | count(distinct location_country) | count(distinct referer_name) |
    +----------+----------------------------------+------------------------------+
    |    44176 |                              109 |                          291 |
    +----------+----------------------------------+------------------------------+
    1 row in set (0.81 sec)
    
    
    mysql> select count(distinct location_country,referer_name) from piwik_log_visit;
    +-----------------------------------------------+
    | count(distinct location_country,referer_name) |
    +-----------------------------------------------+
    |                                           932 |
    +-----------------------------------------------+
    1 row in set (0.19 sec)
    
    mysql> select count(distinct location_country), count(distinct referer_name) from piwik_log_visit;
    +----------------------------------+------------------------------+
    | count(distinct location_country) | count(distinct referer_name) |
    +----------------------------------+------------------------------+
    |                              109 |                          291 |
    +----------------------------------+------------------------------+
    1 row in set (0.16 sec)
    
    mysql> select count(distinct referer_name), count(distinct location_country) from piwik_log_visit;
    +------------------------------+----------------------------------+
    | count(distinct referer_name) | count(distinct location_country) |
    +------------------------------+----------------------------------+
    |                          291 |                              109 |
    +------------------------------+----------------------------------+
    1 row in set (0.16 sec)
    
    mysql> select count(distinct location_country), 1 as filler, count(distinct referer_name) from piwik_log_visit;
    +----------------------------------+--------+------------------------------+
    | count(distinct location_country) | filler | count(distinct referer_name) |
    +----------------------------------+--------+------------------------------+
    |                              109 |      1 |                          291 |
    +----------------------------------+--------+------------------------------+
    1 row in set (0.16 sec)
    
    mysql> select count(distinct location_country), max(location_country) as filler, count(distinct referer_name) from piwik_log_visit;
    +----------------------------------+--------+------------------------------+
    | count(distinct location_country) | filler | count(distinct referer_name) |
    +----------------------------------+--------+------------------------------+
    |                              109 | zw     |                          291 |
    +----------------------------------+--------+------------------------------+
    1 row in set (0.25 sec)
    

    你说

    这些都给了 4,843,634 这不是我要求的

    从某个表中选择计数(DISTINCT Col1),COUNT(DISTINCT Col2)

    从某个表中选择计数(DISTINCT Col2),COUNT(DISTINCT Col1)

    选择 COUNT(DISTINCT Col1), 1 AS Filler, COUNT(DISTINCT Col2) FROM Sometable

    但这没有任何意义。前两个查询应返回两列,最后一个应返回 3。

    您能否在线提供您的实际结果以及您期望看到的结果,也许我们可以弄清楚是否存在实际问题,或者您是否只是误解了某些内容。

    作为参考,我在 Percona Server 5.5.16 上运行了这个

    mysql> select @@version;
    +---------------+
    | @@version     |
    +---------------+
    | 5.5.16-55-log |
    +---------------+
    1 row in set (0.00 sec)
    

    编辑:我也在另一个有 ~5MM 行的数据集上尝试了这个,并得到了相同的结果......一切都检查过了。这是在 Percona Server 5.1.43 上

    • 0

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    您如何显示在 Oracle 数据库上执行的 SQL?

    • 2 个回答
  • Marko Smith

    如何选择每组的第一行?

    • 6 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    我可以查看在 SQL Server 数据库上运行的历史查询吗?

    • 6 个回答
  • Marko Smith

    如何在 PostgreSQL 中使用 currval() 来获取最后插入的 id?

    • 10 个回答
  • Marko Smith

    如何在 Mac OS X 上运行 psql?

    • 11 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Marko Smith

    将数组参数传递给存储过程

    • 12 个回答
  • Martin Hope
    Manuel Leduc PostgreSQL 多列唯一约束和 NULL 值 2011-12-28 01:10:21 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Stuart Blackler 什么时候应该将主键声明为非聚集的? 2011-11-11 13:31:59 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    BrunoLM Guid vs INT - 哪个更好作为主键? 2011-01-05 23:46:34 +0800 CST
  • Martin Hope
    bernd_k 什么时候应该使用唯一约束而不是唯一索引? 2011-01-05 02:32:27 +0800 CST
  • Martin Hope
    Patrick 如何优化大型数据库的 mysqldump? 2011-01-04 13:13:48 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve