一個千萬級排行榜怎麼實現,每個用戶都有自己得名次,同時展示前100名

user_score表

create table user_score
(
    id      int auto_increment
        primary key,
    score   int default 0  not null,
    user_id int default 12 not null,
    constraint user_score_id_uindex
        unique (id)
);

方案1:冷熱數據+redis有序集合

方案2:指定一個冷時間段,處理排名,前一百單存一個redis key,然後各自用戶各自存一個redis key,用sh或php命令處理,不會中斷(key value存json,外面用腳本跳錶去排序)

跳錶實現:http://www.xiaomlove.com/2020/03/19/skip-list-implement-by-php/

方案3:sql

select *
from (select row_number() over (order by total desc) as number, user_id, total
      from (SELECT SUM(score) AS total,
                   user_id
            FROM user_score
            GROUP BY user_id
           ) t) s
where number <= 3;


如需知道自己名詞,用union all合併前3名和自己的數據
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章