1

我正在尝试优化我的查询以使网站加载更快

SELECT ac.id as id, ac.first_name, ac.last_name, ac.email, ac.company_name, upd8r_twitter_accts.id as twitter, upd8r_facebook_accts.id as facebook 
FROM upd8r_user_accts ac  
LEFT OUTER JOIN upd8r_twitter_accts ON ac.id = upd8r_twitter_accts.user_id 
LEFT OUTER JOIN upd8r_facebook_accts ON ac.id = upd8r_facebook_accts.user_id 
WHERE ac.`rfid` = '' AND ac.last_name != '' AND ac.`owner_id` = '114' 
ORDER BY ac.`last_name` asc 

没有连接,查询在 0.0001 秒内运行,但有连接,它运行 0.3432 秒。

我该如何加快速度?

4

1 回答 1

1
SELECT  ac.id as id, ac.first_name, ac.last_name, ac.email, ac.company_name,
        (
        SELECT  id
        FROM    upd8r_twitter_accts
        WHERE   upd8r_twitter_accts.user_id = ac.id
        ORDER BY
                id
        LIMIT 1
        ) twitter,
        (
        SELECT  id
        FROM    upd8r_facebook_accts
        WHERE   upd8r_facebook_accts.user_id = ac.id
        ORDER BY
                id
        LIMIT 1
        ) facebook
FROM    upd8r_user_accts ac
WHERE   ac.`rfid` = ''
        AND ac.`owner_id` = '114'
        AND ac.last_name > ''
ORDER BY
        ac.`last_name` asc

创建以下索引:

upd8r_user_accts (rfid, owner_id, last_name)
upd8r_twitter_accts (user_id, id)
upd8r_facebook_accts (user_id, id)
于 2012-05-30T09:16:18.433 回答