2015-08-15 52 views
0

我正在使用搜索功能编写Web应用程序。我正在搜索“资产”列表。如何在sql数据库中搜索许多字段和许多值

资产具有名称和说明。它也有一个与之相关的标签列表。

我的模式是这样的:

Schema 我的输入是这样的:“许多关键词”

我的问题是什么是搜索资产的最佳途径。同时请记住,将来可能会有更多的字段需要搜索。

我的观点如下:

理念1:

程序上创建一个庞大的查询,如:

 select * 
     from asset 
     where id in (
     select asset_id 
     from asset_tag 
     where tag_id in (
      select id 
      from tags 
      where name in (
       'many', 'key', 'words' 
      ) 
     ) 
    ) or (concat(name, description) like '%many%' 
     or concat(name, description) like '%key%' 
     or concat(name, description) like '%words%') 

这不是因为我可能需要到田里特别好添加更多的领域进行搜索,我有一种感觉,当数据库开始得到一些规模时,这根本不起作用。

理念2:

建立某种形式的索引系统。在那里我创建了另一个名为“asset_index”的表格,其中有两列。 Asset_id和文本。当资产得到更新或添加了标签时,我计算asset_index上的文本字段,以便将所有我想要搜索的内容连接起来。所以我的表看起来像:

asset index

所以我的查询会是这样的:

select * 
from asset 
where id in (
    select asset_id 
    from asset_index 
    where match(text) against('many key words') 
    -- or text like '%many%' or text like '%key%' or text like '%words%' depending up if I can get the database to support full text searches 
) 

还是有更好的办法?第三方索引服务?

+1

难道MySQL或SQL服务器的MySQL?不能同时使用... –

+0

使用REGEX将字符串匹配到mysql中 –

+0

表的大小是多少?如果查询量很大,它应该表现良好并不重要。 –

回答

0

尝试一下本作,返回匹配的值

where text RLIKE '(many)|(key)|(words)' 

where text RLIKE '[many]|[key]|[words]' 
相关问题