0

所以基本上我刚刚对我的 update_feeds 控制器进行了基准测试,发现运行的 sql 查询数量令人震惊。我正在寻找一种方法来优化 fectching 多个提要的过程,然后将数据插入到表中(标题和 url)

目前数据库中有 193 个提要,我为其获取 URL,然后我逐个处理这些提要,检查并将它们的数据插入到另一个表中。问题是 simplepie 遍历并插入每个项目。

所以我以查询结束:3297 Total Execution Time 202.8051

我正在寻找一种优化此过程的方法有人有任何提示吗?我会发布一些代码。谢谢。

用于获取提要的控制器

          $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();
            }
        }

用于插入提要的模型

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();
        }
    }
4

1 回答 1

0

而不是每次加载然后保存,您应该进行更新(然后在更新未找到行时插入)。这将减少每个提要项的单个查询。

于 2011-11-29T00:28:07.920 回答