我有这个索引:
alter table wx_cbmessage add index main_query (cbm_owner_id,cbm_status,cbm_date,cbm_notification_date);
这个查询:
SELECT SQL_CALC_FOUND_ROWS ...
FROM `wx_cbmessage` `msg` FORCE INDEX (main_query)
LEFT JOIN wx_user `from` ON cbm_from_user_id=from.login
WHERE `cbm_owner_id`='username'
AND cbm_date <= 1431448013
AND (`cbm_notification_date`=0 OR cbm_notification_date <= 1431448013)
AND `cbm_status`=1
ORDER BY `cbm_date` DESC
LIMIT 25
是否有cbm_notification_date
作为索引的一部分?它可以被利用吗?
前两列只是完全匹配,但我不知道MySQL是否可以对多个范围搜索做任何事情。
答案 0 :(得分:1)
Please qualify the column names with the table aliases. Without knowing what column is what table, I have to guess at how to advise.
Two ranges -- Won't be used.
EXPLAIN failing to explain -- Try List<Part>
.
I recommend this (Since the columns are all in the same table):
EXPLAIN FORMAT=JSON SELECT ...
It can be used for 2 constants and a range. And it can for the INDEX(cbm_owner_id, cbm_status, cbm_date)
.
The optimizer has no good way to pick which one to use.
ORDER BY
cannot use an index because of the combination of (`cbm_notification_date`=0 OR cbm_notification_date <= 1431448013)
and 'range'. OR
is equivalent to (x=1 OR x=3)
, which is better than a range, although not quite as good as a constant.