相关疑难解决方法(0)

递归 CTE 以找到独特的 slug

我有一个文章表,我希望 slug 是独一无二的。

CREATE TABLE article (
   title char(50) NOT NULL,
   slug  char(50) NOT NULL
);
Run Code Online (Sandbox Code Playgroud)

当用户输入标题时,例如News on Apple,我想检查数据库以查看是否存在相应的 slug,例如news-on-apple。如果是这样,我将给一个数值添加后缀,直到找到一个唯一的值,例如news-on-apple-1. 可以通过递归 CTE 查询而不是在我的 ORM 中进行递归来实现。是否有一个很好的大概数字,我应该停止递归和出错。我可以想象人们使用相同的标题 1000 次,这将导致 1000 次查询只是为了创建 1 篇文章。

我对递归 CTE 的理解可能是不正确的,并且没有更好的方法来找到唯一的 slug。请提出任何替代方案。

postgresql database-design cte recursive

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

jsonb 可以按*值* 甚至部分值进行快速搜索吗?其他类型?

我有一个person必须插入任意数据的表。由于我使用的是 Postgres 9.4,因此jsonb似乎是正确的选择。

示例数据:

id: 1, name: "Joe Doe", preferences: { color: "red" , toy: "car"}
id: 2, name: "Jane Doe", preferences: { color: "blue", food: "hamburguer" }
Run Code Online (Sandbox Code Playgroud)

问题是我需要通过变量值来查询它,例如:

所有在偏好上有“汉堡包”的人,部分查询也必须是可能的,所以搜索“b​​urg”必须给我带来记录2。

使用json函数,我可以搜索值,只要我知道key,对吗?或者有一种快速搜索值的方法?

我想做的是创建一个 tsvector 列并转储 json 以使用全文搜索来执行部分查询。

postgresql database-design full-text-search json

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

使用 PostgreSQL 进行全文搜索

我有一个包含这些行的表:

Stickers
------------------------------------------------------
ID | Title                 |Keywords (ts_vector)
------------------------------------------------------
01 | Sticker Case 580H     |'580h':3 'cas':2 'stick':1
02 | Sticker Case 580L     |'580l':3 'cas':2 'stick':1
03 | Sticker Case 580      |'580':3 'cas':2 'stick':1
04 | Sticker Case Plus 3000|'3000':4 'cas':2 'plus':3 'stick':1
Run Code Online (Sandbox Code Playgroud)

好吧,当我使用这个脚本进行搜索时,只返回第 03 行,我如何返回第 01 和 02 行?

SELECT
*
FROM
stickers
WHERE
keywords @@@ to_tsquery('case & 580');
Run Code Online (Sandbox Code Playgroud)

postgresql full-text-search postgresql-9.2

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

如何使这个查询使用我的多列索引?

目前,我有一个定义如下的视图:

                       View "public.customer_list"
  Column   |          Type           | Modifiers | Storage  | Description 
-----------+-------------------------+-----------+----------+-------------
 id        | bigint                  |           | plain    | 
 name      | character varying(100)  |           | extended | 
 street    | character varying(100)  |           | extended | 
 zip       | character varying(10)   |           | extended | 
 city      | character varying(100)  |           | extended | 
 country   | character varying(3)    |           | extended | 
 phone     | character varying(100)  |           | extended | 
 mail      | character varying(100)  |           | extended | 
 rating    | integer                 | …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index postgresql-performance

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

相当于 PostgreSQL 中的 UTF8_UNICODE_CI 排序规则

我想要 PostgreSQL 数据库中表中的一列(我使用的是 9.6 版)。我知道UTF8_UNICODE_CIMySQL上的排序规则,所以我尝试了:

CREATE TABLE thing (
    id    BIGINT PRIMARY KEY
   ,name  VARCHAR(120) NOT NULL COLLATE "UTF8_UNICODE_CI"
);
Run Code Online (Sandbox Code Playgroud)

但我得到:

ERROR: collation "UTF8_UNICODE_CI" for encoding "UTF8" does not exist
Run Code Online (Sandbox Code Playgroud)

环顾四周,我发现pg_collation表格显示了排序规则,其中显示:

=# SELECT * from pg_collation;
 collname | collnamespace | collowner | collencoding | collcollate | collctype
----------+---------------+-----------+--------------+-------------+-----------
 default  |            11 |        10 |           -1 |             |
 C        |            11 |        10 |           -1 | C           | C
 POSIX    |            11 |        10 |           -1 | …
Run Code Online (Sandbox Code Playgroud)

postgresql collation pattern-matching encoding case-sensitive

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

使用 SIMILAR TO 和许多前缀优化查询

我在 PostgreSQL 9.6中有一个查询需要很长时间才能运行:

SELECT DISTINCT ON (gc.number)
       gc.number, gc.code, ch.client_id, ch.client_parent_id
FROM client gc, client_hierarchy ch
WHERE gc.code = ch.client_id
AND gc.number in (SELECT NUMB FROM directory
                  WHERE ACTIVE = TRUE
                  AND NUMB SIMILAR TO '(ABTR|GREW|POEW)%');
Run Code Online (Sandbox Code Playgroud)

SIMILAR TO接收许多不同NUMB的作为参数。ABTR|GREW|POEW大约 3.500 多。

表定义:

CREATE TABLE directory (
    id BIGINT PRIMARY KEY NOT NULL,
    active BOOLEAN NOT NULL,
    numb VARCHAR(8),
    branch VARCHAR(20),
    city VARCHAR(50),
    modified_date TIMESTAMP NOT NULL,
    name VARCHAR(200)
);
CREATE INDEX dir_numb_index ON directory (numb); …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index postgresql-performance

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

DBLINK 匹配本地表中的值

我正在尝试运行以下 dblink start 事务;

select dblink_connect('host=remote port=5432 dbname=remote_db user=user password=pass');
insert into local_table (column1, column2 )
select * from dblink(`
    select remote_column1 ltrim(remote_column2, ''TEST'') from remote_table
       where remote_column2 ilike ''TEST%''  
) as t(column1 varchar, column2 varchar) ;
Run Code Online (Sandbox Code Playgroud)

现在上述工作正常,但我遇到了一个挑战: - 我想要而不是硬编码TEST,我希望它从本地表中查询,所以我希望替换TEST为类似SELECT uniq_id from some_local_table的东西它背后的逻辑是我想要获取具有的记录前缀匹配的结果SELECT uniq_id from some_local_table但在插入时修剪前缀以local_table希望有人理解我

postgresql

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

在创建索引期间“错误:文本搜索字典“unaccent”不存在?

我在 Mac OS X Yosemite 上运行 PostgreSQL 9.3。

我尝试创建一个非重音小写三元组索引。为了实现它,我这样做了:

mydb=# CREATE EXTENSION pg_trgm SCHEMA public VERSION "1.1"; 
       CREATE EXTENSION unaccent SCHEMA public; 
       ALTER FUNCTION unaccent(text) IMMUTABLE;
CREATE EXTENSION
CREATE EXTENSION
ALTER FUNCTION
Run Code Online (Sandbox Code Playgroud)

然后我尝试创建索引:

mydb=# CREATE INDEX author_label_hun_gin_trgm ON address 
       USING gin (public.unaccent(lower(label_hun)) gin_trgm_ops);
ERROR:  text search dictionary "unaccent" does not exist
Run Code Online (Sandbox Code Playgroud)

...并得到这个错误。如果我尝试列出可用的文本搜索词典,该unaccent词典似乎就在那里:

mydb=# \dFd
                             List of text search dictionaries
   Schema   |      Name       |                        Description                        
------------+-----------------+-----------------------------------------------------------
 pg_catalog | danish_stem     | snowball stemmer for danish language
 pg_catalog | dutch_stem      | …
Run Code Online (Sandbox Code Playgroud)

postgresql index index-tuning functions unaccent

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

使用带有非重音和右端通配符的 ILIKE

我使用 Postgresql 9.4,我有一个名为 foo 的大表。我想搜索它,但如果搜索文本很短(例如“v”)或很长(例如“这是一个在表 foo% 上使用 gin 的搜索示例”),我的执行时间会很长。在这种情况下,我的索引被忽略。这是我的搜索查询:

EXPLAIN (ANALYZE, TIMING)
SELECT  "foo".* FROM "foo" WHERE "foo"."locale" = 'de'
AND f_unaccent(foo.name) ILIKE f_unaccent('v%')
AND foo.configuration->'bar' @> '{"is":["a"]}'
LIMIT 100;
Run Code Online (Sandbox Code Playgroud)

这是我的索引:

CREATE INDEX index_foo_on_name_de_gin ON foo USING gin(f_unaccent(name) gin_trgm_ops) WHERE locale = 'de';
Run Code Online (Sandbox Code Playgroud)

为什么索引被忽略并使用seq scan和/或Bitmap heap scan?如何添加其他索引来解决此问题?

为什么它会重新检查?

Recheck Cond: ((f_unaccent((name)::text) ~~* 'v%'::text) AND ((locale)::text = 'de'::text))
Run Code Online (Sandbox Code Playgroud)

功能f_unaccent

CREATE OR REPLACE FUNCTION f_unaccent(text)
         RETURNS text AS
         $func$
         SELECT unaccent('unaccent', $1)
         $func$  LANGUAGE sql IMMUTABLE …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index index-tuning postgresql-9.4 postgresql-performance

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

有效地搜索整个 1 级嵌套 JSONB

假设我们需要检查 jsonb 列是否包含与任何值(非嵌套,仅第一级)中的子字符串匹配的特定值。

如何有效地优化查询以搜索JSONB每个值的整个列?

是否有一些好的替代方法可以替代转换ILIKE %val%为文本的 jsonb 数据类型?

jsonb_each_text(jsonb_column) ILIKE '%val%'
Run Code Online (Sandbox Code Playgroud)

例如,考虑以下数据:

SELECT '{"col1": "somevalue", "col2": 5.5, "col3": 2016-01-01, "col4": "othervalue", "col5": "yet_another_value"}'::JSONB
Run Code Online (Sandbox Code Playgroud)

当需要%val%在列中包含不同键配置的记录中搜索模式时,您将如何优化这样的查询?是否有更好的替代方法来将每个键值对提取为文本并执行 ILIKE/POSIX 搜索?

主要是,我正在寻找一种不同的替代方法来将整个 jsonb 字段解压缩到单独的键行中,它们的值为 text

postgresql json postgresql-9.5

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