我有一个包含大量数据和几个索引列的表,我需要运行一个查询来计算有多少条记录几乎具有重复的数据。这就是我所说的几乎重复的数据:有一个用户表,其中有一列电话号码,有时这些号码带有额外的前缀,但我知道号码 CC123456
和 CCX123456
(CC 是国家/地区代码,X 是额外前缀)相同。
我最初的想法是使用子查询,但它运行了 25 分钟,然后 mysql 工作台就退出了,所以我认为应该有更好的方法来做到这一点。我尝试的查询类似于
/* CC is once again the country code and X is the extra prefix */
SELECT COUNT(*)
FROM users
WHERE CHAR_LENGTH(phone_number) = 13 AND
phone_number LIKE 'CCX%' AND
phone_number IN (
SELECT CONCAT(CC, SUBSTRING(phone_number FROM 3))
FROM users
WHERE CHAR_LENGTH(phone_number) = 12 AND
phone_number LIKE 'CC%'
);
有人知道我怎样才能做得更好吗?
编辑:我对查询运行了 EXPLAIN,这是结果。 u1 和 u2 只是表的别名,id_store 和 email 只是索引列
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
'1' | 'PRIMARY' | 'u1' | 'range' | 'id_store,id_store_email' | 'id_store' | '31' | NULL | '37604' | 'Using where; Using index'
'2' | 'DEPENDENT SUBQUERY' | 'u2' | 'range' | 'id_store,id_store_email' | 'id_store' | '31' | NULL | '4881464' | 'Using where; Using index'
最佳答案
进行自加入怎么样?类似这样的事情(未在 MySQL 中测试):
SELECT COUNT(*)
FROM users U, users S
WHERE substring(U.phone_number,1,3)='CCX' and substring(S.phone_number,1,2)='CC' and not(substring(S.phone_number,3,1) = 'X')
and substring(U.phone_number,4,10) = substring(S.phone_number,3,10)
关于mysql - 如何改进计数查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20805225/