找到重新接入用户的最有效方法

时间:2013-12-12 07:12:06

标签: mysql

我想在过去24小时内吸引所有活跃用户,看看他们之前是否与我们互动过。我有以下查询:

SELECT $                                                                                                                                                                             
    DISTINCT user_id $                                                                                                                                                               
FROM $                                                                                                                                                                               
    activity $                                                                                                                                                                
WHERE user_id in (SELECT $                                                                                                                                                           
                      DISTINCT user_id $                                                                                                                                             
                  FROM $                                                                                                                                                             
                      activity $                                                                                                                                              
                  WHERE $                                                                                                                                                            
                      date > DATE_SUB(NOW(), INTERVAL 24 HOUR)) AND $                                                                                                                
      date < DATE_SUB(NOW(), INTERVAL 24 HOUR);

我有user_id和date的索引。这可以提高效率吗?

2 个答案:

答案 0 :(得分:0)

当他指示我使用EXISTS时,克里斯把我放在正确的轨道上。正确的查询是:

SELECT $                                                                                                                                                                                 DISTINCT user_id $                                                                                                                                                               FROM $                                                                                                                                                                               
    activity o $                                                                                                                                                              
WHERE $                                                                                                                                                                              
    EXISTS (SELECT $                                                                                                                                                                 
                1 $                                                                                                                                                                  
            FROM $                                                                                                                                                                   
                activity i $                                                                                                                                                  
            WHERE $                                                                                                                                                                  
                o.user_id = i.user_id AND $                                                                                                                                          
                DATEDIFF(o.date, i.date) > 0) AND $                                                                                                                                                           
    date > DATE_SUB(NOW(), INTERVAL 24 HOUR)$                       

解释所提到的行检查从20k到170

答案 1 :(得分:0)

你能试试吗?

SELECT DISTINCT user_id
FROM activity INNER JOIN
    (
        SELECT DISTINCT user_id
        FROM activity
        WHERE date > DATE_SUB(NOW(), INTERVAL 24 HOUR)
            AND date < DATE_SUB(NOW(), INTERVAL 24 HOUR
    ) x ON x.user_id = activity.user_id

或者

SELECT DISTINCT user_id
FROM activity a1
WHERE EXISTS (
    SELECT 1
    FROM activity a2
    WHERE a2.user_id = a1.user_id
      date > DATE_SUB(NOW(), INTERVAL 24 HOUR))
      AND date < DATE_SUB(NOW(), INTERVAL 24 HOUR
);