在多个表中搜索(最佳做法) [英] Searching across multiple tables (best practices)

查看:93
本文介绍了在多个表中搜索(最佳做法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由表格组成的物业管理应用程序:

I have property management application consisting of tables:

tenants
landlords
units
properties
vendors-contacts

基本上,我希望一个搜索字段可以全部搜索它们,而不必选择要搜索的类别.这会是一个可以接受的解决方案(技术上明智的选择吗?)

Basically I want one search field to search them all rather than having to select which category I am searching. Would this be an acceptable solution (technology wise?)

从长远来看,跨5个表进行搜索是否可以,并且不会使服务器瘫痪?做到这一点的最好方法是什么?

Will searching across 5 tables be OK in the long run and not bog down the server? What's the best way of accomplishing this?

使用PostgreSQL

Using PostgreSQL

推荐答案

为此,我建议使用像Lucene这样的专用全文本索引工具.它可能会更容易启动和运行,并且结果也更快,功能更丰富.如果您还需要结构化搜索功能,或者您的搜索索引的事务性很重要,则Postgres全文索引将非常有用.

I would suggest using a specialized full-text indexing tool like Lucene for this. It will probably be easier to get up and running, and the result is faster and more featureful too. Postgres full text indexes will be useful if you also need structured search capability on top of this or transactionality of your search index is important.

如果您确实想在数据库中实现此功能,则在使用代理键的情况下,类似于以下方案的方法可能会起作用:

If you do want to implement this in the database, something like the following scheme might work, assuming you use surrogate keys:

  1. 为每个可搜索表创建一个视图,该视图具有该表的主键列,该表的名称以及该表中所有可搜索字段的串联.
  2. 在完全相同的串联的to_tsvector()上的基础上创建功能GIN或GiST索引.
  3. 在所有视图上创建一个UNION ALL,以创建可搜索的视图.

之后,您可以像这样进行搜索:

After that you can do the searches like this:

SELECT id, table_name, ts_rank_cd(body, query) AS rank
    FROM search_view, to_tsquery('search&words') query
    WHERE query @@ body
    ORDER BY rank DESC
    LIMIT 10;

这篇关于在多个表中搜索(最佳做法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆