设为首页 加入收藏

TOP

Mycat(6):聊天消息表,按月分表java客户端跨月查询数据(三)
2015-11-21 01:32:06 来源: 作者: 【 】 浏览:3
Tags:Mycat 聊天 消息 java 客户端 查询 数据
try { //如果id == 0就是按照id倒叙查询。 if (id == 0) { String sql = "select `id`,`gid`,`content`,`create_time`,`create_month` from msg where gid = ? and create_month = ? order by id desc limit ? "; preparedStatement = connect .prepareStatement(sql); preparedStatement.setInt(1, gid); preparedStatement.setInt(2, month); preparedStatement.setInt(3, limit); } else {// String sql = "select `id`,`gid`,`content`,`create_time`,`create_month` from msg where gid = ? and create_month = ? and id < ? order by id desc limit ? "; preparedStatement = connect .prepareStatement(sql); preparedStatement.setInt(1, gid); preparedStatement.setInt(2, month); preparedStatement.setInt(3, id); preparedStatement.setInt(4, limit); } resultSet = preparedStatement.executeQuery(); int lastId = id; while (resultSet.next()) { int id2 = resultSet.getInt("id"); //设置最后查询id。 lastId = id2; int gid2 = resultSet.getInt("gid"); String content = resultSet.getString("content"); java.util.Date create_time = resultSet.getDate("create_time"); int create_month = resultSet.getInt("create_month"); Msg msg = new Msg(); msg.setId(id2); msg.setGid(gid2); msg.setContent(content); msg.setCreateTime(create_time); msg.setCreateMonth(create_month); //增加数据到list。 list.add(msg); } //非常重要的,如果id > 1,且当月没有查询到数据,查询前一个月的数据,直到id = 1 为止。 if (lastId > 1 && list.size() < limit && month >= 201501) { //剩余数据 int remainSize = limit - list.size(); //使用递归进行查询。month-1 是简单操作,实际应该用Date返回前一个月。 List remainList = selectByGidMonth(gid, month - 1, lastId, remainSize); list.addAll(remainList); } } catch (Exception e) { e.printStackTrace(); } return list; } private static void testSelect() { //假设分页是20 条记录。 int page = 20; int lastId = 0; List list = selectByGidMonth(99, 201504, lastId, page); for (Msg msg : list) { System.out.println(msg); lastId = msg.getId(); } System.out.println("###########################"); list = selectByGidMonth(99, 201503, lastId, page); for (Msg msg : list) { System.out.println(msg); lastId = msg.getId(); } System.out.println("###########################"); list = selectByGidMonth(99, 201503, lastId, page); for (Msg msg : list) { System.out.println(msg); lastId = msg.getId(); } System.out.println("###########################"); list = selectByGidMonth(99, 201502, lastId, page); for (Msg msg : list) { System.out.println(msg); lastId = msg.getId(); } System.out.println("###########################"); list = selectByGidMonth(99, 201501, lastId, page); for (Msg msg : list) { System.out.println(msg); lastId = msg.getId(); } } public static void main(String[] args) { init(); //testInsert(); testSelect(); close(); } }

java客户端调用说明,首先msg表的id是按照gid连续自增的,如果id > 1,且当月没有查询到数据,查询前一个月的数据,直到id = 1 为止。

 if (lastId > 1 && list.size() < limit && month >= 201501) { //剩余数据 int remainSize = limit - list.size(); //使用递归进行查询。month-1 是简单操作,实际应该用Date返回前一个月。 List
                   
                     remainList = selectByGidMonth(gid, month - 1, lastId, remainSize); list.addAll(remainList); }
                   

使用递归函数往前一个月一个月查询数据,直到查询到id = 1 为止。查询结果如下,每次显示20条数据,插入的100 条 % 28 分别插入4个月数据。
查询结果可以跨月查询:

Msg{id=99, gid=99, content='test content 99', createTime=3915-07-08, createMonth=201504} Msg{id=98, gid=99, content='test content 98', createTime=3915-07-07, createMonth=201504} Msg{id=97, gid=99, content='test content 97', createTime=3915-07-06, createMonth=201504} Msg{id=96, gid=99, content='test content 96', createTime=3915-07-05, createMonth=201504} Msg{id=95, gid=99, content='test content 95', createTime=3915-07-04, createMonth=201504} Msg{id=94, gid=99, content='test content 94', createTime=3915-07-03, createMonth=201504} Msg{id=93, gid=99, content='test content 93', createTime=3915-07-02, createMonth=201504} Msg{id=92, gid=99, content='test content 92', createTime=3915-07-01, createMonth=201504} Msg{id=91, gid=99, content='test content 91', createTime=3915-06-30, createMonth=201504} Msg{id=90, gid=99, content='test content 90', createTime=3915-06-29, createMonth=201504} Msg{id=89, gid=99, content='test content 89', createTime=3915-06-28, createMonth=201504} Msg{id=88, gid=99, content='test content 88', createTime=3915-06-27, createMonth=201504} Msg{id=87, gid=99, content='test content 87', createTime=3915-06-26, createMonth=201504} Msg{id=86, gid=99, content='test content 86', createTime=3915-06-25, createMonth=201504} Msg{id=85, gid=99, content='test content 85', createTime=3915-06-24, createMonth=201504} Msg{id=84, gid=99, content='test content 84', createTime=3915-06-23, createMonth=201504} Msg{id=83, gid=99, content='test content 83', createTime=3915-05-22, createMonth=201503} Msg{id=82, gid=99, content='test content 82', createTime=3915-05-21, createMonth=201503} Msg{id=81, gid=99, content='test content 81', createTime=3915-05-20, createMonth=201503} Msg{id=80, gid=99, content='test content 80', createTime=3915-05-19, createMonth=201503} ########################### Msg{id=79, gid=99, content='test content 79', createTime=3915-05-18, createMonth=201503} Msg{id=78, gid=99, content='test content 78', createTime=3915-05-17, createMonth=201503} Msg{id=77, gid=99, content='test content 77', createTime=3915-05-16, createMonth=201503} Msg{id=76, gid=99, content='test content 76', createTime=3915-05-15, createMonth=201503} Msg{id=75, gid=99, content='test content 75', createTime=3915-05-14, createMonth=201503} Msg{id=74, gid=99, content='test content 74', createTime=3915-05-13, createMonth=201503} Msg{id=73, gid=99, content='test content 73', createTime=3915-05-12, createMonth=201503} Msg{id=72, gid=99, content='test content 72', createTime=3915-05-11, createMonth=201503} Msg{id=71, gid=99, content='test content 71', createTime=3915-05-10, createMonth=201503} Msg{id=70, gid=99, content='test content 70', createTime=3915-05-09, createMonth=201503} Msg{id=69, gid=99, content='test content 69', createTime=3915-05-08, createMonth=201503} Msg{id=68, gid=99, content='test content 68', createTime=3915-05-07, createMonth=201503} Msg{id=67, gid=99, content='test content 67', createTime=3915-05-06, createMonth=201503} Msg{id=66, gid=99, content='test content 66', createTime=3915-05-05, createMonth=201503} Msg{id=65, gid=99, content='test content 65', createTime=3915-05-04, createMonth=201503} Msg{id=64, gid=99, content='test content 64', createTime=3915-05-03, createMonth=201503} Msg{id=63, gid=99, content='test content 63', createTime=3915-05-02, createMonth=201503} Msg{id=62, gid=99, content='test content 62', createTime=3915-05-01, createMonth=201503} Msg{id=61, gid=99, content='test content 61', createTime=3915-04-30, createMonth=201503} Msg{id=60, gid=99, content='test content 60', createTime=3915-04-29, createMonth=201503} ########################### Msg{id=59, gid=99, content='test content 59', createTime=3915-04-28, createMonth=201503} Msg{id=58, gid=99, content='test content 58', createTime=3915-04-27, createMonth=201503} Msg{id=57, gid=99, content='test content 57', createTime=3915-04-26, createMonth=201503} Msg{id=56, gid=99, content='test content 56', createTime=3915-04-25, createMonth=201503} Msg{id=55, gid=99, content='test content 55', createTime=3915-03-27, createMonth=201502} Msg{id=54, gid=99, content='test content 54', createTime=3915-03-26, createMonth=201502} Msg{id=53, gid=99, content='test content 53', createTime=3915-03-25, createMonth=201502} Msg{id=52, gid=99, content='test content 52', createTime=3915-03-24, createMonth=201502} Msg{id=51, gid=99, content='test content 51', createTime=3915-03-23, createMonth=201502} Msg{id=50, gid=99, content='test content 50', createTime=3915-03-22, createMonth=201502} Msg{id=49, gid=99, content='test content 49', createTime=3915-03-21, createMonth=201502} Msg{id=48, gid=99, content='test content 48', createTime=3915-03-20, createMonth=201502} Msg{id=47, gid=99, content='test content 47', createTime=3915-03-19, createMonth=201502} Msg{id=46, gid=99, content='test content 46', createTime=3915-03-18, createMonth=201502} Msg{id=45, gid=99, content='test content 45', createTime=3915-03-17, createMonth=201502} Msg{id=44, gid=99, content='test content 44', createTime=3915-03-16, createMonth=201502} Msg{id=43, gid=99, content='test content 43', createTime=3915-03-15, createMonth=201502} Msg{id=42, gid=99, content='test content 42', createTime=3915-03-14, createMonth=201502} Msg{id=41, gid=99, content='test content 41', createTime=3915-03-13, createMonth=201502} Msg{id=40, gid=99, content='test content 40', createTime=3915-03-12, createMonth=201502} ########################### Msg{id=39, gid=99, content='test content 39', createTime=3915-03-11, createMonth=201502} Msg{id=38, gid=99, content='test content 38', createTime=3915-03-10, createMonth=201502} Msg{id=37, gid=99, content='test content 37', createTime=3915-03-09, createMonth=201502} Msg{id=36, gid=99, content='test content 36', createTime=3915-03-08, createMonth=201502} Msg{id=35, gid=99, content='test content 35', createTime=3915-03-07, createMonth=201502} Msg{id=34, gid=99, content='test content 34', createTime=3915-03-06, createMonth=201502} Msg{id=33, gid=99, content='test content 33', createTime=3915-03-05, createMonth=201502} Msg{id=32, gid=99, content='test content 32', createTime=3915-03-04, createMonth=201502} Msg{id=31, gid=99, content='test content 31', createTime=3915-03-03, createMonth=201502} Msg{id=30, gid=99, content='test content 30', createTime=3915-03-02, createMonth=201502} Msg{id=29, gid=99, content='test content 29', createTime=3915-03-01, createMonth=201502} Msg{id=28, gid=99, content='test content 28', createTime=3915-02-28, createMonth=201502} Msg{id=27, gid=99, content='test content 27', createTime=3915-01-27, createMonth=201501} Msg{id=26, gid=99, content='test content 26', createTime=3915-01-26, createMonth=201501} Msg{id=25, gid=99, content='test content 25', createTime=3915-01-25, createMonth=201501} Msg{id=24, gid=99, content='test content 24', createTime=3915-01-24, createMonth=201501} Msg{id=23, gid=99, content='test content 23', createTime=3915-01-23, createMonth=201501} Msg{id=22, gid=99, content='test content 22', createTime=3915-01-22, createMonth=201501} Msg{id=21, gid=99, content='test content 21', createTime=3915-01-21, createMonth=201501} Msg{id=20, gid=99, content='test content 20', createTime=3915-01-20, createMonth=201501} ########################### Msg{id=19, gid=99, content='test content 19', createTime=3915-01-19, createMonth=201501} Msg{id=18, gid=99, content='test content 18', createTime=3915-01-18, createMonth=201501} Msg{id=17, gid=99, content='test content 17', createTime=3915-01-17, createMonth=201501} Msg{id=16, gid=99, content='test content 16', createTime=3915-01-16, createMonth=201501} Msg{id=15, gid=99, content='test content 15', createTime=3915-01-15, createMonth=201501} Msg{id=14, gid=99, content='test content 14', createTime=3915-01-14, createMonth=201501} Msg{id=13, gid=99, content='test content 13', createTime=3915-01-13, createMonth=201501} Msg{id=12, gid=99, content='test content 12', createTime=3915-01-12, createMonth=201501} Msg{id=11, gid=99, content='test content 11', createTime=3915-01-11, createMonth=201501} Msg{id=10, gid=99, content='test content 10', createTime=3915-01-10, createMonth=201501} Msg{id=9, gid=99, content='test content 9', createTime=3915-01-09, createMonth=201501} Msg{id=8, gid=99, content='test content 8', createTime=3915-01-08, createMonth=201501} Msg{id=7, gid=99, content='test content 7', createTime=3915-01-07, createMonth=201501} Msg{id=6, gid=99, content='test content 6', createTime=3915-01-06, createMonth=201501} Msg{id=5, gid=99, content='test content 5', createTime=3915-01-05, createMonth=201501} Msg{id=4, gid=99, content='test content 4', createTime=3915-01-04, createMonth=201501} Msg{id=3, gid=99, content='test content 3', createTime=3915-01-03, createMonth=201501} Msg{id=2, gid=99, content='test content 2', createTime=3915-01-02, createMonth=201501} Msg{id=1, gid=99, content='test content 1', createTime=3915-01-01, createMonth=201501}

5,总结

mycat可以支持按月插入数据,但是查询起来要自己做好分月查询方案。
由于用户插入的数据有可能分散在多个月的数据表中,查询的时候需倒序一个月一个月的查询。
数据的存储可以按照年,500G数据放到一个磁盘,一年增加一个磁盘,新数据都写到新磁盘上面,保证数据随着时间增长只需要新增加数据库和磁盘即可,不需要进行数据迁移。

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇oracle查询数据库名、实例名等 下一篇后台管理用户权限数据库设计

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: