相关疑难解决方法(0)

索引不与 = ANY() 一起使用,但与 IN 一起使用

t有两个索引:

create table t (a int, b int);
create type int_pair as (a int, b int);
create index t_row_idx on t (((a,b)::int_pair));
create index t_a_b_idx on t (a,b);

insert into t (a,b)
select i, i
from generate_series(1, 100000) g(i)
;
Run Code Online (Sandbox Code Playgroud)

ANY运算符不使用索引:

explain analyze
select *
from t
where (a,b) = any(array[(1,1),(1,2)])
;
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Seq Scan on t  (cost=0.00..1693.00 rows=1000 width=8) (actual time=0.042..126.789 rows=1 loops=1)
   Filter: (ROW(a, b) = ANY (ARRAY[ROW(1, 1), ROW(1, 2)]))
   Rows Removed …
Run Code Online (Sandbox Code Playgroud)

postgresql index optimization execution-plan postgresql-9.4

18
推荐指数
1
解决办法
1万
查看次数

使用复合类型创建新表

在 ORDBMS 数据库中,我看到可以使用复杂类型:

create type name as( ...)
Run Code Online (Sandbox Code Playgroud)

我可以在创建新表时引用这些类型,例如:

create table example (row_name ref(name))
Run Code Online (Sandbox Code Playgroud)

如何在 PostgreSQL 中实现相同的目标?

postgresql database-design ddl composite-types

9
推荐指数
1
解决办法
4670
查看次数

在postgresql函数中循环jsonb对象的键/值对

我正在尝试在 Postgres 中创建一个函数,该函数可以遍历给定jsonb对象中的每个键/值对。

create or replace function myFunction
(input jsonb)
returns jsonb as $$
BEGIN

    // foreach(key in input)
    //       do some math operation on its corresponding value

    returns input;

END; $$
Run Code Online (Sandbox Code Playgroud)

参数输入应为 jsonb 对象,例如{"a":1, "b":2, "c":3}.

我想遍历对象中的每个键/值对。从我的评论中可以看出,我曾经使用更通用的编程语言(如 c、java 等)编写后端代码。所以我不擅长 SQL。我曾尝试在线搜索,但他们谈论如何遍历jsonb数组而不是对象。所以真的卡在这里了。

postgresql plpgsql json

7
推荐指数
1
解决办法
1万
查看次数