lightdb22.4-新增优化器提示cardinality 和ordered_predicates
admin
2024-01-30 03:35:57
0

lightdb 22.4 新增优化器提示cardinality 和ordered_predicates

lightdb 在22.4 新增了如下两个优化器提示:

  • cardinality
  • ordered hint

下面对这两个hint 进行介绍

1. cardinality hint

cardinality hint 用于强制设置表或多表查询结果的预估行数,从而影响优化器选择执行路径,hint的效果体现在对explain 中rows值的印象。

具体请看如下案例:

1.0 前置准备

lightdb@postgres=# create table t1 (key1 int , key2 int);
CREATE TABLE
lightdb@postgres=# create table t2 (key1 int , key2 int);
CREATE TABLE

1.1 影响join算法选择

通过使用cardinality设置t1表预估行数为10,导致走了hashjoin:


lightdb@postgres=# explain select * from t1, t2 where t1.key1 = t2.key1;QUERY PLAN                            
------------------------------------------------------------------Merge Join  (cost=317.01..711.38 rows=25538 width=16)Merge Cond: (t1.key1 = t2.key1)->  Sort  (cost=158.51..164.16 rows=2260 width=8)Sort Key: t1.key1->  Seq Scan on t1  (cost=0.00..32.60 rows=2260 width=8)->  Sort  (cost=158.51..164.16 rows=2260 width=8)Sort Key: t2.key1->  Seq Scan on t2  (cost=0.00..32.60 rows=2260 width=8)
(8 rows)lightdb@postgres=# explain select /*+cardinality(t1 10)*/* from t1, t2 where t1.key1 = t2.key1;QUERY PLAN                               
------------------------------------------------------------------------Hash Join  (cost=32.73..74.93 rows=113 width=16)Hash Cond: (t2.key1 = t1.key1)->  Seq Scan on t2 @"lt#0"  (cost=0.00..32.60 rows=2260 width=8)->  Hash  (cost=32.60..32.60 rows=10 width=8)->  Seq Scan on t1 @"lt#0"  (cost=0.00..32.60 rows=10 width=8)
(5 rows)lightdb@postgres=# 

1.2 影响内外表选择

对应hashjoin,可以通过把rows 设小,可以强制把设置的表作为内表构建hash表。

lightdb@postgres=# explain select /*+cardinality(t1 20) cardinality(t2 20)*/* from t1, t2 where t1.key1 = t2.key1;QUERY PLAN                               
------------------------------------------------------------------------Hash Join  (cost=32.85..65.57 rows=2 width=16)Hash Cond: (t1.key1 = t2.key1)->  Seq Scan on t1 @"lt#0"  (cost=0.00..32.60 rows=20 width=8)->  Hash  (cost=32.60..32.60 rows=20 width=8)->  Seq Scan on t2 @"lt#0"  (cost=0.00..32.60 rows=20 width=8)
(5 rows)lightdb@postgres=# explain select /*+cardinality(t1 10) cardinality(t2 20)*/* from t1, t2 where t1.key1 = t2.key1;QUERY PLAN                               
------------------------------------------------------------------------Hash Join  (cost=32.73..65.41 rows=1 width=16)Hash Cond: (t2.key1 = t1.key1)->  Seq Scan on t2 @"lt#0"  (cost=0.00..32.60 rows=20 width=8)->  Hash  (cost=32.60..32.60 rows=10 width=8)->  Seq Scan on t1 @"lt#0"  (cost=0.00..32.60 rows=10 width=8)
(5 rows)lightdb@postgres=# explain select /*+cardinality(t1 20) cardinality(t2 10)*/* from t1, t2 where t1.key1 = t2.key1;QUERY PLAN                               
------------------------------------------------------------------------Hash Join  (cost=32.73..65.41 rows=1 width=16)Hash Cond: (t1.key1 = t2.key1)->  Seq Scan on t1 @"lt#0"  (cost=0.00..32.60 rows=20 width=8)->  Hash  (cost=32.60..32.60 rows=10 width=8)->  Seq Scan on t2 @"lt#0"  (cost=0.00..32.60 rows=10 width=8)
(5 rows)lightdb@postgres=# 

1.3 影响子查询

lightdb@postgres=# explain select t1.key1 from t1, (select distinct * from t2 ) x where t1.key1 = x.key1 and x.key2>10;QUERY PLAN                                  
-----------------------------------------------------------------------------Hash Join  (cost=49.23..174.72 rows=2509 width=4)Hash Cond: (t1.key1 = x.key1)->  Seq Scan on t1  (cost=0.00..32.60 rows=2260 width=4)->  Hash  (cost=46.45..46.45 rows=222 width=4)->  Subquery Scan on x  (cost=42.02..46.45 rows=222 width=4)->  HashAggregate  (cost=42.02..44.23 rows=222 width=8)Group Key: t2.key1, t2.key2->  Seq Scan on t2  (cost=0.00..38.25 rows=753 width=8)Filter: (key2 > 10)
(9 rows)lightdb@postgres=# explain select /*+cardinality(x 100)*/t1.key1 from t1, (select distinct * from t2 ) x where t1.key1 = x.key1 and x.key2>10;QUERY PLAN                              -----------------------------------------------------------------------------
--------Hash Join  (cost=47.70..125.50 rows=1130 width=4)Hash Cond: (t1.key1 = x.key1)->  Seq Scan on t1 @"lt#0"  (cost=0.00..32.60 rows=2260 width=4)->  Hash  (cost=46.45..46.45 rows=100 width=4)->  Subquery Scan on x @"lt#0"  (cost=42.02..46.45 rows=100 width=4)->  HashAggregate  (cost=42.02..44.23 rows=222 width=8)Group Key: t2.key1, t2.key2->  Seq Scan on t2 @"lt#1"  (cost=0.00..38.25 rows=753 w
idth=8)Filter: (key2 > 10)
(9 rows)lightdb@postgres=# 

1.4 影响多表

lightdb@postgres=# explain select * from t1 ,t2 ,t1 a where t1.key1=t2.key1 and t2.key1=a.key1;QUERY PLAN                                 -----------------------------------------------------------------------------
-Merge Join  (cost=475.52..5209.87 rows=288579 width=24)Merge Cond: (a.key1 = t1.key1)->  Sort  (cost=158.51..164.16 rows=2260 width=8)Sort Key: a.key1->  Seq Scan on t1 a  (cost=0.00..32.60 rows=2260 width=8)->  Materialize  (cost=317.01..775.23 rows=25538 width=16)->  Merge Join  (cost=317.01..711.38 rows=25538 width=16)Merge Cond: (t1.key1 = t2.key1)->  Sort  (cost=158.51..164.16 rows=2260 width=8)Sort Key: t1.key1->  Seq Scan on t1  (cost=0.00..32.60 rows=2260 width=8)->  Sort  (cost=158.51..164.16 rows=2260 width=8)Sort Key: t2.key1->  Seq Scan on t2  (cost=0.00..32.60 rows=2260 width=8)
(14 rows)lightdb@postgres=# explain select /*+cardinality(t1 t2 100)*/* from t1 ,t2 ,t1 a where t1.key1=t2.key1 and t2.key1=a.key1;QUERY PLAN                             -----------------------------------------------------------------------------
---------Hash Join  (cost=712.63..790.43 rows=1130 width=24)Hash Cond: (a.key1 = t1.key1)->  Seq Scan on t1 a @"lt#0"  (cost=0.00..32.60 rows=2260 width=8)->  Hash  (cost=711.38..711.38 rows=100 width=16)->  Merge Join  (cost=317.01..711.38 rows=100 width=16)Merge Cond: (t1.key1 = t2.key1)->  Sort  (cost=158.51..164.16 rows=2260 width=8)Sort Key: t1.key1->  Seq Scan on t1 @"lt#0"  (cost=0.00..32.60 rows=2260 
width=8)->  Sort  (cost=158.51..164.16 rows=2260 width=8)Sort Key: t2.key1->  Seq Scan on t2 @"lt#0"  (cost=0.00..32.60 rows=2260 
width=8)
(12 rows)lightdb@postgres=# 

note

cardinality hint是rows hint 的别名,在22.4 我们对rows hint 进行了扩展,支持对单表进行设置。

2. ordered_predicates hint

ordered_predicates 用于强制优化器保留约束的顺序,对索引条件无效, 不考虑连接条件。目前具有如下限制:

  • 对于涉及等价推导的约束不起效,包括实际参与推导,和推导参数的约束。

具体案例如下:

2.0 前置准备

chuhx@postgres=# create table test1 (key1 int, key2 int, key3 int);
CREATE TABLE
chuhx@postgres=# create table test2 (key1 int, key2 int, key3 int);
CREATE TABLE
chuhx@postgres=# 

2.1 对于where 或on或having后约束 不组合

where

lightdb@postgres=# EXPLAIN (COSTS false) select * from test1 where mod(key2, 10) = 2 and key1 = 1;QUERY PLAN                   
------------------------------------------------Seq Scan on test1Filter: ((key1 = 1) AND (mod(key2, 10) = 2))
(2 rows)lightdb@postgres=# EXPLAIN (COSTS false) select/*+ordered_predicates*/ * from test1 where mod(key2, 10) = 2 and key1 = 1;  QUERY PLAN                   
------------------------------------------------Seq Scan on test1 @"lt#0"Filter: ((mod(key2, 10) = 2) AND (key1 = 1))
(2 rows)lightdb@postgres=# EXPLAIN (COSTS false) select * from test1 where mod(key2, 10) = 2 and key1 > 1;QUERY PLAN                   
------------------------------------------------Seq Scan on test1Filter: ((key1 > 1) AND (mod(key2, 10) = 2))
(2 rows)lightdb@postgres=# EXPLAIN (COSTS false) select/*+ordered_predicates*/ * from test1 where mod(key2, 10) = 2 and key1 > 1;QUERY PLAN                   
------------------------------------------------Seq Scan on test1 @"lt#0"Filter: ((mod(key2, 10) = 2) AND (key1 > 1))
(2 rows)

on

lightdb@postgres=# EXPLAIN (COSTS false) select * from test1 a join test2 b on a.key1=b.key1 and mod(a.key2, 10) = 2 and  a.key3 = 10; QUERY PLAN                          
-------------------------------------------------------------Hash JoinHash Cond: (b.key1 = a.key1)->  Seq Scan on test2 b->  Hash->  Seq Scan on test1 aFilter: ((key3 = 10) AND (mod(key2, 10) = 2))
(6 rows)lightdb@postgres=# EXPLAIN (COSTS false) select/*+ordered_predicates*/ * from test1 a join test2 b on a.key1=b.key1 and mod(a.key2, 10) = 2 and  a.key3 = 10; QUERY PLAN                          
-------------------------------------------------------------Hash JoinHash Cond: (b.key1 = a.key1)->  Seq Scan on test2 b @"lt#0"->  Hash->  Seq Scan on test1 a @"lt#0"Filter: ((mod(key2, 10) = 2) AND (key3 = 10))
(6 rows)

having

lightdb@postgres=# EXPLAIN (COSTS false) select key1 from test1 group by key1, key2 having mod(key2, 10) = 2 and key1 = 1;QUERY PLAN                         
------------------------------------------------------------GroupGroup Key: key1, key2->  SortSort Key: key2->  Seq Scan on test1Filter: ((key1 = 1) AND (mod(key2, 10) = 2))
(6 rows)lightdb@postgres=# EXPLAIN (COSTS false) select/*+ordered_predicates*/ key1 from test1 group by key1, key2 having mod(key2, 10) = 2 and key1 = 1;QUERY PLAN                         
------------------------------------------------------------GroupGroup Key: key1, key2->  SortSort Key: key2->  Seq Scan on test1 @"lt#0"Filter: ((mod(key2, 10) = 2) AND (key1 = 1))
(6 rows)

2.2 on 和where 结合(无等价推导, having 类同where,不再举例)

lightdb@postgres=# EXPLAIN (COSTS false) select * from test1 a join test2 b on a.key1=b.key1 and mod(a.key2, 10) = 2 where mod(b.key2, 10) = 2 and a.key3 = 10; QUERY PLAN                       
-------------------------------------------------------Nested LoopJoin Filter: (a.key1 = b.key1)->  Seq Scan on test1 aFilter: ((key3 = 10) AND (mod(key2, 10) = 2))->  Seq Scan on test2 bFilter: (mod(key2, 10) = 2)
(6 rows)lightdb@postgres=# EXPLAIN (COSTS false) select/*+ordered_predicates*/ * from test1 a join test2 b on a.key1=b.key1 and mod(a.key2, 10) = 2 where mod(b.key2, 10) = 2 and a.key3 = 10; QUERY PLAN                       
-------------------------------------------------------Nested LoopJoin Filter: (a.key1 = b.key1)->  Seq Scan on test1 a @"lt#0"Filter: ((mod(key2, 10) = 2) AND (key3 = 10))->  Seq Scan on test2 b @"lt#0"Filter: (mod(key2, 10) = 2)
(6 rows)

2.3 不起效情况

有等价类推导a.key2=b.key2 && b.key2 = 1 导致 b 表上的 ((key2 = 1) AND (mod(key2, 10) = 2)) 不能保持sql 中定义顺序。

lightdb@postgres=# EXPLAIN (COSTS false) select * from test1 a left join test2 b on a.key1=b.key1 and a.key2=b.key2 where mod(b.key2, 10) = 2 and b.key2 = 1; 
e                      QUERY PLAN                      
y2 where mod(b.key2, 10) = 2 and b.key2 = 1; 
------------------------------------------------------Nested LoopJoin Filter: (a.key1 = b.key1)->  Seq Scan on test2 bFilter: ((key2 = 1) AND (mod(key2, 10) = 2))->  Seq Scan on test1 aFilter: (key2 = 1)
(6 rows)lightdb@postgres=# EXPLAIN (COSTS false) select/*+ordered_predicates*/ * from test1 a left join test2 b on a.key1=b.key1 and a.key2=b.key2 where mod(b.key2, 10) = 2 and b.key2 = 1; QUERY PLAN                      
------------------------------------------------------Nested LoopJoin Filter: (a.key1 = b.key1)->  Seq Scan on test2 b @"lt#0"Filter: ((key2 = 1) AND (mod(key2, 10) = 2))->  Seq Scan on test1 a @"lt#0"Filter: (key2 = 1)
(6 rows)

有等价类推导a.key2=b.key2和a.key2 = 1 导致 a 表上((key2 = 1) AND (mod(key2, 10) = 2)) 不能保留sql中顺序。

这边单独说明是因为在代码内部与上述不起效的原理不同(由于外连接)。

lightdb@postgres=# EXPLAIN (COSTS false) select * from test1 a left join test2 b on a.key1=b.key1 and a.key2=b.key2 where mod(a.key2, 10) = 2 and a.key2 = 1;
y2 where mod(a.key2, 10) = 2 and a.key2 = 1;QUERY PLAN                        
----------------------------------------------------------Nested Loop Left JoinJoin Filter: ((a.key2 = b.key2) AND (a.key1 = b.key1))->  Seq Scan on test1 aFilter: ((key2 = 1) AND (mod(key2, 10) = 2))->  Seq Scan on test2 bFilter: (key2 = 1)
(6 rows)lightdb@postgres=# EXPLAIN (COSTS false) select /*+ordered_predicates*/* from test1 a left join test2 b on a.key1=b.key1 and a.key2=b.key2 where mod(a.key2, 10) = 2 and a.key2 = 1;QUERY PLAN                        
----------------------------------------------------------Nested Loop Left JoinJoin Filter: ((a.key2 = b.key2) AND (a.key1 = b.key1))->  Seq Scan on test1 a @"lt#0"Filter: ((key2 = 1) AND (mod(key2, 10) = 2))->  Seq Scan on test2 b @"lt#0"Filter: (key2 = 1)
(6 rows)

相关内容

热门资讯

原创 宁... 由于化肥和天然气的短缺,欧盟委员会正在向欧洲农民建议,尝试用牛粪以及小型牲畜的粪便和尿液,来替代来自...
原创 3... 在情感的迷宫中,每个星座都有其独特的情感语言。今天,让我们以作家的身份,用专业的语气,探讨那些扎心的...
原创 5... 昨天亏的钱今天回来了? 先别急着加仓,看看这些细节你未必注意到。 5月22日(周五),A股高开,随后...
美股收盘:标普连涨八周道指刷新... 财联社5月23日讯(编辑 赵昊)周五(5月22日),受美债收益率回落影响,美股三大指数连三日收升,其...
新规实施在即,A股公司董秘密集... 董秘任职新规即将正式施行,相关的职务变动已提前开启。据财闻不完全统计发现,4月21日至今约130家公...
肾内科举办人工智能助力临床医疗... 在医疗信息化与智能化快速发展的今天,人工智能正逐步成为提升临床工作效率与质量的重要工具。为进一步增强...
坪山人等到了!盒马鲜生坪山首店... 南都讯 记者曾海城5月22日,深圳市坪山区马峦街道悦阾SPACE购物中心迎来七家优质品牌集中签约。其...
港股异动丨AI应用股集体拉升 ... 港股市场AI应用股集体拉升,迅策(3317.HK)午后强势拉升,一度大涨16.4%至264港元,市值...
OpenAI估值冲刺万亿美元I... SpaceX提交招股书的同一天,另一条重磅消息在资本市场快速发酵。据《华尔街日报》等多家媒体报道,O...
拟上市企业股权激励咨询怎么选,... 在拟上市企业的发展进程中,股权激励咨询显得尤为关键。如何选择一家合适的咨询机构,是众多企业面临的重要...
39.44万亿元!当险资规模超... 记者 姜鑫 39.44万亿元——这是2026年第一季度末的保险资金运用余额。同期,公募基金的资产净值...
*ST金科撤销退市及其他风险警... 观点网讯:5月22日,金科地产集团股份有限公司(*ST金科)发布关于申请撤销退市风险警示及其他风险警...
下沉市场餐饮创业选连锁项目有哪... 随着居民日常就餐需求越来越偏向便利化、高性价比,社区周边的便民餐饮赛道逐渐受到创业者和投资者的关注,...
【每周经济观察】市场分化孕育民... 国家统计局最新发布的数据显示,今年前4个月,我国民间固定资产投资同比下降5.2%。乍一看,这个数字确...
一季度赚了4350万元,蔚来能... 本报(chinatimes.net.cn)记者刘凯 北京报道 继2025年第四季度首次实现非美国通用...
“A股不死鸟”600696,确... 公司股票将于2026年6月1日进入退市整理期,预计最后交易日为6月22日,交易期限为15个交易日。退...
富途、老虎双双大跌!分别被罚没... 5月22日下午,证监会官网消息称,近日,证监会依法对Tiger Brokers (NZ) Limit...
原创 中... 在全球稀土市场的博弈中,中国东北冻土下的一个重大发现,犹如一颗重磅炸弹,让海外稀土突围的压力瞬间倍增...
西大门2025年研发投入298... 西大门(605155)披露2025年年度报告。报告期内,公司全年研发投入达2989.83万元,同比下...
AI掉队后,腾讯文档团队被压缩... 5月21日,在多个职场社交平台,有用户发文谈及腾讯文档裁员信息,该用户称腾讯文档业务将取消北京办公地...