antevenio/pdo-mysql-select-iterator
最新稳定版本:0.1.14
Composer 安装命令:
composer require antevenio/pdo-mysql-select-iterator
包简介
PHP PDO Mysql select statement iterator implemented as multiple queries using LIMIT clauses
README 文档
README
PHP PDO Mysql select statement iterator implemented as multiple queries using LIMIT clauses.
What is this thing
So, you want to iterate through millions of table rows coming as a result of some MySQL select query because you want to do your thingie with them, but alas!, your lovely database admin doesn't like queries that stay for too long running in his server, or even worse, the server itself isn't able to hold them!. What do we do?, we do issue several queries that would fetch smaller blocks of rows in place.
This is an just a PHP iterator based on this concept. Upon receiving a SELECT query and a PDO database connection, an iterator is built so you can seamlessly traverse results without having to worry about anything else.
Install
To add as a dependency using composer:
composer require antevenio/pdo-mysql-select-iterator
Usage example
<?php $pdo = new PDO('mysql:host=localhost;dbname=kidsshouting', 'myuser', 'mypass'); $iterator = (new \PdoMysqlSelectIterator\Factory()) ->create($pdo, "select a,b from tbl order by a", 1000); // Get a total row count from the query if needed $total = count($iterator); foreach ($iterator as $item) { // Do your stuff with $item }
Notes
The factory will throw a InvalidQueryException on non "select" queries.
The factory will only return a LimitIterator when:
- The blockSize is > 0
- The query has an "order by" clause
- The query is not using any "rand()" functions
If any of the previous conditions is NOT met, the factory will return a non limit based iterator called "NativePDOIterator".
To ensure consistency among results, you might want to get the whole iteration and count inside a database transaction. The most suitable isolation level would be REPEATABLE-READ:
统计信息
- 总下载量: 19.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-23