PgSQL · 实战经验 · 分组TOP性能提升44倍

news/2024/7/7 15:18:20

业务背景

按分组取出TOP值,是非常常见的业务需求。

比如提取每位歌手的下载量TOP 10的曲目、提取每个城市纳税前10的人或企业。

传统方法

传统的方法是使用窗口查询,PostgreSQL是支持窗口查询的。

例子

测试表和测试数据,生成10000个分组,1000万条记录。

postgres=# create table tbl(c1 int, c2 int, c3 int);
CREATE TABLE
postgres=# create index idx1 on tbl(c1,c2);
CREATE INDEX
postgres=# insert into tbl select mod(trunc(random()*10000)::int, 10000), trunc(random()*10000000) from generate_series(1,10000000);
INSERT 0 10000000

使用窗口查询的执行计划

postgres=# explain select * from (select row_number() over(partition by c1 order by c2) as rn,* from tbl) t where t.rn<=10;
                                       QUERY PLAN                                       
----------------------------------------------------------------------------------------
 Subquery Scan on t  (cost=0.43..770563.03 rows=3333326 width=20)
   Filter: (t.rn <= 10)
   ->  WindowAgg  (cost=0.43..645563.31 rows=9999977 width=12)
         ->  Index Scan using idx1 on tbl  (cost=0.43..470563.72 rows=9999977 width=12)
(4 rows)

使用窗口查询的结果举例

postgres=# select * from (select row_number() over(partition by c1 order by c2) as rn,* from tbl) t where t.rn<=10;
 rn |  c1  |   c2   | c3 
----+------+--------+----
  1 |    0 |   1657 |   
  2 |    0 |   3351 |   
  3 |    0 |   6347 |   
  4 |    0 |  12688 |   
  5 |    0 |  16991 |   
  6 |    0 |  19584 |   
  7 |    0 |  24694 |   
  8 |    0 |  36646 |   
  9 |    0 |  40882 |   
 10 |    0 |  41599 |   
  1 |    1 |  14465 |   
  2 |    1 |  29032 |   
  3 |    1 |  39969 |   
  4 |    1 |  41094 |   
  5 |    1 |  69481 |   
  6 |    1 |  70919 |   
  7 |    1 |  75575 |   
  8 |    1 |  81102 |   
  9 |    1 |  87496 |   
 10 |    1 |  90603 |   
......

使用窗口查询的效率,20.1秒

postgres=# explain (analyze,verbose,costs,timing,buffers) select * from (select row_number() over(partition by c1 order by c2) as rn,* from tbl) t where t.rn<=10;
                                                                     QUERY PLAN                                                                     
----------------------------------------------------------------------------------------------------------------------------------------------------
 Subquery Scan on t  (cost=0.43..770563.03 rows=3333326 width=20) (actual time=0.040..20813.469 rows=100000 loops=1)
   Output: t.rn, t.c1, t.c2, t.c3
   Filter: (t.rn <= 10)
   Rows Removed by Filter: 9900000
   Buffers: shared hit=10035535
   ->  WindowAgg  (cost=0.43..645563.31 rows=9999977 width=12) (actual time=0.035..18268.027 rows=10000000 loops=1)
         Output: row_number() OVER (?), tbl.c1, tbl.c2, tbl.c3
         Buffers: shared hit=10035535
         ->  Index Scan using idx1 on public.tbl  (cost=0.43..470563.72 rows=9999977 width=12) (actual time=0.026..11913.677 rows=10000000 loops=1)
               Output: tbl.c1, tbl.c2, tbl.c3
               Buffers: shared hit=10035535
 Planning time: 0.110 ms
 Execution time: 20833.747 ms
(13 rows)

雕虫小技

如何优化?

可以参考我之前写的,使用递归查询,优化count distinct的方法。

本文同样需要用到递归查询,获得分组ID

postgres=# with recursive t1 as (
postgres(#  (select min(c1) c1 from tbl )
postgres(#   union all
postgres(#  (select (select min(tbl.c1) c1 from tbl where tbl.c1>t.c1) c1 from t1 t where t.c1 is not null)
postgres(# )
postgres-# select * from t1;

写成SRF函数,如下

postgres=# create or replace function f() returns setof tbl as $$
postgres$# declare
postgres$#   v int;
postgres$# begin
postgres$#   for v in with recursive t1 as (                                                                           
postgres$#    (select min(c1) c1 from tbl )                                                                   
postgres$#     union all                                                                                      
postgres$#    (select (select min(tbl.c1) c1 from tbl where tbl.c1>t.c1) c1 from t1 t where t.c1 is not null) 
postgres$#   )                                                                                                
postgres$#   select * from t1
postgres$#   LOOP
postgres$#     return query select * from tbl where c1=v order by c2 limit 10;
postgres$#   END LOOP;
postgres$# return;
postgres$# 
postgres$# end;
postgres$# $$ language plpgsql strict;
CREATE FUNCTION

优化后的查询结果例子

postgres=# select * from f();
  c1  |   c2   | c3 
------+--------+----
    0 |   1657 |   
    0 |   3351 |   
    0 |   6347 |   
    0 |  12688 |   
    0 |  16991 |   
    0 |  19584 |   
    0 |  24694 |   
    0 |  36646 |   
    0 |  40882 |   
    0 |  41599 |   
    1 |  14465 |   
    1 |  29032 |   
    1 |  39969 |   
    1 |  41094 |   
    1 |  69481 |   
    1 |  70919 |   
    1 |  75575 |   
    1 |  81102 |   
    1 |  87496 |   
    1 |  90603 |   
......

优化后,只需要464毫秒返回10000个分组的TOP 10。

postgres=# explain (analyze,verbose,timing,costs,buffers) select * from f();
                                                     QUERY PLAN                                                      
---------------------------------------------------------------------------------------------------------------------
 Function Scan on public.f  (cost=0.25..10.25 rows=1000 width=12) (actual time=419.218..444.810 rows=100000 loops=1)
   Output: c1, c2, c3
   Function Call: f()
   Buffers: shared hit=170407, temp read=221 written=220
 Planning time: 0.037 ms
 Execution time: 464.257 ms
(6 rows)

小结

  1. 传统的方法使用窗口查询,输出多个每个分组的TOP 10,需要扫描所有的记录。效率较低。

  2. 由于分组不是非常多,只有10000个,所以可以选择使用递归的方法,用上索引取TOP 10,速度非常快。

  3. 目前PostgreSQL的递归语法不支持递归的启动表写在subquery里面,也不支持启动表在递归查询中使用order by,所以不能直接使用递归得出结果,目前需要套一层函数。


http://www.niftyadmin.cn/n/2096504.html

相关文章

互联网 免费的WebService接口

股票行情数据 WEB 服务&#xff08;支持香港、深圳、上海基金、债券和股票&#xff1b;支持多股票同时查询&#xff09;Endpoint: http://webservice.webxml.com.cn/WebServices/StockInfoWS.asmx 复制 EndpointDisco: http://webservice.webxml.com.cn/WebServices/StockInfoW…

DELL RAID卡管理工具 MegaRAID Storage Manager(偏重RAID常用管理命令)

前言&#xff1a;业务生产中大部分服务器RAID控制器使用的LSI产品&#xff0c;例如服务器&#xff1a;DELL、IBM、HP、浪潮、联想、华为。本文主要针对行业主流服务器DELL系列RAID卡管理&#xff0c;借住LSI产品管理软件MegaRAID Storage Manager &#xff08;以下简称MSM&…

BOOL与bool,TRUE/FALSE与true/false

bool是C中定义的类型&#xff0c;true/false为C中关键字 BOOL为VC中的 typedef int BOOL&#xff1b;为int类型。 typedef int BOOL;#ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE 1 #endifbool result BOOL result CPPUNIT_ASSERT(true 1); //CPPUNIT…

手机自动化_业务测试关注点

手机业务测试关注点&#xff1a; 1、登录 ●登录用户名和密码错误时&#xff0c;界面有提示信息 ●用户主动退出登录后&#xff0c;下次启动APP时&#xff0c;应该进入登录界面 ●对于支持自动登录的APP&#xff0c;数据交换时 &#xff0c;是否能自动登录成功且数据库操作无误…

JS总结篇--[转]JS学习总结-技巧、方法、细节

变量转换 var myVar "3.14159", str "" myVar,// string类型 int ~~myVar, // number类型 float 1*myVar, // number类型 bool !!myVar, // boolean类型 array [myVar]; // array类型 但是转换日期(new Date(myVar))和正则表达式(new RegEx…

产品价格谁来定

产品如何定价&#xff0c;最简单的考虑就是合理的成本加上合理的利润。成本如何计算呢,这个问题非常复杂。租用场地、雇佣人员、购买原材料、机器折旧、市场营销、产品研发、出国考察、配备车辆等等&#xff0c;都可以看作成本。经济学上的“成本”概念也很复杂。张五常《经济解…

linux的kernel是怎样工作的(TI_DM36X_ARM系统)(3)

start_kernel调用setup_arch()函数作为执行的第一步&#xff0c;在其中完成特定于体系结构的设置 1 void __init2 setup_arch(char **cmdline_p)3 {4 extern char _end[];5 6 struct alpha_machine_vector *vec NULL;7 struct percpu_struct *cpu;8 …

javascript --- 声明提前(学习笔记)

javascript --- 声明提前(学习笔记) 声明提升 未声明变量console.log(a); 在没有定义 a 的情况下&#xff0c;直接使用&#xff0c;会报错。声明变量console.log(a);var a 2; 输出结果&#xff1a;undefined 并不会输出2。 原因&#xff1a;把这个过程拆分成两个操作。JS在编译…