PHP uses PDO to read a large amount of data processing from mysql.

Source: Internet
Author: User
Tags dsn

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.