---
title: "mysql query count sql 效能调优记录(-)"
author: "Robinson"
date: "2011-01-16T23:09:43Z"
tags: []
categories: ["数据库"]
slug: "mysql-query-count-sql"
draft: false
lastmod: "2011-03-23T13:15:16Z"
id: 875966
url: "https://robinson.iteye.com/blog/875966"
---

**count字段带来的低效sql**

 

**一、大概过程：**

1. set profiling=1;

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

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

30 rows in set (5.44 sec)

 

3.mysql&gt; explain select  com\_id ,company.name,count(company\_albums.id) as albums\_count  from company\_albums ,company  where company\_albums.com\_id=company.id group by company.name order by albums\_count desc limit 30;  
+----+-------------+----------------+--------+---------------+---------+---------+------------------------------------+-------+---------------------------------+  
| 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 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)

 

 

4。

mysql&gt; show profiles;

 

mysql&gt; show profiles;  
+----------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
| Query\_ID | Duration   | Query                                                                                                                                                                                                                            |  
+----------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+                                                                                                                                                                                                               |  
|       63 | 5.43254700 | select  com\_id ,company.name,count(company\_albums.id) as albums\_count  from company\_albums ,company  where company\_albums.com\_id=company.id group by company.name order by albums\_count desc limit 30                            |  
|       65 | 0.00039400 | explain select  com\_id ,company.name,count(company\_albums.id) as albums\_count  from company\_albums ,company  where company\_albums.com\_id=company.id group by company.name order by albums\_count desc limit 30                    |  
+----------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

 

**mysql&gt;** show profile cpu ,block io for query 63;  
+--------------------------------+----------+----------+------------+--------------+---------------+  
| Status                         | Duration | CPU\_user | CPU\_system | Block\_ops\_in | Block\_ops\_out |  
+--------------------------------+----------+----------+------------+--------------+---------------+  
| starting                       | 0.000036 | 0.000000 |   0.000000 |            0 |             0 |  
| checking query cache for query | 0.000104 | 0.000000 |   0.000000 |            0 |             0 |  
| Opening tables                 | 0.000019 | 0.000000 |   0.000000 |            0 |             0 |  
| System lock                    | 0.000009 | 0.000000 |   0.000000 |            0 |             0 |  
| Table lock                     | 0.000039 | 0.000000 |   0.000000 |            0 |             0 |  
| init                           | 0.000034 | 0.000000 |   0.000000 |            0 |             0 |  
| optimizing                     | 0.000013 | 0.000000 |   0.000000 |            0 |             0 |  
| statistics                     | 0.000039 | 0.000000 |   0.000000 |            0 |             0 |  
| preparing                      | 0.000018 | 0.000000 |   0.000000 |            0 |             0 |  
| Creating tmp table             | 0.001009 | 0.000000 |   0.000000 |            0 |             0 |  
| executing                      | 0.000017 | 0.000000 |   0.000000 |            0 |             0 |  
| **Copying to tmp table           | 5.428130 | 1.916120 |   5.116320 |**              0 |             0 |  
| Sorting result                 | 0.001783 | 0.012001 |   0.000000 |            0 |             0 |  
| Sending data                   | 0.000074 | 0.004000 |   0.000000 |            0 |             0 |  
| end                            | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |  
| removing tmp table             | 0.000727 | 0.000000 |   0.000000 |            0 |             0 |  
| end                            | 0.000016 | 0.000000 |   0.000000 |            0 |             0 |  
| query end                      | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |  
| freeing items                  | 0.000050 | 0.000000 |   0.000000 |            0 |             0 |  
| storing result in query cache  | 0.000403 | 0.000000 |   0.000000 |            0 |             0 |  
| logging slow query             | 0.000008 | 0.000000 |   0.000000 |            0 |             0 |  
| cleaning up                    | 0.000007 | 0.000000 |   0.000000 |            0 |             0 |  
+--------------------------------+----------+----------+------------+--------------+---------------+  
22 rows in set (0.00 sec)

 

**二、问题总结：**

通过以上company\_albums | ALL    | com\_idx  综合得知全表扫描了，试着分析和调整

 

**mysql&gt;** explain select  com\_id ,company.name,count(company\_albums.name) as albums\_count  from company\_albums ,company  where company\_albums.com\_id=company.id group by company.name order by albums\_count desc limit 30  ;  
+----+-------------+----------------+--------+---------------+---------+---------+------------------------------------+-------+---------------------------------+  
| 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 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;** explain 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 30  ;  
+----+-------------+----------------+-------+---------------+---------+---------+-------------------------+------+---------------------------------+  
| id | select\_type | table          | type  | possible\_keys | key     | key\_len | ref                     | rows | Extra                           |  
+----+-------------+----------------+-------+---------------+---------+---------+-------------------------+------+---------------------------------+  
|  1 | SIMPLE      | **company**        | **index | PRIMARY**        | name    | **767**     | NULL                    |    1 | Using temporary; Using filesort |  
|  1 | SIMPLE      | **company\_albums** | **ref   | com\_idx**        | com\_idx | **4**        | test\_01.company.id |  108 | Using index                     |  
+----+-------------+----------------+-------+---------------+---------+---------+-------------------------+------+---------------------------------+  
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.09 sec)

 

**三、结论**

现在优化后只需要(0.09 sec)了，写sql的时候尽量多思考和谨慎，频繁查询的更要仔细