相关疑难解决方法(0)

检查Postgres数组中是否存在值

我需要一种方法来测试给定数组中是否存在值.到目前为止,我想出了类似的东西

select '{1,2,3}'::int[] @> (ARRAY[]::int[] || value_variable::int)
Run Code Online (Sandbox Code Playgroud)

但我一直认为应该有一个更简单的方法,我只是看不到它.

编辑:刚认识到我能做到这一点

select '{1,2,3}'::int[] @> ARRAY[value_variable::int]
Run Code Online (Sandbox Code Playgroud)

这要好得多,我相信这已经足够了,但如果你有其他方法可以做,请分享.

arrays postgresql postgresql-9.0

169
推荐指数
6
解决办法
20万
查看次数

选择其他表中不存在的行

我有两个postgresql表:

table name     column names
-----------    ------------------------
login_log      ip | etc.
ip_location    ip | location | hostname | etc.
Run Code Online (Sandbox Code Playgroud)

我想得到每个login_log没有行的IP地址ip_location.
我尝试了这个查询,但它抛出了语法错误.

SELECT login_log.ip 
FROM login_log 
WHERE NOT EXIST (SELECT ip_location.ip
                 FROM ip_location
                 WHERE login_log.ip = ip_location.ip)
Run Code Online (Sandbox Code Playgroud)
ERROR: syntax error at or near "SELECT"
LINE 3: WHERE NOT EXIST (SELECT ip_location.ip`
Run Code Online (Sandbox Code Playgroud)

我也想知道这个查询(通过调整使其工作)是否是用于此目的的最佳性能查询.

sql postgresql null exists left-join

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

PostgreSQL中的IN vs ANY运算符

PostgreSQL中INANY运算符有什么区别?
两者的工作机制似乎是一样的.任何人都能用一个例子来解释这个吗

sql database postgresql rdbms sql-in

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

在Postgres DB中使用大量id的Django查询过滤器

我想将Django中的查询传递给我的PostgreSQL数据库.当我使用大量的id过滤我的查询时,查询非常慢并且最多可达70秒.

在寻找答案,我看到这篇文章解决了我的问题,只需更改ARRAY [ids]IN语句VALUES (id1), (id2), ....

我在pgadmin中使用原始查询测试了解决方案,查询从70s到300ms ......

如何在Django中执行相同的命令(即不使用id数组,而是使用VALUES查询)?

arrays django postgresql parameter-passing

12
推荐指数
2
解决办法
785
查看次数

将PL/pgSQL SELECT转换为数组

这是我的函数声明和正文的一部分:

CREATE OR REPLACE FUNCTION access_update()
RETURNS void AS $$
DECLARE team_ids bigint[];
BEGIN
    SELECT INTO team_ids "team_id" FROM "tmp_team_list";

    UPDATE "team_prsnl"
    SET "updt_dt_tm" = NOW(), "last_access_dt_tm" = NOW()
    WHERE "team_id" IN team_ids;
END; $$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)

我想team_ids成为一个我可以在UPDATE声明中使用的一组int .这个函数给我这样的错误:

psql:functions.sql:62: ERROR:  syntax error at or near "team_ids"
LINE 13:  AND "team_id" IN team_ids;
Run Code Online (Sandbox Code Playgroud)

sql postgresql plpgsql postgresql-9.1

9
推荐指数
2
解决办法
2万
查看次数

查找文本数组包含类似于输入值的行

我正在尝试获取类型列text[]包含类似于某些用户输入的值的行。

到目前为止,我一直在想和做的是使用'ANY'and 'LIKE'运算符,如下所示:

select * from someTable where '%someInput%' LIKE ANY(someColum);
Run Code Online (Sandbox Code Playgroud)

但这是行不通的。该查询返回与此查询相同的值:

select * from someTable where 'someInput' = ANY(someColum);
Run Code Online (Sandbox Code Playgroud)

使用unnest()子查询中的函数我得到了很好的结果,但是WHERE如果可能,我需要查询此in 子句。

为什么LIKE操作员不与操作员一起工作,ANY并且我没有出现任何错误?我认为原因之一应该是ANY运算符在查询的右边,但是...

unnest()如果不使用WHERE条款,是否有解决方案?

arrays postgresql any sql-like unnest

6
推荐指数
3
解决办法
4464
查看次数

在WHERE IN子句中使用JSONB Array中的值

JSONB在PostgreSQL中有一个对象:

'{"cars": ["bmw", "mercedes", "pinto"], "user_name": "ed"}'
Run Code Online (Sandbox Code Playgroud)

我试图在a的子句中使用其中"cars"数组的:WHERESELECT

SELECT car_id FROM cars WHERE car_type IN ('bmw', 'mercedes', 'pinto');
Run Code Online (Sandbox Code Playgroud)

这将正确返回值1,2和3 - 请参阅本文底部的表格设置.

目前,在我的功能中我这样做:

(1) Extract the "cars" array into a variable `v_car_results`.
(2) Use that variable in the `WHERE` clause.

Pseudo code:

    DECLARE v_car_results TEXT
    BEGIN
        v_car_results = '{"cars": ["bmw", "mercedes", "pinto"], "user_name": "ed"}'::json#>>'{cars}';
            -- this returns 'bmw', 'mercedes', 'pinto'
        SELECT car_id FROM cars WHERE car_type IN ( v_car_results );
    END
Run Code Online (Sandbox Code Playgroud)

但是,该SELECT语句不返回任何行.我知道它正在阅读这3种车型作为单一 …

arrays postgresql where set-returning-functions jsonb

6
推荐指数
1
解决办法
1933
查看次数

如何将自定义类型数组传递给Postgres函数

我有自定义类型

CREATE TYPE mytype as (id uuid, amount numeric(13,4));
Run Code Online (Sandbox Code Playgroud)

我想将它传递给具有以下签名的函数:

CREATE FUNCTION myschema.myfunction(id uuid, mytypes mytype[])
  RETURNS BOOLEAN AS...
Run Code Online (Sandbox Code Playgroud)

如何在postgres查询中调用它,并且不可避免地从PHP中调用?

php sql postgresql plpgsql postgresql-9.1

5
推荐指数
2
解决办法
2788
查看次数

将数组值发送到ruby中的sql查询?

我正在努力解决看似红宝石的语义问题.我正在编写一个从表单中获取可变数量的参数并创建Postgresql查询的方法.

def self.search(params)
    counter = 0
    query = ""
    params.each do |key,value|
        if key =~ /^field[0-9]+$/
            query << "name LIKE ? OR "
            counter += 1
        end
    end
    query = query[0..-4] #remove extra OR and spacing from last

    params_list = []
    (1..counter).each do |i|
      field = ""
      field << '"%#{params[:field'
      field << i.to_s
      field << ']}%", '
      params_list << field
    end
    last_item = params_list[-1]
    last_item = last_item[0..-3] #remove trailing comma and spacing
    params_list[-1] = last_item

    if params …
Run Code Online (Sandbox Code Playgroud)

ruby postgresql ruby-on-rails rails-activerecord

5
推荐指数
1
解决办法
3841
查看次数

具有许多值的 Postgres IN 子句不使用部分索引

我正在使用 Postgres 9.2.24。

我有一个以_order大约 100,000,000 行命名的表。该表有一个名为 的列merged_id int8。大约 2,000,000_order行有一个merged_id值,其余的为空。

我找到了两种不同的 Postgres 行为,我在其中_order使用查询进行搜索

select * from _order where merged_id in ( 10001 ,10002 ,10003 ....., 11000);
Run Code Online (Sandbox Code Playgroud)

如果我创建这样的索引:

create index order_merged_id_index on _order(merged_id);
Run Code Online (Sandbox Code Playgroud)

无论 in 子句中有多少个 id(测试从 1 到 50 到 100 到 200 到 1000)EXPLAIN显示搜索都将使用index_scan.

但是如果我创建这个部分索引:

create index order_merged_id_index on _order(merged_id) where merged_id is not null;
Run Code Online (Sandbox Code Playgroud)

EXPLAINseq_scanWHERE子句中显示100 多个 ID 号。

为什么是这样?
有什么办法可以解决吗?

sql postgresql indexing sql-in

3
推荐指数
1
解决办法
1403
查看次数

重写大型IN子句的最佳性能是什么?

我使用go和gorm编写了一个API,它在我们的数据库上运行计算并返回结果.

我只是IN在使用聚合时达到了条件的参数限制.示例查询:

SELECT SUM(total_amount) from Table where user_id in(...70k parameters) group by user_id
Run Code Online (Sandbox Code Playgroud)

我当前的一个边缘案例有> 65535个用户ID,所以我的Postgres客户端抛出一个错误:

got 66037 parameters but PostgreSQL only supports 65535 parameters
Run Code Online (Sandbox Code Playgroud)

我不确定最好的办法是什么.一个将处理此边缘情况的大量参数,同时不影响我的典型用例.我是否对id进行分块并迭代多次查询将其存储在内存中,直到我拥有所需的所有数据?用ANY(VALUES)...

显然,从查询中我对Postgres的知识非常有限,所以任何帮助都会令人难以置信地受到赞赏.

sql postgresql go sql-in go-gorm

3
推荐指数
1
解决办法
314
查看次数

PostgreSQL:将数组传递给过程时出现问题

我的类型为:

CREATE TYPE status_record AS
   (
   id bigint,
   status boolean
   );
Run Code Online (Sandbox Code Playgroud)

使用类型数组作为输入参数进行某些处理的过程,如下所示:

CREATE OR REPLACE FUNCTION update_status(status_list status_record[])
RETURNS text AS
$BODY$
DECLARE  

BEGIN    
--does some processing
return 'SUCCESS'; 

end;$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
Run Code Online (Sandbox Code Playgroud)

最后我查询程序为:

select *
from update_status(cast(ARRAY[(385,false),(387,false)] as status_record[]));
Run Code Online (Sandbox Code Playgroud)

在 pgadmin 中一切正常。后来,当我尝试使用Hibernate 本机 SQL 查询调用相同的命令时,Ka Boom!!!显示如下:

 org.postgresql.util.PSQLException:
 ERROR: array value must start with "{" or dimension information 
Run Code Online (Sandbox Code Playgroud)

最后一个问题:两者ARRAY[--something]{--something}作用相同吗?

java postgresql stored-procedures hibernate

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