简体   繁体   English

PHP,MySQL,FastCGI-处理许多查询的最佳方法

[英]PHP, MySQL, FastCGI - best way to handle many queries

I am trying to figure out the best way to handle db communication in PHP, MySQL setup through FastCGI and usig PHP-FPM; 我试图找出最好的方法来处理PHP,通过FastCGI和usig PHP-FPM进行MySQL设置中的数据库通信; This is for a relatively heavy use site where there is anywhere from 100 to 1,000 SQL queries a second so I would like to make things as efficient as possible. 这是一个相对繁忙的站点,每秒可以进行100到1,000个SQL查询,因此我想使事情变得尽可能高效。

I am rewriting parts of the website and in the new code I am utilizing PDO and have the below class to handle DB queries and connections by doing database::insertEmployee($name, $SIN, $DOB, $position). 我正在重写网站的某些部分,并在新代码中利用PDO,并具有下面的类通过执行database :: insertEmployee($ name,$ SIN,$ DOB,$ position)处理数据库查询和连接。 My concern is that with every query a new PDO connection is established. 我担心的是,每次查询都会建立一个新的PDO连接。 Should I be trying to set up a persistent connection??? 我应该尝试建立持久连接吗???

class database
{
    protected $dbh;
    protected static $instance;

    private function __construct()
    {
        try {
        // building data source name from config
            $dsn = 'mysql:=' . DB_Config::read('db.host') .
                   ';dbname='  . DB_Config::read('db.name');
            $user = DB_Config::read('db.user');
            $password = DB_Config::read('db.password');
            $this->dbh = new PDO($dsn, $user, $password);
            $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            //@TODO-KP: log and alert
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        }

    }

    public static function getInstance()
    {
        if (!isset(self::$instance)) {
            $object = __CLASS__;
            self::$instance = new $object;
        }
        return self::$instance;
    }

    public static function insertEmployee($name, $position, $SIN, $DOB)
    {
        $dbi = self::getInstance();

        try {
            $sthEmployee = $dbi->dbh->prepare('INSERT INTO employees SET
                                                name = :name
                                                , position = :position
                                                , SIN = :SIN
                                                , DOB = :DOB'
            );

            $sthEmployee->bindParam(':name', $name);
            $sthEmployee->bindParam(':position', $position);
            $sthEmployee->bindParam(':SIN', $SIN);
            $sthEmployee->bindParam(':DOB', date('Y-m-d G:i:s', $DOB));

            return $sthEmployee->execute();

        } catch (PDOException $e) {
            //@FIXME-KP: log and alert
            print "Error!: " . $e->getMessage() . "-- name [$name]";
            return '';
        }
    }
}

Any thoughts on most efficient approach would be very, very appreciated! 关于最有效方法的任何想法将非常非常感谢!

Kathryn. 凯瑟琳

My concern is that with every query a new PDO connection is established. 我担心的是,每次查询都会建立一个新的PDO连接。

Well, verify your concern, because I would say this is likely not the case. 好吧,请验证您的担心,因为我会说事实并非如此。

For performance reasons, take care you're using the mysql native driver under the hood as this allows extended metrics of the interaction from PHP with the database. 出于性能原因,请小心使用引擎盖下的mysql本机驱动程序,因为这样可以扩展PHP与数据库之间的交互度量。

Also get a professional support plan from Oracle for Mysql, they have nice monitoring tools and very good support that should help you to get your database and PHP code ready for the traffic to handle. 还可以从Oracle for Mysql获得专业的支持计划,它们具有出色的监视工具和良好的支持,这些信息应有助于您准备好数据库和PHP代码以准备处理流量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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