简体   繁体   English

mysql高级搜索

[英]advanced search with mysql

I'm creating a search function for my website where the user can put in anything he likes in a textfield. 我正在为我的网站创建一个搜索功能,用户可以在其中输入自己喜欢的文本字段。 It get's matched against anything (name, title, job, car brand, ... you name it) 它可以与任何内容(名称,职务,工作,汽车品牌......命名)相匹配

I initially wrote the query with an INNER JOIN on every table that needed to be searched. 我最初在每个需要搜索的表上使用INNER JOIN编写查询。

SELECT column1, column2, ... FROM person INNER JOIN person_car ON ... INNER JOIN car ...

This ended up in a query with 6 or 8 INNER JOINs, and a whole lot WHERE ... LIKE '%searchvalue%' 最终以6或8个INNER JOIN的查询结束,并且在很多地方...就像'%searchvalue%'

Now this query seems to cause a time'out in MySql, and I even got a warning from my hosting provider that the queries just taking up too many resources. 现在,该查询似乎在MySql中导致超时,甚至我的托管服务提供商都警告说该查询占用了太多资源。

Now obviously I'm doing this very wrong, but I was wondering how the correct approach to these kind of search functions is. 现在显然我做错了,但是我想知道使用这种搜索功能的正确方法是什么。

Use multiple queries or UNION multiple queries so they go into a single resultset. 使用多个查询或UNION多个查询,以便它们进入单个结果集。

Additionally, using FULLTEXT indexes will most likely help to speed up your queries since (LIKE '%string%') - especially with the leading '%' - is extremely slow (all rows need to be checked without using indexes) 另外,使用FULLTEXT索引最有可能帮助您加快查询速度,因为(LIKE'%string%')-尤其是前导'%'-极其慢(需要检查所有行而不使用索引)

我建议实现LuceneSphynx搜索引擎,它们比sql查询要快得多且可扩展。

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

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