dsbqfrr098575666 2012-11-20 18:36
浏览 27
已采纳

MYSQL两个表查询vs一次

I have this query:

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

(loading time = 0.1219)

when I split them:

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 

(loading time = 0.0801)

and query this alone: ($t[LajmID] is a PrimaryID from the last table)

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

(Loadin time = 0.0006)

in total: 0.1219 > 0.0807 (0.0801 + 0.0006).

Is this really faster as it looks and/or if there's any other faster ways.

Indexes are as follows:

First table:

LajmID BTREE No No LajmID 36380 A
page_id 36380 A
publikimi 36380 A
klika 36380 A

Second table:

LajmID BTREE No No LajmID 38456 A
language 38456 A

  • 写回答

1条回答 默认 最新

  • dppn67180 2012-11-21 01:13
    关注

    I wasn't originally planning on answering, but my comments on the question basically answered it. My comments are summarized below:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计