简体   繁体   English

使用LIKE查询,外键或jsonb数组过滤Postgres SELECT

[英]Using LIKE queries, Foreign Keys or jsonb arrays for filtering Postgres SELECT

I have a table, which suppose to contain relations to other tables. 我有一个表,该表应该包含与其他表的关系。 But I need these relations only for filtering, ie the relations in this case are not suppose to work as foreign key or some kind of references - I just need to search through them. 但是我只需要这些关系来进行过滤,也就是说,在这种情况下,这些关系不能用作外键或某种类型的引用-我只需要在它们之间进行搜索。 It works like tags or something. 它像标签之类的东西工作。
Here is the example: 这是示例:

CREATE TABLE "public"."table_name" 
(
    "id" uuid NOT NULL DEFAULT uuid_generate_v4(),
    "relations" text NOT NULL,
    "some_column" text,
    "some_another_column" int4,
    "created" timestamp(6) WITH TIME ZONE NOT NULL DEFAULT now(),
    CONSTRAINT "table_name_pkey" PRIMARY KEY ("id") NOT DEFERRABLE INITIALLY IMMEDIATE,
    CONSTRAINT "owner" FOREIGN KEY ("owner") REFERENCES "public"."user" ("id") ON UPDATE NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE,
)
WITH (OIDS=FALSE);
ALTER TABLE "public"."table_name" OWNER TO "postgres";

the relations column will contain multiple uuid keys. relations列将包含多个UUID键。 This column is not suppose to be SELECT ed, I intend to use it only for filtering. 该列不是SELECT ,我打算仅将其用于过滤。 In this case I intend to use this kind of queries to select rows from only this table: 在这种情况下,我打算使用这种查询来仅从该表中选择行:

SELECT 
    id, some_column, some_another_column 
FROM 
    table_name 
WHERE 
    relations LIKE '%c56c8a4f-765a-4e1c-9638-f3736a25da17%' 
    AND owner = 'badee659-1fca-412a-bcf6-c73ecf1e65aa';

Of course, I will create a multicolumn (owner,relations) index. 当然,我将创建一个多列(owner,relations)索引。

Is this a good approach to perform this kind of search queries? 这是执行这种搜索查询的好方法吗? relations column will contain 1 to 10 uuids per each row on the average. relations列将包含平均每每行1至10的UUID。

Or, maybe I should create additional table, which will contain, say, one uuid for each 'relation' and FK for referencing to the table_name table? 或者,也许我应该创建其他表,该表将为每个“关系”包含一个uuid,并包含用于引用table_name表的FK? In this case I will use JOIN queries. 在这种情况下,我将使用JOIN查询。

Or may be there are better ways? 还是有更好的方法? May be I should store uuids as array within jsonb object? 也许我应该将uuids作为数组存储在jsonb对象中? Or something else? 或者是其他东西?

Index consideration 指标考虑

Using operator LIKE with leading % is not sargable . 不可将运算符LIKE与前导% 一起使用 It won't use your index, because the optimizer doesn't know how to narrow first characters from relations column. 它不会使用您的索引,因为优化器不知道如何缩小“ relations列中的第一个字符。

Design 设计

It's almost always a bad idea to store different values in one column as a string with delimiter. 用分隔符将不同的值存储在字符串中几乎总是一个坏主意。

Remember that relational databases are designed for performing JOIN operations efficiently. 请记住,关系数据库旨在有效地执行JOIN操作。 In my opinion it would be better to separate that data into rows with atomic values in their columns. 我认为最好将数据分成几列,并在各列中包含原子值。

Json consideration 杰森考虑

json and jsonb datatypes should only be taken under consideration if your columns are unpredictably changing. 仅当您的列发生jsonb才应考虑jsonjsonb数据类型。 By saying this I mean that whenever you can (without much overhead going on) fit your model into relational one, you should always go for it. 说这句话的意思是,只要您能够(无需太多开销)将模型拟合到关系模型中,就应该始终坚持下去。 The same goes for hstore . hstore

You could read this blog post to grab some information to start with when considering using a mechanism for storing dynamic columns. 在考虑使用存储动态列的机制时,您可以阅读此博文以获取一些入门信息。

A little quote from that post: 该帖子的一些引言:

How to decide when to use json 如何决定何时使用json

Use json if your data won't fit in the database using a normal relational modelling. 如果您的数据无法使用正常的关系建模方法存储在数据库中,请使用json。

Credit for above blogpost goes to @Craig Ringer . 以上博客文章的信誉归@Craig Ringer所有

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM