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会更好些。。
| 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会更好些。。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货