选择非重复记录

时间:2013-12-26 13:14:05

标签: mysql sql performance select not-exists

我有一张包含大约5000万条记录的表格。

表结构如下所示, callerid call_start 字段都被编入索引。

  

id - callerid - call_start

我想选择他们的call_start大于' 2013-12-22'在2013-12-22' 2013-12-22'之前不会复制和调用在整个表格中。

我使用过这样的东西:

SELECT DISTINCT 
  ca.`callerid` 
FROM
  call_archives AS ca 
WHERE ca.`call_start` >= '2013-12-22' 
  AND ca.`callerid` NOT IN 
  (SELECT DISTINCT 
    ca.`callerid` 
  FROM
    call_archives AS ca 
  WHERE ca.`call_start` < '2013-12-21')

但这非常慢,任何建议都非常感激。

3 个答案:

答案 0 :(得分:3)

尝试使用NOT EXISTS

SELECT DISTINCT 
  ca.`callerid` 
FROM
  call_archives AS ca 
WHERE ca.`call_start` >= '2013-12-22' 
  AND NOT EXISTS 
  (SELECT 
    1 
  FROM
    call_archives AS cb 
  WHERE ca.`callerid` = cb.`callerid` 
    AND cb.`call_start` < '2013-12-21')

答案 1 :(得分:2)

只是好奇这个查询是否在您的桌面上快速运行:

SELECT ca.`callerid` 
FROM call_archives 
GROUP BY ca.`callerid` 
HAVING MIN(ca.`call_start`) >='2013-12-22' 

答案 2 :(得分:0)

使用NOT EXISTS代替NOT IN

试试这个:

SELECT DISTINCT ca.callerid 
FROM call_archives AS ca 
WHERE ca.call_start>='2013-12-22' AND 
  NOT EXISTS(SELECT 1 FROM call_archives AS ca1 
             WHERE ca.callerid = ca1.callerid AND ca1.call_start <'2013-12-21');