-1

我有这个查询:

SELECT 
    cms_mod_lajmet_entry.LajmID, 
    page_id, date, 
    foto, eshte_video, 
    title, intro, 
    komente 
FROM cms_mod_lajmet_entry, cms_mod_lajmet_entry_language 
WHERE cms_mod_lajmet_entry.LajmID = cms_mod_lajmet_entry_language.LajmID 
    AND cms_mod_lajmet_entry.page_id = 2  
    AND cms_mod_lajmet_entry.publikimi = 1 
    AND cms_mod_lajmet_entry_language.language = 'sq' 
    AND cms_mod_lajmet_entry.renditja > 0 
ORDER by renditja ASC, date DESC LIMIT 1

(加载时间 = 0.1219)

当我拆分它们时:

SELECT 
    LajmID, 
    page_id, 
    date, 
    foto, 
    eshte_video, 
    komente 
FROM cms_mod_lajmet_entry 
WHERE cms_mod_lajmet_entry.page_id = 2  
    AND cms_mod_lajmet_entry.publikimi = 1 
    AND cms_mod_lajmet_entry.renditja > 0 ORDER by renditja ASC, date DESC LIMIT 1 

(加载时间 = 0.0801)

并单独查询:($t[LajmID] 是最后一个表中的 PrimaryID)

SELECT 
    title, 
    intro 
FROM cms_mod_lajmet_entry_language 
WHERE LajmID = $t[LajmID] 
    AND language = 'sq'

(加载时间 = 0.0006)

总计:0.1219 > 0.0807 (0.0801 + 0.0006)。

这看起来真的更快吗和/或是否有其他更快的方法。

指标如下:

第一张表:

LajmID BTREE 否 否 LajmID 36380 A
page_id 36380 A
publikimi 36380 A
klika 36380 A

第二张表:

LajmID BTREE 否 否 LajmID 38456 A
语言 38456 A

4

1 回答 1

0

我原本不打算回答,但我对这个问题的评论基本上回答了它。我的评论总结如下:

To get accurate query execution times, make sure you run the queries with SQL_NO_CACHE. This will make sure that the query cache doesn't skew the results. The index on the first table wasn't being used since LajmID was on the top of it and it wasn't used in the query.

于 2012-11-21T01:13:08.783 回答