简体   繁体   中英

MYSQL two tables query vs one of a time

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM