0

我正在尝试使用以下方法根据事件的年龄为事件分配一个值:

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;

这个想法是,老大哥表上的旧“事件”需要从排名查询计算中减去不同的值。上述查询中的 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

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

4

0 回答 0