简体   繁体   English

MYSQL查询,优化喜欢和不在

[英]MYSQL Query, Optimize LIKE and NOT IN

I have a query I need to run which is currently running extremely slow. 我有一个需要运行的查询,该查询当前运行非常慢。 The table I am working with has nearly 1 million records. 我正在使用的表有近一百万条记录。

What I need to do is search for items with a LIKE name : Example "SELECT name, other, stuff FROM my_table WHERE name LIKE '%some_name%'" 我需要做的是搜索具有LIKE名称的项目:示例"SELECT name, other, stuff FROM my_table WHERE name LIKE '%some_name%'"

In addition it has to be able to exclude certain terms with wildcards from the search results so it turns into something like this : 另外,它必须能够从搜索结果中排除某些带有通配符的术语,因此它变成了这样的东西:

SELECT name, other, stuff 
FROM my_table 
WHERE name LIKE '%some_name%' 
AND name NOT IN (SELECT name FROM my_table WHERE name LIKE '%term1%' AND name LIKE '%term2%' AND name LIKE '%term3%')

To top things off, I have two INNER JOINs and I have to execute it in PHP. 最重要的是,我有两个INNER JOIN,必须在PHP中执行它。 I have indexes on the table for relevant columns. 我在表上有相关列的索引。 It is on a server that is fast for pretty much every other query I can throw at it. 它在服务器上可以快速处理几乎所有我可以向其抛出的查询。

The question is, is there a different method I could be using to do this type of query thats is quick? 问题是,是否可以使用其他方法快速进行这种类型的查询?

UPDATES 更新

This is my actual query after adding in FULLTEXT index to the chem table and trying the match function instead. 这是在将FULLTEXT索引添加到chem表中并尝试使用match函数之后的实际查询。 NOTE : I don't have control over table/column names. 注意:我无法控制表/列的名称。 I know they dont look nice. 我知道他们看起来不太好。

CREATE FULLTEXT INDEX name_index ON chems (name)

SELECT f.name fname, f.state, f.city, f.id fid,
cl.alt_facilid_ida, cl.alt_facili_idb,
c.ave, c.name, c.id chem_id,
cc.first_name, cc.last_name, cc.id cid, cc.phone_work
FROM facilities f
INNER JOIN facilitlt_chemicals_c cl ON (f.id = cl.alt_facilid485ilities_ida)
INNER JOIN chems c ON (cl.alt_facili3998emicals_idb = c.id)
LEFT OUTER JOIN facilities_contacts_c con ON (f.id = con.alt_facili_ida)
LEFT OUTER JOIN contacts cc ON (con.alt_facili_idb = cc.id)
WHERE match(c.name) against ('+lead -gas' IN BOOLEAN MODE)

That is just an example of a simple query. 那只是一个简单查询的例子。 The actual terms could be numerous. 实际条件可能很多。

You want to implement MySQL's full text capabilities on the columns you're searching. 您想在要搜索的列上实现MySQL的全文功能。 Read more about it here . 在此处了解更多信息。

Moreover, you probably want to do a boolean search: 此外,您可能想进行布尔搜索:

select
    t1.name,
    t1.stuff
from
    my_table t1
where
    match(t1.name) against ('+some_name -term1 -term2 -term3' in boolean mode)

I would suggest to use external Full-text engine like Lucene or Sphinx. 我建议使用外部全文引擎,如Lucene或Sphinx。 Especially if you plan to fire search queries against relatively big datasets. 特别是如果您打算针对较大的数据集触发搜索查询。

In Sphinx case you can use SELECT-like syntax called SphinxQL which will do exactly as required: 在Sphinx的情况下,您可以使用类似SELECT的语法,称为SphinxQL,它将完全按照要求执行:

SELECT * FROM <sphinx_index_name> WHERE MATCH('some_name -term1 -term2 -term3');

as described in http://sphinxsearch.com/docs/current.html#extended-syntax (NOT operator in your case). http://sphinxsearch.com/docs/current.html#extended-syntax中所述 (在您的情况下为NOT运算符)。 Plus you could enable morphology support which might be helpful as well. 另外,您可以启用形态学支持,这可能也会有所帮助。

You can combine MySQL and Sphinx queries with http://sphinxsearch.com/docs/current.html#sphinxse 您可以将MySQL和Sphinx查询与http://sphinxsearch.com/docs/current.html#sphinxse结合使用

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

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