【问题标题】:MySQL LIKE vs LOCATEMySQL LIKE vs LOCATE
【发布时间】:2011-09-21 12:04:27
【问题描述】:

有谁知道哪个更快:

SELECT * FROM table WHERE column LIKE '%text%';

SELECT * FROM table WHERE LOCATE('text',column)>0;

【问题讨论】:

  • 为什么不对此进行基准测试/分析并找出答案?
  • @matro:什么是基准/配置文件.just for enthu
  • 点赞应该更快。全文索引和match against 会快得多。

标签: mysql


【解决方案1】:

2015 年 4 月 20 日添加:另请阅读下面的 Hallie's answer


第一个,但勉强。主要是因为它不需要进行额外的> 0 比较。

mysql> SELECT BENCHMARK(100000000,LOCATE('foo','foobar'));
+---------------------------------------------+
| BENCHMARK(100000000,LOCATE('foo','foobar')) |
+---------------------------------------------+
|                                           0 |
+---------------------------------------------+
1 row in set (3.24 sec)

mysql> SELECT BENCHMARK(100000000,LOCATE('foo','foobar') > 0);
+-------------------------------------------------+
| BENCHMARK(100000000,LOCATE('foo','foobar') > 0) |
+-------------------------------------------------+
|                                               0 |
+-------------------------------------------------+
1 row in set (4.63 sec)


mysql> SELECT BENCHMARK(100000000,'foobar' LIKE '%foo%');
+--------------------------------------------+
| BENCHMARK(100000000,'foobar' LIKE '%foo%') |
+--------------------------------------------+
|                                          0 |
+--------------------------------------------+
1 row in set (4.28 sec)


mysql> SELECT @@version;
+----------------------+
| @@version            |
+----------------------+
| 5.1.36-community-log |
+----------------------+
1 row in set (0.01 sec)

【讨论】:

【解决方案2】:

+1 @Mchl 最直接地回答问题。

但我们应该记住,这两种解决方案都不能使用索引,因此它们必须进行表扫描。

当您尝试修补泰坦尼克号的船体时,尝试在布绷带还是塑料绷带之间做出选择有点愚蠢。

对于这种类型的查询,需要full-text search index。根据表的大小,这将是hundreds or thousands of times faster

【讨论】:

  • MySQL全文的问题是目前不能进行通配符搜索,只能搜索前缀(foo*会匹配foobar,但*bar不会)。
  • Apache Lucene 和 Solr 有同样的限制。 Sphinx Search 支持中缀索引,因此您可以将通配符放在模式的开头。见sphinxsearch.com/docs/current/conf-min-infix-len.html
【解决方案3】:

我像 Mchi 一样做了一些测试。而且我认为很难说哪个更快。它看起来取决于子字符串的第一次出现。

mysql> select benchmark(100000000, 'afoobar' like '%foo%');
+----------------------------------------------+
| benchmark(100000000, 'afoobar' like '%foo%') |
+----------------------------------------------+
|                                            0 |
+----------------------------------------------+
1 row in set (9.80 sec)

mysql> select benchmark(100000000, locate('foo', 'afoobar'));
+------------------------------------------------+
| benchmark(100000000, locate('foo', 'afoobar')) |
+------------------------------------------------+
|                                              0 |
+------------------------------------------------+
1 row in set (8.08 sec)

mysql> select benchmark(100000000, 'abfoobar' like '%foo%');
+-----------------------------------------------+
| benchmark(100000000, 'abfoobar' like '%foo%') |
+-----------------------------------------------+
|                                             0 |
+-----------------------------------------------+
1 row in set (10.55 sec)

mysql> select benchmark(100000000, locate('foo', 'abfoobar'));
+-------------------------------------------------+
| benchmark(100000000, locate('foo', 'abfoobar')) |
+-------------------------------------------------+
|                                               0 |
+-------------------------------------------------+
1 row in set (10.63 sec)

mysql> select benchmark(100000000, 'abcfoobar' like '%foo%');
+------------------------------------------------+
| benchmark(100000000, 'abcfoobar' like '%foo%') |
+------------------------------------------------+
|                                              0 |
+------------------------------------------------+
1 row in set (11.54 sec)

mysql> select benchmark(100000000, locate('foo', 'abcfoobar'));
+--------------------------------------------------+
| benchmark(100000000, locate('foo', 'abcfoobar')) |
+--------------------------------------------------+
|                                                0 |
+--------------------------------------------------+
1 row in set (12.48 sec)

mysql> select @@version;
+------------+
| @@version  |
+------------+
| 5.5.27-log |
+------------+
1 row in set (0.01 sec)

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 2016-11-22
    • 2012-05-28
    • 2011-08-03
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多