18

This may be a very simplistic question, so apologies in advance, but I am very new to database usage.

I'd like to have Postgres run its full text search across multiple joined tables. Imagine something like a model User, with related models UserProfile and UserInfo. The search would only be for Users, but would include information from UserProfile and UserInfo.

I'm planning on using a gin index for the search. I'm unclear, however, on whether I'm going to need a separate tsvector column in the User table to hold the aggregated tsvectors from across the tables, and to setup triggers to keep it up to date. Or if it's possible to create an index without a tsvector column that'll keep itself up to date whenever any of the relevant fields in any of the relevant tables change. Also, any tips on the syntax of the command to create all this would be much appreciated as well.

4

1 回答 1

12

您最好的答案可能是在每个表中都有一个单独的 tsvector 列(当然,有一个索引)。如果您将数据聚合到一个共享的 tsvector 中,那么每当个人更新时,都会在该共享的 tsvector 上创建大量更新。

每个表需要一个索引。然后当你查询它时,显然你需要多个 WHERE 子句,每个字段一个。然后 PostgreSQL 将自动找出使用哪种索引组合来为您提供最快的结果 - 可能使用位图扫描。它会使您的查询编写起来更复杂一些(因为您需要多个列匹配子句),但这保持了在您想要的情况下仅查询某些字段的灵活性。

您不能创建一个跟踪多个表的索引。为此,您需要单独的 tsvector 列和每个表上的触发器来更新它。

于 2010-02-09T11:19:54.053 回答