小编Juk*_*rpa的帖子

PostgreSQL - 处理数千个元素的数组

我希望根据列是否包含在作为整数数组传递的大值列表中来选择行。

这是我目前使用的查询:

SELECT item_id, other_stuff, ...
FROM (
    SELECT
        -- Partitioned row number as we only want N rows per id
        ROW_NUMBER() OVER (PARTITION BY item_id ORDER BY start_date) AS r,
        item_id, other_stuff, ...
    FROM mytable
    WHERE
        item_id = ANY ($1) -- Integer array
        AND end_date > $2
    ORDER BY item_id ASC, start_date ASC, allowed ASC
) x
WHERE x.r <= 12
Run Code Online (Sandbox Code Playgroud)

该表的结构如下:

    Column     |            Type             | Collation | Nullable | Default 
---------------+-----------------------------+-----------+----------+---------
 item_id       | integer                     |           | not null …
Run Code Online (Sandbox Code Playgroud)

postgresql performance postgresql-performance

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