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)

相关内容

热门资讯

解密科技赛道超额收益!顶尖基金... 2026年以来A股结构性行情极致演绎,主动权益基金业绩严重分化。数据显示,截至7月3日,主动权益基金...
证券板块为什么是“股市风向标”... 资本市场的长期投资价值对于证券公司的长期投资价值具有显著的正向带动作用。从收入构成中我们能够看到证券...
原创 年... 46% 的 Z 世代、35% 的千禧一代,延后了买房、结婚、生育这些人生重大决策。 听起来有点惨,但...
消息称SambaNova融资1... IT之家 7 月 8 日消息,据彭博社稍早前消息,AI 芯片企业 SambaNova 即将在当地时间...
利好突袭!A股盘中,直线拉升!... 云计算概念股异动! 今日(7月8日),云计算概念股集体拉升,板块指数涨幅超过4%,浪潮信息一字涨停,...
原创 传... 这几年喊了多年的美元霸权衰落,终于不再是空话。美国自己急得团团转,四处找新的货币锚点,还真折腾出几个...
韩国股市跌入技术性熊市,监管紧... 韩国股市正面临今年以来最严峻的考验,基准指数KOSPI从历史高点下跌20%,进入技术性熊市门槛,监管...
原创 美... 网上一直流传着一组扎心数据,即:中国居民可支配收入仅占GDP的比例45%左右,而美国却高达75%以上...
AI休整期不悲观,某些板块底部... 继续普调,严重缩量! 今日沪指跌1.26%、创业板跌0.94%、科创综指跌0.18%; 全天成交额...
知名经济学家高善文去世,曾预判... 出品|搜狐财经 作者|汪梦婷 7月7日,知名经济学家、原国投证券首席经济学家高善文不幸去世,享年55...
20万份文件泄密,苹果被坑惨了 苹果的机密在印度代工厂塔塔电子大面积泄露后,坐不住的不仅是库克,还有新德里的高官们。 当地时间7月2...
起点订购APP贵金属订购交易欺...   互联网上充斥着白银、铂金、铜等现货贵金属投资公司的宣传,通过不实的贵金属现货APP吸引投资者,但...
全新易购APP高额手续费的期货...   全新易购APP白银铂金现货订购出现严重的损失,投资者在这个平台根本赚不到钱,开始还好,后面的交易...
银行板块震荡上扬 中国银行等股... 观点网讯:7月8日,银行板块震荡上扬,中国银行、农业银行、建设银行、成都银行、工商银行等股纷纷上扬。...
AI Agent进入深水区:企... AI Agent从技术概念走向商业落地,企业付费意愿成为检验产品价值的唯一标准。过去两年大量AI创业...
一通智投APP内幕交易欺骗投资...   一通智投APP虚假现货订购交易为非法期货,投资者被宣传广告给诱导,下载了一通智投APP,结果在这...
港股科技股全线大涨!跌多了是基... 港股市场近日整体有明显反弹的科技股今日再度集体走强,恒生科技指数拉升涨超3%。个股中,联想涨超8%,...
一场博览会,看透低空经济风口全... 文丨许佳慧 低空经济风起,谁是真正的弄潮儿? 【观赛道:题材风口开启,全链布局成型】 低空经济有多火...
黄金单边牛市没那么快回来?预测... 黄金这波反弹刚冒头,没人敢直接砸重仓赌方向。Polymarket上的交易显示,黄金7月触及4300美...
超颖电子布局“A+H”:一季度... 瑞财经 吴文婷7月6日,超颖电子发布公告称,为满足公司业务发展需要,深入推进国际化战略,打造国际化资...