explain 出来的结果。。+----+-------------+-------+--------+---------------+------------+---------+--------------------------------------+------+--------------------------+
| id | select_type | table | type   | possible_keys | key        | key_len | ref                                  | rows | Extra                    |
+----+-------------+-------+--------+---------------+------------+---------+--------------------------------------+------+--------------------------+
|  1 | SIMPLE      | pd    | range  | PRIMARY       | PRIMARY    | 8       | NULL                                 |  168 | Using where; Using index |
|  1 | SIMPLE      | p     | eq_ref | PRIMARY       | PRIMARY    | 4       | kleehaus_wedding.pd.product_id       |    1 | Using where              |
|  1 | SIMPLE      | p2s   | eq_ref | PRIMARY       | PRIMARY    | 8       | kleehaus_wedding.pd.product_id,const |    1 | Using where; Using index |
|  1 | SIMPLE      | ps    | ref    | product_id    | product_id | 4       | kleehaus_wedding.p.product_id        |    1 | Using index              |
+----+-------------+-------+--------+---------------+------------+---------+--------------------------------------+------+--------------------------++----+-------------+-------+--------+---------------------+-------------+---------+---------------------------------------+------+--------------------------+
| id | select_type | table | type   | possible_keys       | key         | key_len | ref                                   | rows | Extra                    |
+----+-------------+-------+--------+---------------------+-------------+---------+---------------------------------------+------+--------------------------+
|  1 | SIMPLE      | p2c   | ref    | PRIMARY,category_id | category_id | 4       | const                                 |  436 | Using where              |
|  1 | SIMPLE      | pd    | eq_ref | PRIMARY             | PRIMARY     | 8       | kleehaus_wedding.p2c.product_id,const |    1 | Using where; Using index |
|  1 | SIMPLE      | p     | eq_ref | PRIMARY             | PRIMARY     | 4       | kleehaus_wedding.p2c.product_id       |    1 | Using where              |
|  1 | SIMPLE      | ps    | ref    | product_id          | product_id  | 4       | kleehaus_wedding.p.product_id         |    1 | Using index              |
|  1 | SIMPLE      | p2s   | eq_ref | PRIMARY             | PRIMARY     | 8       | kleehaus_wedding.pd.product_id,const  |    1 | Using where; Using index |
+----+-------------+-------+--------+---------------------+-------------+---------+---------------------------------------+------+--------------------------+
第一个SQL,TYPE为range,扫描的行数只有168,运行的速度为:0.02sec
第二个SQL, TYPE为ref,扫描的行数为436,运行的速度为:0.02sec - 0.03sec从上面EXPLAIN出来的数据,哪个SQL会更好些。。

解决方案 »

  1.   

    没有版表结构、SQL语句,只从扫描的行数来看,似乎第1个要好些
      

  2.   

    看explain的效果应该是第一个好点
      

  3.   

    但我有个疑问,为什么show processlist的时候,第一个SQL经常都会出现的???但执行的效率并不低。
      

  4.   

    似乎你应该先把SQL贴出来,并把表关系和索引关系贴出来,这样给你的建议才会更全面一点