经过测试800万数据,越是执行到后面,查询速度越慢,执行了一晚上天才构造完成,通过后台查询得知,
mysql查询数据,当OFFSET 很大的时候,查询数据会非常慢,每次查询会消耗大量的IO,数据库会根据索引,依次排除前面的2000000条数据,最后得到1000条。
例如:select id,title,content from article limit 1000 offset 2000000 返回数据几十秒
所以,导入速度越来越慢的根本原因是查询数据。
如果把上面的sql改成:select id,title,content from article where id>100000 order by id asc limit 1000 offset 0,
修改办法:修改util\XSDataSource.class.php 的
一、修改XSDataSource类
1.增加全局变量$previd
1 protected $inCli; 2 private $dataList, $dataPos; 3 protected $previd=0;//上次执行得到的最大id
2.修改getData方法,把最后一条数据的id赋值给$previd
1 final public function getData() 2 { 3 if ($this->dataPos === null || $this->dataPos === count($this->dataList)) { 4 $this->dataPos = 0; 5 $this->dataList = $this->getDataList(); 6 if (!is_array($this->dataList) || count($this->dataList) === 0) { 7 $this->deinit(); 8 $this->dataList = $this->dataPos = null; 9 return false; 10 } 11 } 12 $data = $this->dataList[$this->dataPos]; 13 $this->dataPos++; 14 $this->previd=$data["id"];//循环赋值,最后剩下的肯定最大 15 return $data; 16 }
二、XSDatabaseDataSource类
1. 修改getDataList方法
1 if ($this->limit <= 0) { 2 return false; 3 } 4 $wheresql=" where id > ".$this->previd." order by id asc"; 5 //判断是否存在where 6 if(stripos($this->sql, "where") >0){ 7 $wheresql=" and id > ".$this->previd." order by id asc"; 8 } 9 $sql = $this->sql .$wheresql. ' LIMIT ' . min(self::PLIMIT, $this->limit) . ' OFFSET ' . $this->offset; 10 //构造出来的sql语句:select id,title,content from article where id>100000 order by id asc limit 1000 offset 0, 11 $this->limit -= self::PLIMIT; 12 //$this->offset += self::PLIMIT;//隐藏此处,offset便不会自增长 13 return $this->db->query($sql);