pg distinct 改写递归优化(德哥的思路)

德哥的优化思路巨牛逼,这种递归思维真的太吊了,我目前就缺递归思路。

下面SQL1000W行数据,列的选择性很低,只有两个值('1'和'11')都是字符串类型,'1'只有一条数据,'11'有9999999行数据。****

慢SQL:

复制代码
select distinct col from tt;

                                                      QUERY PLAN                                                      
----------------------------------------------------------------------------------------------------------------------
 HashAggregate  (cost=169247.11..169247.12 rows=1 width=3) (actual time=5082.733..5082.735 rows=2 loops=1)
   Group Key: col
   ->  Seq Scan on tt  (cost=0.00..144247.29 rows=9999929 width=3) (actual time=0.005..275.906 rows=10000000 loops=1)
 Planning Time: 0.365 ms
 Execution Time: 5082.772 ms
(5 行记录)

CTE递归优化:

复制代码
WITH RECURSIVE t AS (
   (SELECT col FROM tt ORDER BY col LIMIT 1)  
   UNION ALL
   SELECT (SELECT col FROM tt WHERE col > t.col ORDER BY col LIMIT 1)
   FROM t
   WHERE t.col IS NOT NULL
   )
SELECT col FROM t WHERE col IS NOT NULL;

                                                                        QUERY PLAN                                                                        
----------------------------------------------------------------------------------------------------------------------------------------------------------
 CTE Scan on t  (cost=50.84..52.86 rows=100 width=38) (actual time=0.024..0.079 rows=2 loops=1)
   Filter: (col IS NOT NULL)
   Rows Removed by Filter: 1
   CTE t
     ->  Recursive Union  (cost=0.43..50.84 rows=101 width=38) (actual time=0.022..0.076 rows=3 loops=1)
           ->  Limit  (cost=0.43..0.46 rows=1 width=3) (actual time=0.021..0.021 rows=1 loops=1)
                 ->  Index Only Scan using idx_1_2_tt on tt tt_1  (cost=0.43..260443.37 rows=9999929 width=3) (actual time=0.020..0.020 rows=1 loops=1)
                       Heap Fetches: 0
           ->  WorkTable Scan on t t_1  (cost=0.00..4.84 rows=10 width=38) (actual time=0.017..0.017 rows=1 loops=3)
                 Filter: (col IS NOT NULL)
                 Rows Removed by Filter: 0
                 SubPlan 1
                   ->  Limit  (cost=0.43..0.46 rows=1 width=3) (actual time=0.024..0.024 rows=0 loops=2)
                         ->  Index Only Scan using idx_1_2_tt on tt  (cost=0.43..95149.36 rows=3333310 width=3) (actual time=0.024..0.024 rows=0 loops=2)
                               Index Cond: (col > (t_1.col)::text)
                               Heap Fetches: 0
 Planning Time: 0.096 ms
 Execution Time: 0.096 ms
(18 行记录)

里面的逻辑是:

复制代码
(SELECT col FROM tt ORDER BY col LIMIT 1)

**  根节点通过order by 升序 找到最小的一条数据作为起点。**

递归查询:

复制代码
SELECT (SELECT col FROM tt WHERE col > t.col ORDER BY col LIMIT 1)
FROM t
WHERE t.col IS NOT NULL

**  在第一次迭代中,CTE t 包含值'1'。这个查询将在tt表中寻找col大于'1'的最小值。在数据集中,这将是'11'。**

**  在第二次迭代,CTE t 将包含'11'。此时,查询将尝试找到大于'11'的最小值,但没有这样的值,所以返回NULL。**

递归结束:
**  当递归查询返回NULL时,递归结束。这时,CTE t 将包含'1'和'11',返回和distinct 一样逻辑的数据。**

理解了整个逻辑后我都吓尿了,就一道算法题,确实要跟巨佬学习才行,加深递归思维。

相关推荐
Rainly200038 分钟前
工作日志之postgresql实现分布式锁
数据库·分布式·postgresql
AC赳赳老秦2 小时前
财务数据智能解读:DeepSeek分析利润表生成异常波动原因报告
数据库·人工智能·postgresql·zookeeper·测试用例·时序数据库·deepseek
AI题库3 小时前
PostgreSQL 18 从新手到大师:实战指南 - 1.3 Docker环境搭建
数据库·docker·postgresql
lkbhua莱克瓦243 小时前
基础-SQL-DML
开发语言·数据库·笔记·sql·mysql
l1t4 小时前
PostgreSQL Distinct On 关键字的用法
数据库·sql·postgresql
2401_832298104 小时前
云服务器 vs 传统物理服务器,企业该如何选择?
sql
AI题库5 小时前
PostgreSQL 18 从新手到大师:实战指南 - 1.2 关系型数据库基础
数据库·postgresql
ha_lydms6 小时前
4、Spark 函数_m/n/o/p/q/r
大数据·数据库·python·sql·spark·数据处理·dataworks
lkbhua莱克瓦247 小时前
基础-约束
android·开发语言·数据库·笔记·sql·mysql·约束
falldeep7 小时前
LeetCode高频SQL50题总结
数据结构·数据库·sql·算法·leetcode·职场和发展