简体   繁体   中英

PHP mySql large data

I am trying to get data from a table which having million of records in it. My query is based on some other tables also which cause many joins. Now once we execute this query its hang the database and taking too much time to execute and during this time all other operations with the database stop working.

Can anyone help me what is the best way to handle such type of queries?

Regards

If you're using PDO as your MySQL driver, try this class (namespace it or rename it to avoid conflicts, as needed):

<?php
class MySQL {

    private $result;

    /**
     * Constructor establishes a database connection via PDO.
     */
    public function __construct($host, $port, $dbname, $username, $password, $sql) {
        try {
            $db = new PDO(
                sprintf( "mysql:host=%s;port=%s;dbname=%s", $host, $port, $dbname ),
                $username,
                $password,
                array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
            );
            $stmt = $db->prepare($sql, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false));
            $this->result = $db->query($stmt);

        } catch ( PDOException $e ) {
            sprintf("MySQL Connection Error: %s", $e->getMessage()));
        }
    }

    /**
     * Get Next Table Row
     *
     * @return stdClass Get next table row as a stdObject.
     */
    public function next_row() {
        return $this->result->fetchObject();
    }

}

The constructor accepts all of your database variables needed to establish a connection, as well as the $sql string for your query. The option PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false should keep your query from timing out by not loading all of the results into memory at once.

Example usage:

<?php
$host = "127.0.0.1";
$port = "3306"
$dbname = "mydatabase"
$username = "user"
$password = "l33th@ck3r"
$sql = "SELECT * FROM table WHERE foo='bar'";
$mysql = new MySQL($host, $port, $dbname, $username, $password, $sql);

while ($row = $mysql->next_row()) {
    // do stuff here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM