MySQL非常偶然无法保存结果集

时间:2015-03-21 21:29:30

标签: php mysql

这是一个生产的PHP / MySQL系统,已经运行了几个月,大多没有问题。

很偶然,就像每隔几周一次,我看到"无法保存结果集"来自MySQL查询的错误。

我知道这可能是由一个大的结果集引起的,但这不是这里的情况。在最近的例子中,它发生在通常不超过10行的小表中(行表示活动游戏,并且在游戏结束后它们被移除 - 大量转换,但总是很小)。

查询非常简单:

SELECT player_1_id, player_2_id FROM minuetServer_games 
WHERE  ( player_1_id = '1513399' OR player_2_id = '1513399' )  
AND last_action_time < SUBTIME( CURRENT_TIMESTAMP, '0 0:01:22.000' ) 
FOR UPDATE;

此查询的目的是为给定的user_id(它们可以是p1或p2)找到任何不太陈旧的活动游戏。如果存在,我在下一个查询中更新了他们游戏的last_action_time(当然,当结果集未被保存时,更新就不会发生)。

我运行了CHECK TABLE,结果没问题。

虚假,偶尔会出现什么原因?#34;无法保存结果集&#34;警告?有办法解决吗?有一个坚实的解决方案吗?

编辑:

评论者要求提供有关他PHP配置的更多信息。这是phpinfo()输出的直接链接:

http://cordialminuet.com/info.php

EDIT2:

这是创建查询的代码:

global $tableNamePrefix;
cm_queryDatabase( "SET AUTOCOMMIT=0" );

global $moveTimeLimit;

$query = "SELECT player_1_id, player_2_id ".
    "FROM $tableNamePrefix"."games ".
    "WHERE ( player_1_id = '$user_id' OR player_2_id = '$user_id' ) ".
    " AND last_action_time < ".
    " SUBTIME( CURRENT_TIMESTAMP, '$moveTimeLimit' ) FOR UPDATE;";

// watch for deadlock here and retry
$result = cm_queryDatabase( $query, 0 );

这里是cm_queryDatabase的代码:

function cm_queryDatabase( $inQueryString, $inDeadlockFatal=1 ) {
    global $cm_mysqlLink;
    if( gettype( $cm_mysqlLink ) != "resource" ) {
        // not a valid mysql link?
        cm_connectToDatabase();
        }
    $result = mysql_query( $inQueryString, $cm_mysqlLink );

    // a bunch of error handling here if $result is FALSE
    //  ......

    // then finally

    return $result;
    }

这里是cm_connectToDatabase的代码:

function cm_connectToDatabase( $inTrackStats = true) {
    global $databaseServer,
           $databaseUsername, $databasePassword, $databaseName,
           $cm_mysqlLink;

    $cm_mysqlLink =
        mysql_connect( $databaseServer, 
                       $databaseUsername, $databasePassword );


    // bunch of error handling if $cm_mysqlLink is false
    // ....
    }

请注意&#34;无法保存结果集&#34;是mysql_query抛出的警告。此外,该查询每天运行数百次而没有问题。这个警告最多每隔几周发生一次。这是几天前最新的堆栈跟踪:

Warning:  mysql_query() [<a href='function.mysql-query'>function.mysql-query</a>]: Unable to save result set in /home/jcr14/public_html/gameServer/server.php on line 11248

#0  cm_noticeAndWarningHandler(2, mysql_query() [<a href='function.mysql-query'>function.mysql-query</a>]: Unable to save result set, /home/jcr14/public_html/gameServer/server.php, 11248, Array ([inQueryString] => SELECT player_1_id, player_2_id FROM minuetServer_games WHERE  ( player_1_id = '1513399' OR player_2_id = '1513399' )  AND last_action_time <      SUBTIME( CURRENT_TIMESTAMP, '0 0:01:22.000' ) FOR UPDATE;,[inDeadlockFatal] => 0,[cm_mysqlLink] => Resource id #4))

#1  mysql_query(SELECT player_1_id, player_2_id FROM minuetServer_games WHERE  ( player_1_id = '1513399' OR player_2_id = '1513399' )  AND last_action_time <      SUBTIME( CURRENT_TIMESTAMP, '0 0:01:22.000' ) FOR UPDATE;, Resource id #4) called at [/home/jcr14/public_html/gameServer/server.php:11248]

#2  cm_queryDatabase(SELECT player_1_id, player_2_id FROM minuetServer_games WHERE  ( player_1_id = '1513399' OR player_2_id = '1513399' )  AND last_action_time <      SUBTIME( CURRENT_TIMESTAMP, '0 0:01:22.000' ) FOR UPDATE;, 0) called at [/home/jcr14/public_html/gameServer/server.php:5979]

我会在服务器日志中查看过去几个月的其他示例。

编辑3:

在过去30天(我刷新旧日志条目),这已经发生了五次。其中四次出现在上述查询中。有一次发生在另一个查询中:

SELECT next_magic_square_seed % 4294967296 
FROM minuetServer_server_globals FOR UPDATE;

这一天也被称为100次,没有问题。这是一个包含单行的表。

周围的代码:

function cm_getNewSquare() {

    global $tableNamePrefix;

    // have it wrap around at the 32-bit unsigned max
    // because getMagicSquare6 takes a 32-bit unsigned seed.
    // we store it as a BIGINT to keep it from getting stuck on the same
    // square after four billion games

    $query = "SELECT next_magic_square_seed % 4294967296 ".
        "FROM $tableNamePrefix".
        "server_globals FOR UPDATE;";

    $result = cm_queryDatabase( $query );

编辑4:

表格结构:

mysql> describe minuetServer_games;
+-----------------------+---------------------+------+-----+---------+----------------+
| Field                 | Type                | Null | Key | Default | Extra          |
+-----------------------+---------------------+------+-----+---------+----------------+
| game_id               | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| creation_time         | datetime            | NO   |     | NULL    |                |
| last_action_time      | datetime            | NO   |     | NULL    |                |
| player_1_id           | int(10) unsigned    | NO   | MUL | NULL    |                |
| player_2_id           | int(10) unsigned    | NO   | MUL | NULL    |                |
| dollar_amount         | decimal(11,2)       | NO   | MUL | NULL    |                |
| amulet_game           | tinyint(3) unsigned | NO   | MUL | NULL    |                |
| amulet_game_wait_time | datetime            | NO   | MUL | NULL    |                |
| started               | tinyint(3) unsigned | NO   |     | NULL    |                |
| round_number          | int(10) unsigned    | NO   |     | NULL    |                |
| game_square           | char(125)           | NO   |     | NULL    |                |
| player_1_got_start    | tinyint(4)          | NO   |     | NULL    |                |
| player_2_got_start    | tinyint(4)          | NO   |     | NULL    |                |
| player_1_moves        | char(13)            | NO   |     | NULL    |                |
| player_2_moves        | char(13)            | NO   |     | NULL    |                |
| player_1_bet_made     | tinyint(3) unsigned | NO   |     | NULL    |                |
| player_2_bet_made     | tinyint(3) unsigned | NO   |     | NULL    |                |
| player_1_ended_round  | tinyint(3) unsigned | NO   |     | NULL    |                |
| player_2_ended_round  | tinyint(3) unsigned | NO   |     | NULL    |                |
| move_deadline         | datetime            | NO   |     | NULL    |                |
| player_1_coins        | tinyint(3) unsigned | NO   |     | NULL    |                |
| player_2_coins        | tinyint(3) unsigned | NO   |     | NULL    |                |
| player_1_pot_coins    | tinyint(3) unsigned | NO   |     | NULL    |                |
| player_2_pot_coins    | tinyint(3) unsigned | NO   |     | NULL    |                |
| settled_pot_coins     | tinyint(3) unsigned | NO   |     | NULL    |                |
| semaphore_key         | int(10) unsigned    | NO   |     | NULL    |                |
+-----------------------+---------------------+------+-----+---------+----------------+
26 rows in set (0.00 sec)

CREATE语句:

 "CREATE TABLE $tableName(" .
"game_id BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT," .
"creation_time DATETIME NOT NULL,".
"last_action_time DATETIME NOT NULL,".
"player_1_id INT UNSIGNED NOT NULL," .
"INDEX( player_1_id )," .
"player_2_id INT UNSIGNED NOT NULL," .
"INDEX( player_2_id )," .
"dollar_amount DECIMAL(11, 2) NOT NULL,".
"INDEX( dollar_amount )," .
"amulet_game TINYINT UNSIGNED NOT NULL,".
"INDEX( amulet_game ),".
// wait for a random amount of time before
// settling on an opponent for an amulet game
"amulet_game_wait_time DATETIME NOT NULL,".
"INDEX( amulet_game_wait_time ),".
"started TINYINT UNSIGNED NOT NULL,".
"round_number INT UNSIGNED NOT NULL," .
// 36-cell square, numbers from 1 to 36, separated by #
// character
"game_square CHAR(125) NOT NULL,".
// flag set when each player requests the very first game
// state. This indicates that both are aware that the game
// has started, so leave penalties can be assessed
"player_1_got_start TINYINT NOT NULL,".
"player_2_got_start TINYINT NOT NULL,".
"player_1_moves CHAR(13) NOT NULL,".
"player_2_moves CHAR(13) NOT NULL,".
"player_1_bet_made TINYINT UNSIGNED NOT NULL,".
"player_2_bet_made TINYINT UNSIGNED NOT NULL,".
"player_1_ended_round TINYINT UNSIGNED NOT NULL,".
"player_2_ended_round TINYINT UNSIGNED NOT NULL,".
"move_deadline DATETIME NOT NULL,".
"player_1_coins TINYINT UNSIGNED NOT NULL, ".
"player_2_coins TINYINT UNSIGNED NOT NULL, ".
"player_1_pot_coins TINYINT UNSIGNED NOT NULL, ".
"player_2_pot_coins TINYINT UNSIGNED NOT NULL, ".
// coins in both pots that are common knowledge to both players
// (coins that have been matched by opponent to move on
// to next turn)
"settled_pot_coins TINYINT UNSIGNED NOT NULL, ".
"semaphore_key INT UNSIGNED NOT NULL ) ENGINE = INNODB;"

2 个答案:

答案 0 :(得分:5)

  1.   

    虚假的,偶尔的“无法保存结果集”警告的原因是什么?

    遗憾的是,PHP手册中没有详细记录此错误。那么,让我们的RTFS!

    查看您的ext/mysql版本的源代码,错误"Unable to save result set" appears only once

        if(use_store == MYSQL_USE_RESULT) {
            mysql_result=mysql_use_result(mysql->conn);
        } else {
            mysql_result=mysql_store_result(mysql->conn);
        }
        if (!mysql_result) {
            if (PHP_MYSQL_VALID_RESULT(mysql->conn)) { /* query should have returned rows */
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save result set");
    

    查看PHP_MYSQL_VALID_RESULT宏的the definition

    #define PHP_MYSQL_VALID_RESULT(mysql)       \
        (mysql_field_count(mysql)>0)
    

    很明显,出现此错误时,mysql_store_result()必须返回NULLmysql_field_count()必须返回正数。后一个函数的MySQL C API documentation表示:

      

    此函数的正常用法是mysql_store_result()返回NULL时(因此您没有结果集指针)。在这种情况下,您可以调用mysql_field_count()来确定mysql_store_result()是否应该产生非空结果。这使客户端程序能够在不知道查询是SELECT(或类似SELECT)语句的情况下采取适当的操作。

    嗯,这似乎准确描述了你案件中出现的情况。在其中链接到Section 23.8.15.1, “Why mysql_store_result() Sometimes Returns NULL After mysql_query() Returns Success”

      

    成功调用mysql_store_result()后,mysql_query()可能会返回NULL。发生这种情况时,表示发生以下情况之一:

         
        
    • malloc()失败(例如,如果结果集太大)。

    •   
    • 无法读取数据(连接时发生错误)。

    •   
    • 查询未返回任何数据(例如,它是INSERTUPDATEDELETE)。

    •   

    由于在您执行SELECT查询时出现了这种情况,并且没有任何关于连接错误的迹象(如果那是我想将被客户端删除因此,似乎这个错误的最可能原因是malloc()失败。 This answer可以很好地解释malloc()失败的情况。

  2.   

    有没有办法解决它?

    当然有,是的!但它可能涉及详细分析MySQL为什么在这种情况下尝试分配内存失败的原因。我的猜测是你的托管环境限制了MySQL可用的内存,并且已经达到内存分配。

    或许只是增加MySQL可用的内存会有帮助吗?否则,您可能希望阅读How MySQL Uses Memory并开始调整一些服务器设置......

  3.   

    是否有可靠的解决方法?

    如果出现这种情况,您可能只是简单地重新尝试查询?

答案 1 :(得分:-1)

我已经通过释放我的服务器磁盘空间来解决此问题,因为我的服务器达到了100%的使用率。

此问题已记录在/var/log/mysql/error.log中,因此在执行任何操作之前,我始终先检查错误日志。