日期计算排名 - 为事件年龄分配值

时间:2012-04-25 21:24:48

标签: mysql

我正在尝试根据它的年龄为事件分配一个值:

SELECT u.iduser, timeIsImportant
FROM user AS u
LEFT OUTER JOIN(
SELECT action, user_id, action_time, 
    (IF(bb.action_time < DATE_SUB(CURDATE(), INTERVAL 7 DAY), 
      (CASE 
       WHEN bb.action_time BETWEEN DATE_SUB(CURDATE(), INTERVAL 3 MONTH) AND CURDATE() THEN 0.1 
       WHEN bb.action_time BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 MONTH) AND CURDATE() THEN 0.2
       WHEN bb.action_time BETWEEN DATE_SUB(CURDATE(), INTERVAL 12 MONTH) AND CURDATE() THEN 0.4
       WHEN bb.action_time BETWEEN DATE_SUB(CURDATE(), INTERVAL 18 MONTH) AND CURDATE() THEN 0.7
       WHEN bb.action_time BETWEEN DATE_SUB(CURDATE(), INTERVAL 24 MONTH) AND CURDATE() THEN 1.0
       END), 0)) 
AS timeIsImportant
FROM bigbrother AS bb
ORDER BY bigbrother.uid DESC LIMIT 1) 
AS bbb 
ON
bbb.user_id = u.iduser AND bbb.action = "C"
WHERE u.iduser = 2;

这个想法是,bigbrother表上较旧的'事件'需要从排名查询计算中减去不同的值。上面查询中的timeIsImportant值将是以下示例中的agePoints。

示例数据:

     row1                              row2
 -------------                    -------------
 rank: 4.7                         rank 4.9
 agePoints: 0.1                    agePoints 0.4
 timedRank: (rank-AgePoints)       timedRank: (rank-AgePoints)

 -------------------------------------------------------------

 SQL: ORDER BY timedRank DESC

 row1, row2

 SQL: ORDER BY timedRank ASC

 row2, row1

我想知道是否还有另一种基于事件年龄分配值的方法,因为我正在对每个页面加载进行此计算,以便对搜索结果进行排名,并发现这段代码速度变慢嵌套在搜索查询中时的整体性能。

0 个答案:

没有答案