PHP uses PDO to read a large amount of data processing from mysql.
Preface
This article describes how PHP uses PDO to read a large amount of data from mysql and share the content for your reference. I will not talk about it here. Let's take a look at the details.
Environment
- Mysql: 5.6.34
- Php 5.6
- Nginx: php-fpm
Applicable scenarios
Data Set service needs to be processed
- Export services that read certain data from mysql
- Some mysql business operations need to be processed at one time, such as update and deletion.
- More operations to process certain datasets
Key pdo settings
$ Dbh = new \ PDO ($ dsn, $ user, $ pass); # key settings. If this parameter is not set, php will still retrieve data from pdo once to php $ dbh-> setAttribute (\ PDO: MYSQL_ATTR_USE_BUFFERED_QUERY, false ); // The cursor attribute in perpare is not required $ something = $ dbh-> prepare ("SELECT * FROM 'order'", array (\ PDO: ATTR_CURSOR => \ PDO:: CURSOR_SCROLL); $ something-> execute ();
Generator
Generator, iterative Data Operations
This generator can be omitted
Try code
Class Test {public function test () {set_time_limit (0); $ dbms = 'mysql'; // Database Type $ host = C ('db _ host '); // Database Host NAME $ dbName = C ('db _ name'); // used database $ user = C ('db _ user '); // database connection username $ pass = C ('db _ pwd'); // password $ dsn = "$ dbms: host = $ host; dbname = $ dbName "; $ dbh = new \ PDO ($ dsn, $ user, $ pass); $ dbh-> setAttribute (\ PDO: MYSQL_ATTR_USE_BUFFERED_QUERY, false ); $ something = $ dbh-> prepare ("SELECT * FROM 'order'"); $ something-> execute () ; $ I = 0; $ newLine = PHP_SAPI = 'cli '? "\ N": '<br/>'; foreach ($ this-> cursor ($ something) as $ row) {// var_dump ($ row ); echo $ row ['id']. $ newLine; $ I ++;} echo "memory consumption :". (apsaradb for memory_get_usage ()/1024/1024 ). "M ". $ newLine; echo "number of rows processed :". $ I. $ newLine; echo "success";} public function cursor ($ something) {while ($ row = $ something-> fetch (\ PDO: FETCH_ASSOC )) {yield $ row ;}}$ test = new Test (); $ test-> test ();
Output
1 .. // omitted part of id804288 memory consumption: "0.34918212890625M processed data rows: 254062 success
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.