简体   繁体   English

如何加快查询速度?

[英]How to speed this query up?

I'm using yii framework to create a website takes care of poems and words and stuff... 我正在使用yii框架创建一个网站来处理诗歌,单词和其他东西...

the problem is that the database has more than 250,000 record of messages and more than 500,000 records of keywords and relationships :S 问题是数据库有超过250,000条消息记录以及超过500,000条关键字和关系记录:S

what I'm trying to say that I want to make an optimal query to get all related messages of a specific message depending on key-tags.. 我要说的是,我想做出一个最佳查询,以根据键标获取特定消息的所有相关消息。

what I did so far is caching the query, but it's still slow when opening it for the first time and that's a big problem! 到目前为止,我所做的是缓存查询,但是在第一次打开它时仍然很慢,这是一个大问题!

here is my code: 这是我的代码:

    public function getRelatedMessages()
    {
        $id = $this->id;
        $dependency = "select `messages`.id,count(id) as tag_count from messages left join `messages_tags` on `messages`.`id` = `messages_tags`.mid 
            where `messages_tags`.`tagid` in (select `tags`.`id` from tags left join `messages_tags` on tags.id = `messages_tags`.tagid 
            where `messages_tags`.`mid` = {$id}) and id <> {$id} group by id order by tag_count desc";
        $dependency =  Messages::model()->cache(900)->findAllBySql($dependency);
        foreach ($dependency as $dependency) {
            $dependency1[] = $dependency['id'];
        }
        $dependency = implode(", ", $dependency1);
        $sql = "select * from messages where id in ({$dependency})  limit 4";
        $relateds =  Messages::model()->findAllBySql($sql);
        $db = array();
        foreach($relateds as $related) {
            $db[] = $related;
        }
        if(!$relateds || count($db)<4) {
            $limit = 4-count($db);
            $sql = "select `messages`.id, `messages`.url_key, `messages`.photo_url, `messages`.message from messages order by rand(), likes desc, comments desc, impressions desc, reported asc limit {$limit};";

            $relateds =  Messages::model()->cache(900)->findAllBySql($sql);
            foreach($relateds as $related) {
                $db[] = $related;
            }
        }

        return $db;
    } 

the code above selects only 4 records from the related messages after filtering them and ordering them and stuff.. the problem is with "order by" but I need it :S 上面的代码在过滤掉相关消息并对其进行排序和填充之后,从相关消息中仅选择4条记录。问题在于“ order by”,但我需要它:S

sorry if that was too long.. 抱歉,如果时间太长。

thank you :) 谢谢 :)

You could improve the first query by not using findAllBySql() and instantiating all the ActiveRecord classes. 您可以通过不使用findAllBySql()并实例化所有ActiveRecord类来改进第一个查询。 You don't need them, so use plain sql query like this (query stays the same but code around has changed): 您不需要它们,因此请使用像这样的普通sql查询(查询保持不变,但周围的代码已更改):

       // ...
       $dependency = $this->dbConnection->createCommand("
           select `messages`.id, count(id) as tag_count
           from messages left join `messages_tags` on `messages`.`id` = `messages_tags`.mid 
           where
               `messages_tags`.`tagid` in 
                  (select `tags`.`id` from tags left join `messages_tags` on tags.id = `messages_tags`.tagid where `messages_tags`.`mid` = {$id})
               and id <> {$id}
           group by id order by tag_count desc")->queryColumn();

       $dependency = implode(", ", $dependency);
       $sql = "select * from messages where id in ({$dependency})  limit 4";
       $relateds =  Messages::model()->findAllBySql($sql);
       // ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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