在约束Query Slow?

时间:2013-03-08 10:16:19

标签: php mysql

我有这个需要时间的查询.Below就是那个查询 -

SELECT
    m1.mes_id,
    m1.body, 
    m1.subject,
    m1.frm_id AS mem_id,
    m1.date 
FROM 
    messages_system_new m1 
WHERE 
    m1.mes_id IN 
        (SELECT
            MAX(mn1.mes_id) AS mes_id 
        FROM messages_system_new mn1
        WHERE
            mn1.mem_id = '401' 
            AND mn1.frm_id != '401' 
            AND mn1.trashed_user NOT LIKE '%401%' 
            AND mn1.type = 'message' 
            AND mn1.folder = 'inbox' AND mn1.is_spam='N' 
        GROUP BY mn1.frm_id
        )

当解雇EXPLAIN时给予

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   PRIMARY m1  ALL (NULL)  (NULL)  (NULL)  (NULL)  6091    Using where
2   DEPENDENT SUBQUERY  mn1 ref mem_id,frm_id   mem_id  8   const   59  Using where; Using temporary; Using filesort

这意味着什么?如何改进查询?

1 个答案:

答案 0 :(得分:0)

可能会尝试使用JOIN来尝试。像这样的东西

SELECT
    m1.mes_id,
    m1.body, 
    m1.subject,
    m1.frm_id AS mem_id,
    m1.date 
FROM messages_system_new m1 
INNER JOIN (SELECT mn1.frm_id  MAX(mn1.mes_id) AS mes_id 
        FROM messages_system_new mn1
        WHERE
            mn1.mem_id = '401' 
            AND mn1.frm_id != '401' 
            AND mn1.trashed_user NOT LIKE '%401%' 
            AND mn1.type = 'message' 
            AND mn1.folder = 'inbox' AND mn1.is_spam='N' 
        GROUP BY mn1.frm_id
        ) Sub1
ON m1.mes_id = Sub1.mes_id

messafes_system_new上有哪些索引?