简体   繁体   English

创建具有基于其他表列值生成的布尔列的表?

[英]create a table with a Boolean column generated based on other tables columns values?

I have tables A, B, C with millions of rows each. 我有表A,B,C,每个表都有数百万行。 Tables B and C reference table A. The tables are mainly used for one query with multiple filters but only one of those filters vary between queries. 表B和C引用表A。这些表主要用于具有多个过滤器的一个查询,但是这些过滤器中只有一个在查询之间有所不同。 since the constant parameters are adding significant time to the query execution time, I was wondering if there is a way to precompute these params into a new table. 由于常量参数会增加查询执行时间,因此我想知道是否存在一种将这些参数预先计算到新表中的方法。 I was looking at materialized views but the issue is that the computed type I want will be different from the original column type. 我当时在查看物化视图,但问题是我想要的计算类型将与原始列类型不同。 To explain I will give an example. 为了解释,我将举一个例子。

lets say these tables represent a book store database. 可以说这些表代表一个书店数据库。 Table A contains general information and table B contain multiple codes for each book to indicate what categories they fall under such as 406, 678, 252.. . 表A包含一般信息,表B包含每本书的多个代码,以指示它们属于哪些类别,例如406, 678, 252.. I'm building a query to search for books that only fall under 3 of those categories. 我正在建立一个查询来搜索仅属于这些类别中的3类的图书。 The variable here is the keyword search in the discreption of the book. 这里的变量是书中的关键词搜索。 I will always need books under those 3 categories (codes) so these are constants. 我将始终需要这3类(代码)下的书籍,因此它们是常量。

What I want to do is create a table where it will have a column that tells me whether a given serial falls under those 3 codes or not. 我想做的是创建一个表,在该表中将有一个列,该列告诉我给定的序列是否属于这3个代码之下。 this can be done with a boolean type. 这可以通过布尔类型来完成。 I don't want to have to join these table and filter for these 3 codes (and more in the real scenario) for every query.. As I understand materialized views can't have generated fields? 我不想为每个查询而联接这些表并为这3个代码(在实际场景中更多)进行过滤。.据我了解,物化视图无法生成字段?

What do you think is a good solution here? 您认为这里有什么好的解决方案?

You have multiple options. 您有多种选择。

Partial Index 部分指数

PostgreSQL allows you to create an index with a where clause like so: PostgreSQL允许您使用where子句创建索引,如下所示:

create index tableb_category on tableb (category)
where category in (406, 678, 252);

Create a view for those categories: 为这些类别创建一个视图:

create view v_books_of_interest
as
select tablea.*, tableb.*
from tablea
inner join table b
   on tableb.bookid = tablea.bookid
   and tableb.category in (406, 678, 252);

Now, your queries can use this book_of_interest rather than books. 现在,您的查询可以使用book_of_interest而不是book。 Frankly, I would start with this first. 坦白说,我首先开始。 Query optimization with the right indexes goes a long way. 具有正确索引的查询优化有很长的路要走。 Millions of rows in multiple table are manageable. 多个表中的数百万行是可管理的。

Materialized view 物化视图

create materialized view mv_books_of_interest
as
select tablea.*, tableb.*
from tablea
inner join table b
   on tableb.bookid = tablea.bookid
   and tableb.category in (406, 678, 252);
with no data;

Periodically, run a cron job (or the like) to refresh it: 定期运行cron作业(或类似任务)以刷新它:

refresh materialized view mv_books_of_interest;

Partitioning data 分区数据

https://www.postgresql.org/docs/9.3/ddl-partitioning.html will get you started. https://www.postgresql.org/docs/9.3/ddl-partitioning.html会帮助您入门。 If your team is on-board with table inheritance, great. 如果您的团队支持表继承,那就太好了。 Give it a shot and see how that works for your use case. 试一试,看看它如何适合您的用例。

Trigger 触发

Create a field is_interesting in tableA (or tableB, depending on how you want to access data). 在tableA(或tableB,取决于您如何访问数据)中创建一个is_interesting字段。 Create a trigger that checks for a certain criteria when data is inserted in dependencies and then turns the book's flag true/false. 创建一个触发器,该触发器在将数据插入依赖项时检查特定条件,然后将书的标记设置为true / false。 That will allow your queries to run faster but could slow down your inserts and updates. 这将使您的查询运行得更快,但可能会降低插入和更新的速度。

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

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