2017-01-09 104 views
4

我在mysql中有一个大表,它有1300万行。为什么mysql select count(distinct user_id)返回错误的数字?

Mysql版本是5.7.10。

表如下结构:

create table table_name ( 
    user_id varchar(20) not null,  
    item_id varchar(20) not null 
); 

1.第一个SQL是:

select count(distinct user_id) from table; 

结果:760000

2.第二SQL是:

select count(1) from (select user_id from table group by user_id) a; 

结果:120000

user_id是不为每一行空

而且,正确的数字是120,000。为什么第一个sql得到错误的数字?

然后,我运行配置单元和spark-sql中的第一个sql,结果是120,000。

那么,这是一个MySQL的错误或东西可以设置使事情正确吗?

谢谢!

更新:我尝试另一台PC上,第一个SQL的结果是120,000.This时间得到正确的number.Mysql版本是26年6月5日。 所以,也许这是一个5.7.10的错误。

+2

已经有这些缺陷的1-2过去[例如这一个](http://bugs.mysql.com/bug.php?id=70038)。也许是另一个类似的错误和/或回归的例子? –

+0

MyISAM或InnoDB? – Strawberry

+0

他们都尝试过。起初,它是InnoDB,然后我改为MyISAM。第一个sql的结果总是错误的。 –

回答

0

There is当一列包括在两列唯一键中时,MySQL中的多个已知错误计数不同。

herehere