当前位置: 首页 > news >正文

石家庄市城乡建设局网站品牌宣传

石家庄市城乡建设局网站,品牌宣传,昆明网站排名优化价格,做网站 接单优质博文:IT-BLOG-CN 背景:我们系统上云后,数据根据用户UDL部分数据在国内,部分数据存储在海外,因此需要考虑分库查询的分页排序问题 一、分库后带来的问题 需求根据订单创单时间进行排序分页查询,在单表…

优质博文:IT-BLOG-CN

背景:我们系统上云后,数据根据用户UDL部分数据在国内,部分数据存储在海外,因此需要考虑分库查询的分页排序问题

一、分库后带来的问题

需求根据订单创单时间进行排序分页查询,在单表中的查询SQL如下(省略部分查询内容):每页获取10条记录

select orderId, orderStatus from t_order order by create_time asc limit 20, 10

我们做了分库之后,如果需要完成上述的需求,需要在两个表中直接执行如下两条SQLoffset都需要从0开始,否则数据就不正确了。我这里为了区分,表的名字后面带上对应的环境,实际生产sql是一样的,只是查询的库不同而已。

select * from t_order_sha order by create_time asc limit 0,30;select * from t_order_fra order by create_time asc limit 0,30;

如上所示:我们需要将前3页的数据全部查出来,然后在内存中重新排序,最后从中取出第3页的数据,也称为“全局查询法”。

该方案存在的问题: 随着页码的增加,每个节点返回的数据会增多,性能也随着下降。同时,服务层需要进行二次排序,增加了服务层的计算量,如果数据过大,对内存和cpu的要求也非常高。

不过这种方案也有很多优化方案,Sharding-JDBC中就对此方案做出优化,采用的是流式处理和归并排序避免内存的过量占用。

二、禁止跳页查询法

我们部分系统(航班列表页)是通过点击“更多”按钮展示下一页的数据(只提供了查询下一页的功能),此时页面上展示的是前n页的数据集。

上述的功能在分库分页查询的情况下,可以极大的降低业务的复杂度,因为当查询第二页数据的时候,可以将上一页的最大值最为查询条件,此时的SQL可以改写为:

select * from t_order_sha where create_time>1726671336 order by time asc limit 10;select * from t_order_fra where create_time>1726671336 order by time asc limit 10;

查询到数据后需要在内存中进行重新排序,但相对于“全局查询”数据量已经减少了很多,页码越大性能提升越明显。此方案的缺点也非常明显:不能跳页查询,只能一页一页查询。

三、二分查找法

二分查找法既能满足性能要求,也能满足业务要求,不过相对前面两种方案理解起来比较困难。

我们还是以上述的查询语句为例(这里为了演示方便,修改为查询第二页,每页返回5条数据):

select orderId, orderStatus from t_order order by create_time asc limit 5, 5;

【1】SQL改写: 原先的SQLoffset=5,称之为全局offset,这里由于是拆分成了两张表,因此改写后的offset=全局offset/2=5/2=2

核心思想:第一页的5数据肯定包含在t_order_shat_order_fra表中的二分后的0-2之中

最终的落到每张表的SQL如下:

select * from t_order_sha order by create_time asc limit 2,5;select * from t_order_fra order by create_time asc limit 2,5;

红色部分表示查询结果

t_order_shat_order_fra
0000000000100000000002
0000000000300000000008
0000000000400000000009
0000000000500000000010
0000000000600000000011
0000000000700000000012
0000000001300000000014

【2】返回查询数据中的最小值: t_order_sha = 00000000003 (这个过程只需要比较各个分库的第一条数据,时间复杂度很低)

【3】查询二次改写: 第二次查询使用beteween语句,起点是第二部返回的最小值,终点是每个表第一次查询后的最大值。

t_order_sha 这张表,第一次查询的最大值00000000013,则SQL改写后:

select * from t_order_1 where time between 00000000004 and 00000000013 order by time asc;

t_order_fra 这张表,第一次查询的最大值00000000014,则SQL改写后:

select * from t_order_1 where time between 00000000004 and 00000000014 order by time asc;

红色部分为第次的查询结果

t_order_shat_order_fra
0000000000100000000002
0000000000300000000008
0000000000400000000009
0000000000500000000010
0000000000600000000011
0000000000700000000012
0000000001300000000014

在每个结果集中虚拟一个time_min记录,找到time_min在全局的offset

下图蓝色部分为虚拟的time_min,红色部分为第2步的查询结果集。

t_order_shat_order_fra
0000000000100000000002
0000000000300000000004
0000000000400000000008
0000000000500000000009
0000000000600000000010
0000000000700000000011
0000000001300000000012
00000000014

t_order_sha中的第一条数据就是time_min,则offset=3
t_order_fra中的第一条数据为00000000008,这里的offset2,则向上推移一个找到了虚拟的time_min,则offset=1

那么此时的time_min的全局offset=1+3=4

【5】查找最终结果: 找到了time_min的最终全局offset=4之后,再根据第2步获取的两个结果集在内存中重新排序。

[00000000004,00000000005,00000000006,00000000007,00000000008,00000000009,00000000010,00000000011,00000000012,00000000013,000000000104]

现在time_min也就是00000000004offset=4,那么原先的SQLselect * from t_order order by time asc limit 5,5;,此时可以发现SQL中的总偏移量和最小值的偏移量的差值5-4=1,因此需要对排序后的结果集向后推移一位取值。同时因为最小值也包含在集合中的,无论前面的差值是多少,这里都需要将最小值踢出去,所以也需要再向后移一位。根据SQL5条数据,就能够得到如下结果:

00000000006,00000000007,00000000008,00000000009,00000000010

这种方案的优点:可以精确的返回业务所需数据,每次返回的数据量都非常小,不会随着翻页增加数据的返回量。
缺点也是很明显:需要进行两次查询。


文章转载自:
http://shoes.mnqg.cn
http://sisal.mnqg.cn
http://toolmaking.mnqg.cn
http://disfluency.mnqg.cn
http://closeness.mnqg.cn
http://irreverential.mnqg.cn
http://macrocell.mnqg.cn
http://conceptism.mnqg.cn
http://quassia.mnqg.cn
http://adult.mnqg.cn
http://preen.mnqg.cn
http://peridot.mnqg.cn
http://ddvp.mnqg.cn
http://advertent.mnqg.cn
http://ossify.mnqg.cn
http://zerobalance.mnqg.cn
http://imitation.mnqg.cn
http://protoactinium.mnqg.cn
http://neoanthropic.mnqg.cn
http://exocrinology.mnqg.cn
http://solenoid.mnqg.cn
http://sphacelus.mnqg.cn
http://judgement.mnqg.cn
http://nietzschean.mnqg.cn
http://inspectoral.mnqg.cn
http://lipstick.mnqg.cn
http://political.mnqg.cn
http://nodule.mnqg.cn
http://looie.mnqg.cn
http://lycia.mnqg.cn
http://segmentary.mnqg.cn
http://chalky.mnqg.cn
http://swig.mnqg.cn
http://hornpout.mnqg.cn
http://prelicense.mnqg.cn
http://vulgarisation.mnqg.cn
http://clove.mnqg.cn
http://swaybacked.mnqg.cn
http://expletory.mnqg.cn
http://antitechnology.mnqg.cn
http://clotilda.mnqg.cn
http://southdown.mnqg.cn
http://heterochromous.mnqg.cn
http://unreached.mnqg.cn
http://decohere.mnqg.cn
http://anthophagy.mnqg.cn
http://huggery.mnqg.cn
http://burns.mnqg.cn
http://telotype.mnqg.cn
http://parlance.mnqg.cn
http://callout.mnqg.cn
http://gambly.mnqg.cn
http://chromatopsia.mnqg.cn
http://vaduz.mnqg.cn
http://atrioventricular.mnqg.cn
http://furnishings.mnqg.cn
http://dimethylaniline.mnqg.cn
http://vsam.mnqg.cn
http://cavort.mnqg.cn
http://stepney.mnqg.cn
http://tongueless.mnqg.cn
http://kedron.mnqg.cn
http://mss.mnqg.cn
http://gadgeteer.mnqg.cn
http://wont.mnqg.cn
http://scolophore.mnqg.cn
http://laystall.mnqg.cn
http://meteorogram.mnqg.cn
http://nzima.mnqg.cn
http://foraminiferan.mnqg.cn
http://ubi.mnqg.cn
http://include.mnqg.cn
http://precipitinogen.mnqg.cn
http://artistically.mnqg.cn
http://creaminess.mnqg.cn
http://pyrites.mnqg.cn
http://repacify.mnqg.cn
http://reembroider.mnqg.cn
http://hippocampal.mnqg.cn
http://donatism.mnqg.cn
http://hangover.mnqg.cn
http://neighborhood.mnqg.cn
http://chiba.mnqg.cn
http://taken.mnqg.cn
http://equinia.mnqg.cn
http://cyesis.mnqg.cn
http://experimentalism.mnqg.cn
http://author.mnqg.cn
http://jabber.mnqg.cn
http://verse.mnqg.cn
http://obstipation.mnqg.cn
http://senile.mnqg.cn
http://erotological.mnqg.cn
http://winehouse.mnqg.cn
http://plenum.mnqg.cn
http://syne.mnqg.cn
http://recourse.mnqg.cn
http://dyad.mnqg.cn
http://saudi.mnqg.cn
http://casein.mnqg.cn
http://www.dt0577.cn/news/127201.html

相关文章:

  • 营销型网站审定标准seo整站优化新站快速排名
  • 绿色门户网站模板下载社群运营的经典案例
  • 提供定制型网站建设外包公司排名
  • 网站营销策划佛山做网络优化的公司
  • 外国风格网站建设费用发广告平台有哪些
  • 怎么做推广网站赌场百度账号注册中心
  • 柳市网站设计推广百度app客服人工电话
  • wordpress调用阅读量福州seo兼职
  • 中华保险网站南京网页搜索排名提升
  • 哪种网站开发最简单seo公司杭州
  • 网站建设怎么付款谷歌推广培训
  • 黄石有哪些做视觉网站的公司网站建设是干嘛的
  • 免费行情网站app斗印电商网站平台搭建
  • 丽江市建设局官方网站神童预言新冠2023结束
  • 网站建设哪个公司快速排名官网
  • 观山湖网站建设互联网广告代理
  • 怎么查公司名称是否被注册商标江门关键词优化公司
  • 做网站的什么公司最好衡阳网站建设
  • 贾汪微网站开发全渠道营销成功案例
  • 省级建设网站各大网站提交入口网址
  • 做公司网站成本免费推广引流app
  • 怎么用dw做博客网站广州网站外包
  • 网站建设小程序公司网站设计的内容有哪些
  • 温州外贸网站建设关键词推广价格
  • 北京建设网站哪里好锦州seo推广
  • 用php做购物网站案例百度搜索引擎属于什么引擎
  • 惠东做网站报价广告软文
  • 用户体验做的好的网站nba中国官方网站
  • 合肥网站建设技术百度网站名称
  • 在中国做外国网站怎么收钱详情页页面页面