我有一个问题,关于 Postgres 中数组列的内部处理。引擎是否将其作为具有内存分配的经典数组来处理,还是以某种方式是指针列表。
知道会很有趣 - 我有几个关于性能的问题需要回答...... :)
我正在尝试根据一组用户的 id 在另一个表中存储的数组中选择他们的所有纬度和经度。这是我的尝试:
SELECT latitude, longitude
FROM userloc WHERE id = ANY( SELECT interested FROM donedeals WHERE deals_id=67);
Run Code Online (Sandbox Code Playgroud)
但它给了我以下错误:
ERROR: operator does not exist: integer = integer[]
LINE 1: SELECT latitude, longitude FROM userloc WHERE id = ANY( SELE...
^
HINT: No operator matches the given name and argument type(s).
You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
donedeals有一int列 fordeals_id和一int array列 for interested,其中包含与 的 id 列对应的 id userloc,该列存储纬度和经度:
deals_id | interested …Run Code Online (Sandbox Code Playgroud) 基础信息
背景(不感兴趣可跳过):
我正在维护的一个项目使用 ORM,它显然将我的枚举值(从 Byte 继承)存储到存储在 varbinary(max) 字段中的二进制序列化 .Net 对象中。我只是在出现新的要求要求我的代码在中等信任度下运行后才发现这种情况发生。由于 .Net 二进制格式化程序需要完全信任才能被调用,因此它开始在枚举上崩溃。
为了清理混乱,我需要创建迁移脚本,将这些 (varbinary(max)) 值转换回整数值。只有少数不同的值,所以这应该不是一个大问题(我认为)。
问题:
选择时,我能够获得 blob 的字符串表示:
SELECT BinValue FROM MyTable where Type = 'Object';
Run Code Online (Sandbox Code Playgroud)
它返回一个字符串“0x...(一个十六进制值的数组)”。
但是当我尝试使用复制和粘贴在列上进行选择时,确保我有确切的二进制等效项:
SELECT ItemId FROM MyTable WHERE Value=convert(varbinary(max), '0x...')
Run Code Online (Sandbox Code Playgroud)
它不返回任何记录。
那么是否可以使用客户端(例如 Management Studio)来解决这个问题?
如果是这样,正确的语法是什么?
我想更新表中的行选择;这有效...
UPDATE t1 SET col1 = 'newvalue' WHERE col0 in (2, 4, 5);
Run Code Online (Sandbox Code Playgroud)
但是我怎么能在 plpgsql 函数中做同样的事情呢?下面给出了一个语法错误...
CREATE OR REPLACE FUNCTION foo(intarray int[])
RETURNS VOID AS
$BODY$
BEGIN
UPDATE t1 SET col1 = 'newvalue'
WHERE col0 in intarray;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
Run Code Online (Sandbox Code Playgroud)
错误...
ERROR: syntax error at or near "intarray"
LINE 7: WHERE col0 in intarray;
Run Code Online (Sandbox Code Playgroud)
编辑:
如果我们更换in intarray;用in (intarray);的功能被记录,但在运行时SELECT * FROM foo(ARRAY[1,3,5])的误差变...
ERROR: operator does not exist: integer = integer[]
LINE 2: WHERE …Run Code Online (Sandbox Code Playgroud) 在搜索 能力和性能方面,将标签作为或或全文字段实现的优缺点是什么?arraytext
我发现选择使用数组实现标签的能力有局限性,因为在我看来,如果数组包含,['juicy fruit']那么搜索的人'fruit'将找不到该记录(例如,tags && ARRAY['fruit']找不到它)。创建该记录的人将不得不输入更像是['juicy fruit', 'juicy', 'fruit']只需搜索'fruit'或 即可找到他们的记录的内容'juicy'。而如果我将标签实现为text,那么搜索'fruit'将找到'juicy fruit',更进一步,如果我将标签实现为全文,那么我将'juicy fruit'在使用字符串'fruits'(复数)进行搜索时找到。此外,我认为进行全文搜索不会有性能损失。想法?
但也许标签的全部意义是 完全匹配。
如何使用 Postgres 将 sql 查询结果放入字符串数组中?我在表 'mytable' 的 'zone' 列中有一些属性,我必须把它们全部放在 aString[] areas;
这是我的第一个请求,如果我犯了一些格式错误,抱歉。
有一个返回数组的函数,在查询中我们使用该函数返回结果,例如:
{"TUNDUMA GOING (BORDER)","ARRIVAL DATE",09/01/2016,22,"AT CHECKPOINT"}
{"TUNDUMA RETURN (BORDER)","DEPARTURE DATE",29/01/2016,2,"ON THE ROAD"}
Run Code Online (Sandbox Code Playgroud)
我们如何添加一个WHERE子句,该子句将仅通过搜索ON THE ROAD值的数组的第 5 列进行过滤?
有没有办法参考array[4]?
试过
where trip_status(tlid) <@ array['AT CHECKPOINT']::varchar
Run Code Online (Sandbox Code Playgroud)
同trip_status()是一个返回数组的功能,但我发现了以下错误:
Run Code Online (Sandbox Code Playgroud)Query failed: ERROR: operator does not exist: character varying[] <@ character varying LINE 6: ...ere trip_closed(tlid)=false and trip_status(tlid) <@ array[ ... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
类似的东西where trip_status(tlid)[4] = 'AT CHECKPOINT'存在吗?
我有 PostgreSQL 9.2 数据库和一个表:
id integer,
allowed_types character varying(255)`
Run Code Online (Sandbox Code Playgroud)
样本数据如下:
id allowed_types
1 3,4,5,13,14
Run Code Online (Sandbox Code Playgroud)
如何从中删除4和5,allowed_types其中以逗号分隔的 varchar 列表?
删除后,结果应该是allowed_types = 3,13,14。
该表上有许多记录,每个记录allowed_types可以包含不同的数字,用逗号分隔。
我考虑过string_to_array()and array_remove(),但array_remove()还没有在 9.2 版中。
我正在尝试声明一个日期数组。
我试过这个:
DECLARE
dateVal DATE[] := ['2018-01-01','2018-02-01'];
Run Code Online (Sandbox Code Playgroud)
我得到:
ERROR: syntax error at or near "["
Run Code Online (Sandbox Code Playgroud)
我如何正确声明它?
为了便于论证,我有一个简单的表格。我有一个选择 ids 并循环它们的函数,称为loop_test. 我可以选择一个 id 数组并循环它们,从而导致我在事务中进行更改。
CREATE OR REPLACE FUNCTION loop_test() RETURNS void AS $$
DECLARE
_ids_array INTEGER[];
_id INTEGER;
BEGIN
SELECT ARRAY(SELECT id FROM loop_test) INTO _ids_array;
FOREACH _id IN ARRAY _ids_array
LOOP
UPDATE loop_test SET looped = TRUE WHERE id = _id;
END LOOP;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
桌子:
db=# \d loop_test;
Table "public.loop_test"
Column | Type | Modifiers
---------------+---------+-----------
id | integer |
other_id | integer |
id_copy | integer |
other_id_copy | integer | …Run Code Online (Sandbox Code Playgroud)