PostgreSQL 联结方式--排序-合并联结

          排序-合并联结独立地读取需要联结的两张表,对每张表中的数据行(仅是那些满足各自where子句中条件的数据行)按照联结键进行排序,然后对排序后的数据行集进行合并。对这种联结方法来说排序的开销的非常大的。对于不能够放入内存中的大的数据源来说,可能会使用临时磁盘空间来完成排序。这是非常占内存和时间资源的。但是一旦数据行几点排序完成了,合并的过程是非常快的。为了进行合并,数据库轮流操作两个列表,比较最上面的数据行,丢弃在排序对列中比另一列表中的最上面一行出现得早的数据行,并只返回匹配的行。

          在计算成本的时候,有的时候执行计划的总成本比两个表单独扫描+排序的成本要小,这是因为当两个表排完序后,可能两个表只有前几行是对应的,比如:a表有10条记录,b表10000条,b表只有前10行与a表匹配,那么单独扫描b表闭并排序的成本是比较大的,但是在合并的时候,当a表合并完,发现b表已经没有与a表匹配的了,也就不继续扫描b表了,那么b表也是只扫描了10行,b表实际的成本的计算应该是:b表总成本*(10/10000),后面会具体说这个问题。


1. 建表和插入测试数据参考:http://blog.csdn.net/luojinbai/article/details/44033729
2. select s.*,c.class_name from student s,class c where s.class_id=c.id;

这个查询将会向下面的伪代码一样来处理结果:
select c.class_name, c.id from class c order by id
select s.id, s.name, s.class_id from student s order by s.class_id
compare the rowsets and return rows where id/class_id in both lists match
for an outer join,compare the rowsets and return all rows from the first list setting column values for the other table to null
3. 执行计划
postgres=# explain(analyze,verbose,buffers) select s.*,c.class_name from student s,class c where s.class_id=c.id;
                                                        QUERY PLAN                                                         
---------------------------------------------------------------------------------------------------------------------------
 Merge Join  (cost=24.59..30.40 rows=384 width=44) (actual time=0.169..0.330 rows=384 loops=1)
   Output: s.id, s.name, s.class_id, c.class_name
   Merge Cond: (c.id = s.class_id)
   Buffers: shared hit=4
   ->  <span style="color:#ff0000;">Sort</span>  (cost=1.27..1.29 rows=10 width=36) (actual time=0.029..0.029 rows=3 loops=1)
         Output: c.class_name, c.id
         Sort Key: c.id
         Sort Method: quicksort  Memory: 25kB
         Buffers: shared hit=1
         ->  Seq Scan on public.class c  (cost=0.00..1.10 rows=10 width=36) (actual time=0.007..0.010 rows=10 loops=1)
               Output: c.class_name, c.id
               Buffers: shared hit=1
   ->  <span style="color:#ff0000;">Sort</span>  (cost=23.32..24.28 rows=384 width=12) (actual time=0.134..0.200 rows=384 loops=1)
         Output: s.id, s.name, s.class_id
         Sort Key: s.class_id
         Sort Method: quicksort  Memory: 43kB
         Buffers: shared hit=3
         ->  Seq Scan on public.student s  (cost=0.00..6.84 rows=384 width=12) (actual time=0.006..0.052 rows=384 loops=1)
               Output: s.id, s.name, s.class_id
               Buffers: shared hit=3
 Total runtime: 0.407 ms
(21 rows)

可以看到两个排序节点 Sort。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 要在项目中整合PostgreSQL和MyBatis-Plus,你需要进行以下几个步骤。 首先,你需要在项目的pom.xml文件中添加MyBatis-Plus和PostgreSQL的依赖项。在依赖项中,你需要添加以下代码段:\[1\] ```xml <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- postgresql --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> ``` 接下来,你需要在项目的application.yml文件中进行配置。你需要设置数据库的连接信息,包括URL、用户名和密码。此外,你还需要设置schema的名称。以下是一个示例配置:\[2\] ```yaml spring: datasource: platform: postgres url: jdbc:postgresql://192.188.1.245:5432/uum?currentSchema=uum schemaName: uum username: xxxx password: xxxx driver-class-name: org.postgresql.Driver ``` 最后,你需要在数据库中创建自增字段。在PostgreSQL中,你可以使用sequence来实现自增字段的功能。以下是一个示例的SQL语句:\[3\] ```sql create sequence uum.userid_seq start with 1 increment by 1 no minvalue no maxvalue cache 1; alter sequence uum.userid_seq owner to smartsys; alter table uum.user alter column id set default nextval('uum.userid_seq'); ``` 通过以上步骤,你就可以成功地将PostgreSQL和MyBatis-Plus整合在一起了。你可以使用MyBatis-Plus提供的功能来进行数据库操作。 #### 引用[.reference_title] - *1* [springboot 整合 mybatis plus postgresql](https://blog.csdn.net/weixin_41010294/article/details/105710247)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [MybatisPlus+Postgresql整合的几个坑](https://blog.csdn.net/xuruilll/article/details/122670781)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值