---
title: "mysql query LIKE '%%' sql 效能调优记录(二)"
author: "Robinson"
date: "2011-01-16T23:22:12Z"
tags: []
categories: ["数据库"]
slug: "mysql-query-like-sql"
draft: false
lastmod: "2011-01-16T23:22:12Z"
id: 875972
url: "https://robinson.iteye.com/blog/875972"
---

对**LIKE '%%'这种与不加like的过滤导致sql性能差别很大**

 

**mysql&gt;** select  com\_id ,company.name,count(\*) as albums\_count from company\_albums ,company  
    -&gt; where company\_albums.com\_id=company.id  AND company\_albums.name LIKE '%%'  
    -&gt; group by company.name  
    -&gt; order by albums\_count desc limit 31;

 

+--------+-----------------------+--------------+  
| com\_id | name                  | albums\_count |  
+--------+-----------------------+--------------+  
\------

31 rows in set (4.41 sec)

**mysql&gt;** explain select  com\_id ,company.name,count(\*) as albums\_count from company\_albums ,company  
    -&gt; where company\_albums.com\_id=company.id  AND company\_albums.name **LIKE '%%'**  
    -&gt; group by company.name  
    -&gt; order by albums\_count desc limit 31;  
+----+-------------+----------------+--------+---------------+---------+---------+------------------------------------+-------+----------------------------------------------+  
| id | select\_type | table          | type   | possible\_keys | key     | key\_len | ref                                | rows  | Extra                                        |  
+----+-------------+----------------+--------+---------------+---------+---------+------------------------------------+-------+----------------------------------------------+  
|  1 | SIMPLE      | company\_albums | ALL    | com\_idx       | NULL    | NULL    | NULL                               | 72441 | Using where; Using temporary; Using filesort |  
|  1 | SIMPLE      | company        | eq\_ref | PRIMARY       | PRIMARY | 4       | test\_01.company\_albums.com\_id |     1 |                                              |  
+----+-------------+----------------+--------+---------------+---------+---------+------------------------------------+-------+----------------------------------------------+  
2 rows in set (0.00 sec)

 

**mysql&gt;** select  com\_id ,company.name,count(\*) as albums\_count from company\_albums ,company where company\_albums.com\_id=company.id  group by company.name order by albums\_count desc limit 31;  
+--------+-----------------------+--------------+  
| com\_id | name                  | albums\_count |  
+--------+-----------------------+--------------+  
\------

31 rows in set (0.08 sec)