Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义排序方面的问题想请教一下 #110

Open
3DMXM opened this issue Jul 27, 2020 · 5 comments
Open

自定义排序方面的问题想请教一下 #110

3DMXM opened this issue Jul 27, 2020 · 5 comments

Comments

@3DMXM
Copy link

3DMXM commented Jul 27, 2020

事情是这样的,我自定了一套积分算法:
积分 = 浏览/2 + 点赞 + 收藏*2

然后我想让热门内容按照积分从高到低进行排序
“积分”这个没有在数据库存为字段,如果要在数据库加的话要刷10多万条数据,感觉蛮麻烦的;
如果:
浏览是“click”
点赞是“mark”
收藏是“collection”
积分存为“integral”

请问要怎么写才能让内容按照积分从高到低进行排序

@billge1205
Copy link
Collaborator

可以使用sql模版实现自定义计算后的字段orderby排序的逻辑,但不建议这么做,不命中索引的sql语句执行是非常慢的,建议脚本计算下积分再入库根据积分进行排序

@3DMXM
Copy link
Author

3DMXM commented Jul 27, 2020

但是积分是随着浏览、点赞、收藏变动而变动的,如果把积分加到字段的话,每当“浏览、点赞、收藏”变动时,都要把积分也更新一下,感觉很麻烦。
另外,sql语句我没怎么用过,如果用原生SQL是怎么写的呢

@billge1205
Copy link
Collaborator

billge1205 commented Jul 27, 2020

select (click*2+mark+collection*2) as score from xxxxx where xxxxx order by score desc

@billge1205
Copy link
Collaborator

@3DMXM
刚想了下 其实按你的逻辑 可以再设个积分字段,浏览时积分+1,点赞时+2,收藏时+4。本来就要更新的只是多更新个字段而已,通过计算排序性能肯定很差的哈

@3DMXM
Copy link
Author

3DMXM commented Jul 28, 2020

好的,谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants