有人知道哪一个更快吗:
SELECT * FROM table WHERE column LIKE '%text%';或
SELECT * FROM table WHERE LOCATE('text',column)>0;发布于 2011-09-21 20:58:04
添加于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)发布于 2014-02-27 11:08:21
+1到@Mchl,用于最直接地回答问题。
但我们应该记住,这两种解决方案都不能使用索引,因此它们必然会进行表扫描。
当你试图修补泰坦尼克号的船体时,试图决定是布还是塑料胶带是一种愚蠢的做法。
对于这种类型的查询,需要一个full-text search index。根据表的大小,这将是hundreds or thousands of times faster。
发布于 2014-02-27 10:58:24
我以Mchi did.And的身份做了一些测试,我想很难说哪一个更快。它看起来取决于第一次出现的子字符串。
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)https://stackoverflow.com/questions/7499438
复制相似问题