Parameterized Path 的例子

简介:
Improve the planner's ability to use nested loops with inner index scans (Tom Lane)

The new "parameterized path" mechanism allows inner index scans to use values from relations that are more than one join level up from the scan. This can greatly improve performance in situations where semantic restrictions (such as outer joins) limit the allowed join orderings.

http://www.postgresql.org/docs/current/static/release-9-2.html

数据准备:

复制代码
postgres=# create table tst01(id integer);
CREATE TABLE
postgres=# 

postgres=# insert into tst01 values(generate_series(1,100000));
INSERT 0 100000
postgres=# 

postgres=# create index idx_tst01_id on tst01(id);
CREATE INDEX
postgres=# 
复制代码

运行:

复制代码
postgres=# prepare s(int) as select * from tst01 t where id < $1;
PREPARE
postgres=# explain execute s(2);
                                   QUERY PLAN                                    
---------------------------------------------------------------------------------
 Index Only Scan using idx_tst01_id on tst01 t  (cost=0.00..8.38 rows=1 width=4)
   Index Cond: (id < 2)
(2 rows)

postgres=# explain execute s(10000);
                                      QUERY PLAN                                       
---------------------------------------------------------------------------------------
 Index Only Scan using idx_tst01_id on tst01 t  (cost=0.00..337.64 rows=10130 width=4)
   Index Cond: (id < 10000)
(2 rows)

postgres=# explain execute s(1000000);
                          QUERY PLAN                           
---------------------------------------------------------------
 Seq Scan on tst01 t  (cost=0.00..1693.00 rows=100000 width=4)
   Filter: (id < 1000000)
(2 rows)

postgres=# explain execute s(100000);
                          QUERY PLAN                           
---------------------------------------------------------------
 Seq Scan on tst01 t  (cost=0.00..1693.00 rows=100000 width=4)
   Filter: (id < 100000)
(2 rows)

postgres=# 
复制代码

这是一个小例子,而且还是一个有些特殊的例子。

对比一下在PostgreSQL9.1.0中的表现:

复制代码
postgres=# create table tst01(id integer);
CREATE TABLE
postgres=# insert into tst01 values(generate_series(1,100000));
INSERT 0 100000
postgres=# create index idx_tst01_id on tst01(id);
CREATE INDEX
postgres=# prepare s(int) as select * from tst01 t where id < $1;
PREPARE
postgres=# explain execute s(2);
                                   QUERY PLAN                                   
 
--------------------------------------------------------------------------------
-
 Bitmap Heap Scan on tst01 t  (cost=626.59..1486.25 rows=33333 width=4)
   Recheck Cond: (id < $1)
   ->  Bitmap Index Scan on idx_tst01_id  (cost=0.00..618.26 rows=33333 width=0)
         Index Cond: (id < $1)
(4 rows)

postgres=# explain execute s(10000);
                                   QUERY PLAN                                   
 
--------------------------------------------------------------------------------
-
 Bitmap Heap Scan on tst01 t  (cost=626.59..1486.25 rows=33333 width=4)
   Recheck Cond: (id < $1)
   ->  Bitmap Index Scan on idx_tst01_id  (cost=0.00..618.26 rows=33333 width=0)
         Index Cond: (id < $1)
(4 rows)

postgres=# 
复制代码

可以看到,在9.1里,是不区分状况,执行计划固定。









本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/p/3140795.html,如需转载请自行联系原作者


目录
相关文章
|
2月前
|
C#
C#有趣小例子(三)
C#有趣小例子(三)
27 2
|
2月前
|
C#
C#的小例子和总结(二)
C#的小例子和总结(二)
26 0
|
2月前
|
C#
C#的小例子和总结(四)
C#的小例子和总结(四)
15 1
|
Java Spring 数据采集
需要些例子
教您使用java爬虫gecco抓取JD全部商品信息教您使用DynamicGecco抓取JD全部商品信息 Gecco+Spring+Mybatis完整例子,下载妹子图美女图片 结合spring的插件gecco-spring 结合htmlunit的插件ge...
1027 0
|
数据安全/隐私保护
|
算法 Android开发 计算机视觉
|
SQL 数据库 关系型数据库