Home > Database > Mysql Tutorial > body text

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

DDD
Release: 2024-10-25 07:51:29
Original
150 people have browsed it

##  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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!