0

I have this entry in slow query log:

# 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; 

When I explain this query, it's absolutely fine.

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)

How do I optimize this query? Why does explain show 3 rows and slow query log says it examined 65207 rows.

And yes, I have tried FORCE INDEX, without forcing index and what not, it only gets worse.

4

1 回答 1

0

看起来查询无法使用id_viewed,因为它不在possible_keys列中,而extra列只是说Using where

我也猜测rows是指您已LIMIT编辑的返回行,而不是它必须扫描的行数..您可以删除LIMIT.EXPLAIN

我会尝试一个复合索引(id, approved),也许一个单一(viewed)的排序。

于 2014-08-08T16:18:10.877 回答