【发布时间】:2014-08-08 15:49:05
【问题描述】:
我在慢查询日志中有这个条目:
# User@Host: user[host] @ [ip]
# Thread_id: 1514428 Schema: db Last_errno: 0 Killed: 0
# Query_time: 2.795454 Lock_time: 0.000116 Rows_sent: 15 Rows_examined: 65207 Rows_affected: 0 Rows_read: 65207
# Bytsent: 26618
SET timestamp=1407511874;
select off.*,translated_title,translated_description
from products off USE INDEX(id_viewed)
INNER JOIN members mem ON off.uid = mem.id
Left Join product_language_new pol ON off.id = pol.offer_id and pol.language='en'
where off.approved=1
order by off.viewed
LIMIT 15;
当我解释这个查询时,绝对没问题。
mysql> explain select off.*,translated_title,translated_description
from products off USE INDEX(id_viewed)
INNER JOIN members mem ON off.uid = mem.id
Left Join product_language_new pol ON off.id = pol.offer_id and pol.language='en'
where off.approved=1
order by off.viewed
LIMIT 15;
+----+-------------+-------+--------+-------------------------+-------------+---------+---------------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+-------------------------+-------------+---------+---------------------------+------+-------------+
| 1 | SIMPLE | off | index | NULL | id_viewed | 4 | NULL | 3 | Using where |
| 1 | SIMPLE | mem | eq_ref | PRIMARY | PRIMARY | 4 | db.off.uid | 1 | Using index |
| 1 | SIMPLE | pol | ref | offer_id,id_language | offer_id | 5 | db.off.id | 4 | |
+----+-------------+-------+--------+-------------------------+-------------+---------+---------------------------+------+-------------+
3 rows in set (0.17 sec)
如何优化此查询?为什么解释显示 3 行,慢查询日志说它检查了 65207 行。
是的,我尝试了 FORCE INDEX,没有强制索引,但它只会变得更糟。
【问题讨论】:
-
为所有表添加
create table语句
标签: mysql explain mysql-slow-query-log