php7下pdo的各种fetch模式速度比较

简介

本文主要介绍通过压测数据来检验php7下pdo的各种fetch模式性能。

准备

测试用代码

<?php
// ***************需替换***************
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test_db;port=33062', 'root', 'pass');

// 性能检测脚本
function profile($text, $f) {
    $testCount = 5000;
    $totalTime = .0;
    for($i = 0; $i < $testCount; ++$i){
        $start = microtime(true);
        $rows = $f();
        $end = microtime(true);
        $time = $end - $start;
        $totalTime += $time;
    }
    $avg = $totalTime / $i;
    printf("$text [ avg ] %1.6f [total] %3.6f \n", $avg, $totalTime);
}

// fetch 测试表(table_name)映射类  ***************需替换***************
class FetchClass {
    public $id;
    public $name;
    public $created_at;
    public $updated_at;
}

// ***************select * from table_name, table_name需替换***************
// 1 : 按行读取+返回一个索引为结果集列名的数组
profile("fetch    ASSOC", function() use ($pdo) {
    $st = $pdo->prepare('select * from table_name');
    $st->setFetchMode(PDO::FETCH_ASSOC);
    $st->execute();
    $rows = [];
    while($row = $st->fetch()){
        $rows[] = $row;
    }
    return $rows;
});

// 2 : 按行读取+返回一个属性名对应结果集列名的匿名对象
profile("fetch    OBJ  ", function() use ($pdo) {
    $st = $pdo->prepare('select * from table_name');
    $st->setFetchMode(PDO::FETCH_OBJ);
    $st->execute();
    $rows = [];
    while($row = $st->fetch()){
        $rows[] = $row;
    }
    return $rows;
});

// 3 : 按行读取+返回一个请求类的新实例,映射结果集中的列名到类中对应的属性名。如果 fetch_style 包含 PDO::FETCH_CLASSTYPE(例如:PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE),则类名由第一列的值决定
profile("fetch    CLASS", function() use ($pdo){
    $st = $pdo->prepare('select * from table_name');
    $st->setFetchMode(PDO::FETCH_CLASS, 'FetchClass');
    $st->execute();
    $rows = [];
    while($row = $st->fetch()){
        $rows[] = $row;
    }
    return $rows;
});

// 4 : 按行读取+更新一个被请求类已存在的实例,映射结果集中的列到类中命名的属性
profile("fetch    INTO ", function() use ($pdo){
    $st = $pdo->prepare('select * from table_name');
    $fetchCls = new FetchClass;
    $st->setFetchMode(PDO::FETCH_INTO, $fetchCls);
    $st->execute();
    $rows = [];
    while($row = $st->fetch()){
                $rows[] = clone $row;
        }
    return $rows;
});

// 5 : 全部读取+返回一个索引为结果集列名的数组
profile("fetchAll ASSOC", function() use ($pdo) {
    $st = $pdo->prepare('select * from table_name');
    $st->setFetchMode(PDO::FETCH_ASSOC);
    $st->execute();
    $rows = $st->fetchAll();
    return $rows;
});

// 6 : 全部读取+返回一个属性名对应结果集列名的匿名对象
profile("fetchAll OBJ  ", function() use ($pdo) {
    $st = $pdo->prepare('select * from table_name');
    $st->setFetchMode(PDO::FETCH_OBJ);
    $st->execute();
    $rows = $st->fetchAll();
    return $rows;
});

// 7 : 全部读取+返回一个请求类的新实例,映射结果集中的列名到类中对应的属性名
profile("fetchall CLASS", function() use ($pdo){
    $st = $pdo->prepare('select * from table_name');
    $st->setFetchMode(PDO::FETCH_CLASS, 'FetchClass');
    $st->execute();
    $rows = $st->fetchAll();
    return $rows;
});

测试结果

pdo fetch model

性能评价

  • ASSOC==INTO>CLASS>OBJ

本文涉及源码

https://github.com/cangyan/TAV/tree/master/00025_PHP_PDO_FETCH

参考链接

https://qiita.com/a4_nghm/items/dc2369e8a675b5177af6

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注