简体   繁体   English

SQL查询性能LIKE和wildecards vs. IN

[英]SQL query performance LIKE and wildecards vs. IN

I just wounder if there will be a big performance difference whit these 2 queries 我只是觉得如果这2个查询会有很大的性能差异

SELECT 
  `items_id`, `sport_id`, `sport`, `title`, `url`, 
  `Select3`, `Select6`, `id`, `data`,`image` as image,
  concat('index.php?option=sports&item=',`items_id`,'&p=C108-M108') as count_url
FROM 
  qy9zh_dataitems
WHERE Select6 LIKE '%sport%'
ORDER BY `Select9` DESC LIMIT 0, 4


SELECT 
  `items_id`, `sport_id`, `sport`, `title`, `url`, 
  `Select3`, `Select6`, `id`, `data`,`image` as image,
  concat('index.php?option=sports&item=',`items_id`,'&p=C108-M108') as count_url
FROM 
  qy9zh_dataitems 
WHERE Select6 in ('sport') 
ORDER BY `Select9` DESC LIMIT 0, 4

The queries are working just fine.. both of them.. just worried about the performance :) 查询工作得很好..他们两个..只是担心性能:)

Edit: A strange thing when I do some testing is this: 编辑:当我做一些测试时,一个奇怪的事情是:

SELECT * FROM qy9zh_dataitems WHERE Select6 in ('kvinna') LIMIT 0, 40000

29,051 total, Query took 0.2581 sec 共计29,051次,查询耗时0.2581秒

SELECT * FROM qy9zh_dataitems WHERE Select6 LIKE ('%kvinna%') LIMIT 0, 40000

29,113 total, Query took 0.2218 sec 总共29,113,查询花了0.2218秒

If an index is defined on a column and you are looking for the beginning of this column as in 如果在列上定义了索引,并且您正在查找此列的开头,请参阅

name LIKE 'Jo%' 

the index can be used to speed up the query. 索引可用于加速查询。 However, if you are looking for any part of a word as in 但是,如果您正在寻找单词的任何部分,请参阅

name LIKE '%man%' 

The index cannot be used. 索引无法使用。

The IN clause is generally very fast, however I do not know if it can take advantage of an index. IN子句通常非常快,但我不知道它是否可以利用索引。

The speed of the query depends on the availability of an appropriate index, on the size of the table, on the distribution of the values (many rows with the same values, each row with different values) and of course on the query itself. 查询的速度取决于适当索引的可用性,表的大小,值的分布(具有相同值的许多行,每行具有不同的值),当然还取决于查询本身。 I think that your question cannot be generally answered. 我认为你的问题不能得到普遍回答。 Make tests and compare! 进行测试并进行比较!

These are not even close to being the same queries, and therefore the performance can't be compared. 这些甚至不是接近相同的查询,因此无法比较性能。

The second query can use an index on Select6 to limit the rows to those that equal sport , while the first has to look at every single row to see if it contains sport anywhere in the contents of Select6 . 第二个查询可以使用索引Select6将行限制为那些平等sport ,而首先要看看每一行,看它是否包含sport中的内容随时随地Select6 You're trying to compare apples and oranges. 你想比较苹果和橘子。

These are very different criteria. 这些是非常不同的标准。

WHERE Select6 LIKE '%sport%'

will look for the case where select6 contains the word sport. 将查找select6包含单词sport的情况。

WHERE Select6 in ('sport') 

will look for the case where select6 is the word sport. 将寻找select6是单词sport的情况。

An IN criteria allows you to specify a list of possible values, and should be very fast, if you've indexed the field. IN条件允许您指定可能值的列表,如果您已为该字段建立索引,则应该非常快。 A like, especially a like that is looking for the word anywhere in a string can be difficult to index at all. 类似的,特别是在字符串中的任何地方寻找单词的类似可能很难索引。 Where select6 like 'sport%' would look for a case where select6 starts with the word sport and would be indexable. Where select6 like 'sport%'会查找select6以单词sport开头并且可以索引的情况。

As everyone else posted, basic indexes cannot be used for searches with a leading wild card. 正如其他人发布的那样,基本索引不能用于使用前导外卡的搜索。 If you think those queries are going to be the most common, you want to look at the Full Text Search functions in MySQL. 如果您认为这些查询最常见,那么您需要查看MySQL中的全文搜索功能。

Full Text Searches provide an efficient way of finding records based on individual words in text. 全文搜索提供了一种基于文本中单个单词查找记录的有效方法。 But even then there will be limitations (for example, searching for the word "port" would not necessarily return records like "sport"). 但即便如此,也会有局限性(例如,搜索“port”这个词并不一定会返回像“sport”这样的记录)。

MySQL Full Text Search MySQL全文搜索

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

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