最代码官方2015-12-12 19:56:20
原证精
最代码网站用户私信列表采用mysql union查询优化为Redis查询的经验和相关代码片段分享
由于用户和私信的数据量逐渐增加,查询用户和其他用户的私信合并排重排序的sql语法给mysql带来了很大的压力,springdata jpa的hql查询语法如下:
select id from (select id,target_id,case when user_id=?1 and type=?2 then 'sender' else 'receiver' end flag from javaniu_post where user_id=?1 and type=?2 union select id,user_id,case when user_id=?1 then 'sender' else 'receiver' end flag from javaniu_post where target_id=?1 and type=?2 order by id desc) as ret group by target_id order by id desc
在mysql的slow查询中经常会出现10s以上的记录:
mysql查询性能的瓶颈在于某个用户和其他用户之间交互越多,发送私信越多,mysql union查询参与运算的集合数据量会越大,性能也会越来越低。
数据变化如下:
1.用户A发给用户B生成私信1
用户A->用户B->私信1
2.用户A发给用户C生成私信2
用户A->用户C->私信2
3.用户A发给用户E生成私信3
用户A->用户E->私信3
4.查询用户A的私信列表
私信3,私信2,私信1
5.用户B发给用户A生成私信4
用户B->用户A->私信4
6.查询用户A的私信列表
私信4,私信3,私信2
私信1因为有最新数据私信4的存在,所以不会返回。
所以迫切需要引入新的技术来解决该查询的性能瓶颈,想到了流行的redis技术,于是修改设计如下:
模拟发私信的数据结构如下
127.0.0.1:6379> zadd uid1 1 2 (integer) 1 127.0.0.1:6379> zadd uid1 2 3 (integer) 1 127.0.0.1:6379> zadd uid1 3 5 (integer) 1 127.0.0.1:6379> zrevrange uid1 0 -1 1) "5" 2) "3" 3) "2" 127.0.0.1:6379> zrevrange uid1 0 -1 withscores 1) "5" 2) "3" 3) "3" 4) "2" 5) "2" 6) "1" 127.0.0.1:6379> zadd uid1 4 2 (integer) 0 127.0.0.1:6379> zrevrange uid1 0 -1 withscores 1) "2" 2) "4" 3) "5" 4) "3" 5) "3" 6) "2" 127.0.0.1:6379>
注意:把私信id做为score来做排序
相关代码片段如下:
删除或添加私信时:
String uid = t.getUserId() + ""; String type = ModuleConstants.POST_TYPE_MESSAGE + ""; String tgid = t.getTargetId() + ""; long id = t.getId(); long time = t.getCreateTime().getTime(); String po_uid_tp_tgid = String.format( RedisConstants.POST_USERID_TYPE_TARGETID, uid, type, tgid); // from uid String po_uid_tp = String.format(RedisConstants.POST_USERID_TYPE, uid, type); // to uid String po_tgid_tp = String.format(RedisConstants.POST_USERID_TYPE, tgid, type); if (t.getStatus() == ModuleConstants.MODULE_STATUS_DELETED) {// 删除私信同时要删除redis zsetOps.remove(po_uid_tp_tgid, id + ""); zsetOps.remove(po_uid_tp, tgid); zsetOps.remove(po_tgid_tp, uid); } else { zsetOps.add(po_uid_tp_tgid, id + "", time); zsetOps.add(po_uid_tp, tgid, id); zsetOps.add(po_tgid_tp, uid, id); }
查询用户的私信时:
String uid = userId + ""; String tp = ModuleConstants.POST_TYPE_MESSAGE + ""; String po_uid_tp = String.format(RedisConstants.POST_USERID_TYPE, uid, tp); int total = zsetOps.zCard(po_uid_tp).intValue(); int ps = total / count + (total % count > 0 ? 1 : 0); if (page > ps) { page = ps; } int start = (page - 1) * count; int end = page * count - 1; Set<TypedTuple<String>> _ids = zsetOps.reverseRangeWithScores( po_uid_tp, start, end); List<Long> ids = new ArrayList<Long>(); Iterator<TypedTuple<String>> iterator = _ids.iterator(); while (iterator.hasNext()) { TypedTuple<String> _id = iterator.next(); long id = _id.getScore().longValue(); ids.add(id); } List<Post> ts = (List<Post>) findAllByIds(ids); Pageable pageable = new PageRequest(page - 1, count); Page<Post> _page = new PageImpl<Post>(ts, pageable, total); initBeans(_page.getContent());
redis查询最代码官方,id=1的所有私信列表,和mysql union运算的结果完全一致,但是性能可是天壤之别:
相关资料和代码:
猜你喜欢
请下载代码后再发表评论
- 证HTML5+jQuery制作温馨浪漫爱心表白动画特效
- 精Apache Shiro+SpringMVC+Hibernate Search+Hibernate+Bootstrap企业信息管理系统基础框架搭建整合实例代码教程
- 证精spring MVC+easyUI+mybatis开发网站后台管理系统源代码下载
- 一套EASYUI的经典后台管理模板
- 证精java开源CMS管理系统jeetemp
- 精html网站内容管理系统后台模板源代码下载,花钱买的模板。
- 原精spring mvc+spring+mybatis+shiro+easyui整合开发后台用户权限管理系统
- 一款漂亮的轻量级bootstrap中文后台管理系统模板ace
- js框架jquery实现幸运大转盘抽奖程序代码,兼容多种浏览器
- 证HTML5音乐播放器效果非常漂亮
- 证精企业人力资源管理项目SSH+EXTJS+MySQL整合开发
- 证最新国外极品后台管理系统模板ace admin v1.3,多种开发语言可用,HTML5+Bootstrap3.0
- 证精java开源CMS管理系统jeetemp
- java牛官方想开发一个全部开源的系统
- 证精企业人力资源管理项目SSH+EXTJS+MySQL整合开发
- 精基于springMVC+springSecurity3.x+Mybaits3.x的权限系统,,开放源码,支持开源
- 证精spring MVC+easyUI+mybatis开发网站后台管理系统源代码下载
- 【猪猪-前端】基于HTML5 Bootstrap搭建的后台模板charisma,分页,模糊查询已经全部JS实现,无需编码,嵌入数据即可开发,内置8款皮肤,欧美风格,非常好用!
- 精Apache Shiro+SpringMVC+Hibernate Search+Hibernate+Bootstrap企业信息管理系统基础框架搭建整合实例代码教程
- 证精springmvc+hibernate+jbpm开发OA自动化办公后台管理系统源码下载
- 证精struts2.3+hibernate4.1+spring3.2+EasyUI1.36整合实现的java后台管理系统
- 证HTML5+jQuery制作温馨浪漫爱心表白动画特效
- 证精spring+spring mvc+mybatis+bootstrap框架整合搭建ssm完整项目
- 证精年末最代码部分源码大出血分享-freemarker,bootstrap,springdata jpa分页代码
- 证网站后台管理系统页面模板
- 原证html实战-制作静态网页
- 原精java web开发在线电商购物车系统(jsp+jdbc+spring mvc+bootstrap)
- ssm整合bootstrap开发网站用户信息管理系统(增删改查)
- ssm框架整合bootstrap开发图书馆图书预约管理系统
- 原XML文件解析java工具类dom4J使用实例
- 原证ssm+bootstrap开发小型java web教务后台管理系统
- php蓝天采集器 云端数据采集发布系统 SkyCaiji
- 证SSM通用教师教学管理系统
- 原java开源网页分析工具htmlunit爬虫抓取搜狐图片到本地的实例
- 证html5+CSS3开发高端大气手机网站首页
- 原证javascript实现推箱子网页小游戏(非常值得学习)



chasing future3月2日
暂无贡献等级
zrxjava2月28日
wujianlei2月28日
暂无贡献等级
liu0012月23日
suncy20052月12日
暂无贡献等级
leoxxxx2月12日
han1082月8日
lyad12345672月7日
暂无贡献等级
沙漏2月7日
Smail_ LV32月7日