Home  >  Article  >  Database  >  ## MySQL Pattern Matching: LIKE vs. LOCATE: Which is Faster?

## MySQL Pattern Matching: LIKE vs. LOCATE: Which is Faster?

DDD
DDDOriginal
2024-10-25 07:51:29151browse

##  MySQL Pattern Matching: LIKE vs. LOCATE: Which is Faster?

Comparing the Performance of MySQL LIKE vs LOCATE for Pattern Matching

MySQL provides two primary functions for performing pattern matching operations on strings: LIKE and LOCATE. While both serve similar purposes, they exhibit subtle differences in performance characteristics that can impact the efficiency of queries.

The LIKE operator utilizes a wildcard character (%) to match patterns within a string. For example:

<code class="sql">SELECT * FROM table WHERE column LIKE '%text%';</code>

The LOCATE function, on the other hand, searches for the first occurrence of a specific substring within a string and returns its position. A non-zero result indicates a match:

<code class="sql">SELECT * FROM table WHERE LOCATE('text', column) > 0;</code>

When comparing the performance of these two functions, it is generally observed that the LIKE operator performs marginally faster than LOCATE. This is primarily due to the fact that LIKE does not require an additional comparison ('> 0") to determine whether a match exists.

To illustrate this difference, let's consider the following benchmark results:

<code class="shell">mysql> SELECT BENCHMARK(100000000,LOCATE('foo','foobar'));
+---------------------------------------------+
| BENCHMARK(100000000,LOCATE('foo','foobar')) |
+---------------------------------------------+
|                                           0 |
+---------------------------------------------+

mysql> SELECT BENCHMARK(100000000,LOCATE('foo','foobar') > 0);
+-------------------------------------------------+
| BENCHMARK(100000000,LOCATE('foo','foobar') > 0) |
+-------------------------------------------------+
|                                               0 |
+-------------------------------------------------+

mysql> SELECT BENCHMARK(100000000,'foobar' LIKE '%foo%');
+--------------------------------------------+
| BENCHMARK(100000000,'foobar' LIKE '%foo%') |
+--------------------------------------------+
|                                          0 |
+--------------------------------------------+</code>

In this example, LIKE marginally outperforms LOCATE, taking approximately 0.65 seconds less to execute for 100 million iterations. However, it is important to note that the performance differences can vary depending on the length of the strings, the pattern being searched for, and the version of MySQL being used.

The above is the detailed content of ## MySQL Pattern Matching: LIKE vs. LOCATE: Which is Faster?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn