php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #20374 Performance enhancment for php_mysql_do_connect.
Submitted: 2002-11-11 17:45 UTC Modified: 2002-11-11 21:09 UTC
From: ngaugler at ngworld dot net Assigned: zak (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 4.2.2 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ngaugler at ngworld dot net
New email:
PHP Version: OS:

 

 [2002-11-11 17:45 UTC] ngaugler at ngworld dot net
While investigating server performance I noticed that my queries per second was unusually high, as well as my SHOW STATUS commands in MySQL.  After investigating through the source, I found the function php_mysql_do_connect, which of course does connections.  Within there, line 602 of php_mysql.c for 4.2.2, I found a mysql_stat() where the MySQL connection was tested, and upon failure would reconnect.  My suggestion for PHP, and what I am placing into my own source, is instead of a mysql_stat, a mysql_ping is ran, if the API library is above 3.22.3 (http://www.mysql.com/documentation/mysql/bychapter/index.html#News-3.22.3)  Per the MySQL documentation:

http://www.mysql.com/documentation/mysql/bychapter/manual_Clients.html#mysql_ping


int mysql_ping(MYSQL *mysql) 

8.4.3.164 Description
Checks whether the connection to the server is working. If it has gone down, an automatic reconnection is attempted. 

This function can be used by clients that remain idle for a long while, to check whether the server has closed the connection and reconnect if necessary. 

8.4.3.165 Return Values
Zero if the server is alive. Non-zero if an error occurred. 



Since the libmysqlclient API will already reconnect, there is no need for PHP to do this internally.  If the API version is equal to or above 3.22.3, run a mysql_ping and upon failure, remove the connection from the zend_hash and return failure.  If it is below 3.22.3 (which is 3 years old), run the old source.  I know this is not a huge change, but it does make alittle difference in performance.  Below is a test.c example of how this functions:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mysql.h>

int main (void) {
        MYSQL mysql_connect;
        MYSQL_RES *mysql_result;
        MYSQL_ROW mysql_row;
        my_ulonglong id;
        int c;

        mysql_init(&mysql_connect);
        mysql_real_connect(&mysql_connect,"localhost","root","inT99hB$", "mysql", 0, NULL, 0);
        for (c = 0; c < 3; c++) {
                sleep(2);

                mysql_ping(&mysql_connect);
                printf("Error: %s\n", mysql_error(&mysql_connect));

                if (mysql_real_query(&mysql_connect,"SELECT CONNECTION_ID()",sizeof("SELECT CONNECTION_ID()"))) {
                        printf("Error querying: %s\n", mysql_error(&mysql_connect));
                } else {
                        mysql_result = mysql_store_result(&mysql_connect);
                        mysql_row = mysql_fetch_row(mysql_result);

                        id = strtoul(mysql_row[0], NULL, 0);
                        mysql_free_result(mysql_result);

                        printf("Id is %lu\n", id);
                        mysql_kill(&mysql_connect, id);
                }
        }
}



Example of php_mysql.c
#if MYSQL_VERSION_ID > 32230 /* let mysql_ping check connection and auto reconnect */
                        if(mysql_ping(le->ptr)) {
                                php_error(E_WARNING, "MySQL:  Link to server lost, unable to reconnect");
                                zend_hash_del(&EG(persistent_list), hashed_details, hashed_details_length+1);
                                efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
                        }
#else

... normal code...

#endif


Bests,

nickg

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-11 21:09 UTC] zak@php.net
Thanks for the excellent and complete suggestion - it has been implemented in CVS.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 00:01:30 2024 UTC