簡體   English   中英

是否需要在 postgres plpgsql function 中使用變量來獲取先前查詢的行數?

[英]Necessary to use variable in postgres plpgsql function to get row count of previous query?

在 plpgsql 函數中,您可以使用IF (FOUND)來查找前一個查詢有多少結果。 是否有等效的方法可以從上一個查詢中獲取行數,例如IF (ROW_COUNT > 5) 或者你總是需要在檢查之前使用一個變量來存儲這個值?

例如,這些是唯一的選擇嗎?

-- option 1
select count(*) from mytable into thecount;
if (thecount > 5) ...

-- option 2
select * from mytable;
GET DIAGNOSTICS thecount = ROW_COUNT;
if (thecount > 5) ...

我是否錯過了獲取上一個查詢的行數的任何其他選項?

哪些可用選項是獲取行數的推薦選項,為什么?

if (select count(*) > 5 from mytable) then ...
if (select count(*) from mytable) > 5 then ...

或者更有趣

if 5 < count(*) from mytable then ...

select * from mytable; 是非法的plpgsql語句,因此ROW_COUNT不是這種情況下的選項。

通常ROW_COUNT用於獲取上一條語句(插入、更新、刪除)處理的行數。


更新

如果您不需要確切的行數,但只想檢查它是否大於或小於某個值,則此類檢查的更有效方法是:

select count(*) > 5 from (select 1 from mytable limit 6) as t(x);
# create table mytable(id bigserial not null primary key, x numeric);
# insert into mytable(x) select random() from generate_series(1,10000000) as a;
# explain analyze select count(*) > 5 from mytable;
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                  QUERY PLAN                                                                   │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Finalize Aggregate  (cost=116777.88..116777.89 rows=1 width=1) (actual time=348.815..348.815 rows=1 loops=1)                                  │
│   ->  Gather  (cost=116777.66..116777.87 rows=2 width=8) (actual time=348.713..368.395 rows=3 loops=1)                                        │
│         Workers Planned: 2                                                                                                                    │
│         Workers Launched: 2                                                                                                                   │
│         ->  Partial Aggregate  (cost=115777.66..115777.67 rows=1 width=8) (actual time=328.703..328.703 rows=1 loops=3)                       │
│               ->  Parallel Seq Scan on mytable  (cost=0.00..105361.13 rows=4166613 width=0) (actual time=0.013..211.543 rows=3333333 loops=3) │
│ Planning Time: 0.080 ms                                                                                                                       │
│ JIT:                                                                                                                                          │
│   Functions: 8                                                                                                                                │
│   Options: Inlining false, Optimization false, Expressions true, Deforming true                                                               │
│   Timing: Generation 1.422 ms, Inlining 0.000 ms, Optimization 0.484 ms, Emission 9.246 ms, Total 11.152 ms                                   │
│ Execution Time: 369.318 ms                                                                                                                    │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Time: 369,740 ms

# explain analyze select count(*) > 5 from (select 1 from mytable limit 6) as t(x);
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                       QUERY PLAN                                                       │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Aggregate  (cost=0.17..0.19 rows=1 width=1) (actual time=0.020..0.020 rows=1 loops=1)                                  │
│   ->  Limit  (cost=0.00..0.10 rows=6 width=4) (actual time=0.013..0.015 rows=6 loops=1)                                │
│         ->  Seq Scan on mytable  (cost=0.00..163693.71 rows=9999871 width=4) (actual time=0.012..0.013 rows=6 loops=1) │
│ Planning Time: 0.104 ms                                                                                                │
│ Execution Time: 0.046 ms                                                                                               │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Time: 0,507 ms

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM