简体   繁体   English

PHP / MySQL性能问题

[英]PHP/MySQL performance question

Just had a quick question for those of you knowledgeable about performance. 对于那些了解性能的人来说,只是有一个快速的问题。 I have created a "MySQL" class, and every time I perform a query, I create a new MySQL object. 我创建了一个“MySQL”类,每次执行查询时,我都会创建一个新的MySQL对象。 See below 见下文

    public function get_am_sales() {
        $mysql = new MySQL();
        $mysql->connect();
        $query = "some query";
        $mysql->query($query);
        $result = $mysql->return_assoc();
        unset($mysql);
        return $result;
    }

    public function get_pm_sales() {
        $mysql = new MySQL();
        $mysql->connect();
        $query = "some query";
        $mysql->query($query);
        $result = $mysql->return_assoc();
        unset($mysql);
        return $result;
    }

    public function get_comp_sales() {
        $mysql = new MySQL();
        $mysql->connect();
        $query = "some query";
        $mysql->query($query);
        $result = $mysql->return_assoc();
        unset($mysql);
        return $result;
    }

$sales = new Sales();
$amSales = $sales->get_am_sales();
$pmSales = $sales->get_pm_sales();
$compSales = $sales->get_comp_sales();

The code above obviously works, but I was wondering if this is a performance hit since I open and close a connection with every function call. 上面的代码显然有效,但我想知道这是否因为我打开和关闭每个函数调用的连接而受到性能影响。 I have tried to implement the class using one connection, but I get errors. 我试图使用一个连接实现该类,但我得到错误。 See below 见下文

    public function connect() {
        $this->mysql = new MySQL();
        $this->mysql->connect();
    }

    public function get_am_sales() {
        $query = "SELECT site_id, dly_sls AS am_sales, cov_cnt AS am_covers
                  FROM v_sales_yesterday
                  WHERE am_pm = 'AM'
                  GROUP BY site_id
                  ORDER BY site_id ASC";
        $this->mysql->query($query);
        $result = $this->mysql->return_assoc();
        return $result;
    }

    public function get_pm_sales() {
        $query = "SELECT site_id, dly_sls AS pm_sales, cov_cnt AS pm_covers
                  FROM v_sales_today
                  WHERE am_pm = 'PM'
                  GROUP BY site_id
                  ORDER BY site_id ASC";
        $this->mysql->query($query);
        $result = $this->mysql->return_assoc();
        return $result;
    }

    public function get_comp_sales() {
        $query = "SELECT business_date, site_id, dly_sls AS comp_sales
                  FROM v_sales_today_comp 
                  WHERE am_pm = 'PM'
                  GROUP BY site_id
                  ORDER BY site_id ASC";
        $this->mysql->query($query);
        $result = $this->mysql->return_assoc();
        return $result;
    }

    public function disconnect() {
        unset($this->mysql);
    }

$sales = new Sales();
$sales->connect();
$amSales = $sales->get_am_sales();
$pmSales = $sales->get_pm_sales();
$compSales = $sales->get_comp_sales();
$sales->disconnect();

Does the mysql connection close after the "connect" function executes? “connect”函数执行后mysql连接是否关闭? If deemed necessary, what would be the best way to keep the connection open (using an object oriented approach)? 如果认为有必要,保持连接打开的最佳方法是什么(使用面向对象的方法)? Please let me know if you need anymore details, and I appreciate the help in advance. 如果您需要更多细节,请告诉我,我提前感谢您的帮助。

This is like totally unnecessary. 这完全没必要。 You dont have to open and close connections for each query. 您不必为每个查询打开和关闭连接。 Are you using some kind of a framework? 你在使用某种框架吗? If not you must as all frameworks have active records and DB helpers to make interacting with DB easier. 如果不是,您必须因为所有框架都有活动记录和DB帮助程序,以便更容易与DB交互。 THis would be a great article for you 这对你来说是一篇很棒的文章

http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/ http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/

has just what youre lookng for. 正是你所期待的。

In general, you only want to create your MySQL instance once during the execution of the application. 通常,您只想在执行应用程序期间创建一次MySQL实例。

If you have an overarching Application object then you could just create your MySQL instance inside of the Application object and access it from there. 如果您有一个总体的Application对象,那么您可以在Application对象内部创建MySQL实例并从那里访问它。

The other alternative is to create a Singleton object for your MySQL instance that is then accessed via an 'instance()' method. 另一种方法是为MySQL实例创建一个Singleton对象,然后通过'instance()'方法进行访问。

This way you only open a single connection during the execution cycle of your php script and it will be discarded at the end of script execution. 这样,您只需在php脚本的执行周期内打开一个连接,并在脚本执行结束时将其丢弃。

So now your code looks like so: 所以现在你的代码看起来像这样:

$query = "SELECT * FROM users";

$rs = Mysql::instance()->query($query);

If you need working examples of how to setup a Singleton instance for your purposes you can look at: 如果您需要有关如何为您的目的设置Singleton实例的工作示例,您可以查看:

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

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