duanluanlang8501 2011-11-28 23:51
浏览 17
已采纳

使用SimplePie使用RSS源进行优化的数据库插入 - 需要创意

So basically I have just benchmarked my update_feeds controller and found that the amount of sql queries that are run are shocking. I am looking for a way to optimise the process of fectching multiple feeds and then insert the data into a table (title and url)

Currently there are 193 Feeds in the DB that I fetch the URL's for and then I process these one by one checking and insert data of them into another table. The problem is simplepie goes through and inserts every item.

So I am ending up with QUERIES: 3297 Total Execution Time 202.8051

I am looking for a way to optimise this process does anyone have any tips? I will post some code. Thanks.

Controller for fetching the feeds

          $this->output->set_profiler_sections($sections);
        $this->load->library('simplepie');
        //$this->simplepie->cache_location = BASEPATH .'cache';
        $this->load->model('FeedModel');
        $this->load->model('FeedItemModel');
        $feeds = $this->FeedModel->get_feed_update_urls();
        foreach ($feeds as $feed_id => $feed_url) {
            $this->simplepie->set_feed_url($feed_url);
            //$this->simplepie->set_cache_duration(0);
$this->simplepie->set_timeout(0);
            $this->simplepie->init();
            $items = $this->simplepie->get_items();
            foreach ($items as $item) {
                $this->FeedItemModel->load($feed_id, md5($item->get_id()));
                $this->FeedItemModel->link = $item->get_permalink();
                $this->FeedItemModel->title = $item->get_title();
                $this->FeedItemModel->created_time = $item->get_date('Y-m-d H:i:s');
                $this->FeedItemModel->save();
            }
        }

Model for inserting feeds

function save() {
        if ($this->_id !== false) {
            $this->db->query('UPDATE feed_items SET link=?, title=?, created_time=? WHERE id=?', array($this->link, $this->title, $this->created_time, $this->_id));
        } else {
            $this->db->query('INSERT INTO feed_items(feed_id, remote_id, link, title, created_time, updated_time) VALUES (?, ?, ?, ?, ?, NOW()) ON DUPLICATE KEY UPDATE remote_id=remote_id', array($this->feed_id, $this->remote_id, $this->link, $this->title, $this->created_time, $this->remote_id));
            $this->_id = $this->db->insert_id();
        }
    }
  • 写回答

1条回答 默认 最新

  • duanjuduo4573 2011-11-29 00:28
    关注

    Instead of loading every single time and then saving, you should instead do an update (and then insert when update doesn't find rows). That will reduce by a single query per feed item.

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?