草庐IT

php - MySQL 非常偶然 Unable save result set

coder 2023-10-19 原文

这是一个生产 PHP/MySQL 系统,已经运行了几个月,大部分时间都没有问题。

偶尔,比如每隔几周一次,我会看到来自 MySQL 查询的“无法保存结果集”错误。

我知道这可能是由大型结果集引起的,但这里不是这种情况。在最近的示例中,它发生在通常不超过 10 行的小 table 上(行代表正在进行的游戏,并且在游戏结束后它们会被删除——很多翻转,但总是很小)。

查询很简单:

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,结果正常。

虚假的、偶尔出现的“无法保存结果集”警告的原因是什么?有办法解决吗?有可靠的解决方法吗?

编辑:

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

http://cordialminuet.com/info.php

编辑2:

这是创建查询的代码:

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
    // ....
    }

请注意,“无法保存结果集”是 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 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;"

最佳答案

  1. What are the causes of spurious, occasional "Unable to save result set" warnings?

    遗憾的是,这个错误在 PHP 手册中没有得到很好的记录。那么,让我们使用 RTFS!

    查看您的ext/mysql 版本的源代码,错误“无法保存结果集” 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");
    

    查看the definition PHP_MYSQL_VALID_RESULT 宏:

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

    很明显,要出现此错误,mysql_store_result() 必须返回 NULLmysql_field_count() 必须返回正数。 MySQL C API documentation对于后一个函数状态:

    The normal use of this function is when mysql_store_result() returned NULL (and thus you have no result set pointer). In this case, you can call mysql_field_count() to determine whether mysql_store_result() should have produced a nonempty result. This enables the client program to take proper action without knowing whether the query was a SELECT (or SELECT-like) statement.

    好吧,这似乎准确描述了您的案例中出现的情况。点击其中的链接到 Section 23.8.15.1, “Why mysql_store_result() Sometimes Returns NULL After mysql_query() Returns Success” :

    It is possible for mysql_store_result() to return NULL following a successful call to mysql_query(). When this happens, it means one of the following conditions occurred:

    • There was a malloc() failure (for example, if the result set was too large).

    • The data could not be read (an error occurred on the connection).

    • The query returned no data (for example, it was an INSERT, UPDATE, or DELETE).

    由于在您执行 SELECT 查询时出现了这种情况,并且没有连接错误的迹象(我想 如果情况确实如此),因此看来导致此错误的最可能原因是 malloc() 失败。 This answermalloc() 可能失败的情况给出了很好的解释。

  2. Is there a way to fix it?

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

    也许简单地增加 MySQL 可用的内存会有所帮助?否则,您可能想通读 How MySQL Uses Memory并开始调整一些服务器设置……

  3. Is there a solid work-around?

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

关于php - MySQL 非常偶然 Unable save result set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29188262/

有关php - MySQL 非常偶然 Unable save result set的更多相关文章

  1. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  2. ruby-on-rails - 使用 HTTParty 的非常基本的 Rails 4.1 API 调用 - 2

    Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"

  3. ruby-on-rails - 无法安装 mysql2 0.3.14 gem - 2

    我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby​​目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin

  4. ruby - 如何使用 ruby​​ mysql2 执行事务 - 2

    我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi

  5. ruby - 如何在 Ruby 中生成一个非常大的随机整数? - 2

    我想在ruby​​中生成一个64位整数。我知道在Java中你有很多渴望,但我不确定你会如何在Ruby中做到这一点。另外,64位数字中有多少个字符?这是我正在谈论的示例......123456789999。@num=Random.rand(9000)+Random.rand(9000)+Random.rand(9000)但我认为这是非常低效的,必须有一种更简单、更简洁的方法来做到这一点。谢谢! 最佳答案 rand可以将范围作为参数:pa=rand(2**32..2**64-1)#=>11093913376345012184putsa.

  6. Ruby 服务器在本地主机(teambox)之外非常慢 - 2

    我刚刚在我的Ubuntu9.10服务器上安装了TeamBox。我使用提供的服务器脚本在端口3000上启动并运行它。它的运行速度非常慢,从另一台计算机连接时每个HTTP请求最多需要30秒。我使用链接从shell加载TeamBox,一点也不花时间。然后我设置了一个SSH隧道,它再次运行得非常快。我通过此服务器上的apache以及SAMBA等运行了大约30个虚拟主机,没有任何问题。我该如何解决这个问题? 最佳答案 我的redmine(ruby,webrick)太慢了。现在我解决了这个问题:apt-getinstallmongrelruby

  7. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  8. ruby-on-rails - 当我通过 rvm 使用 rails3 时,如何在 ubuntu 上安装 mysql2 gem? - 2

    我正在尝试绕过rails配置这个极其复杂的迷宫。到目前为止,我设法在ubuntu上设置了rvm(出于某种原因,ruby在ubuntu存储库中已经过时了)。我设法建立了一个Rails项目。我希望我的测试项目使用mysql而不是mysqlite。当我尝试“rakedb:migrate”时,出现错误:“!!!缺少mysql2gem。将其添加到您的Gemfile:gem'mysql2'”当我尝试“geminstallmysql”时,出现错误,告诉我需要为安装命令提供参数。但是,参数列表很大,我不知道该选择哪些。如何通过在ubuntu上运行的rvm和mysql获取rails3?谢谢。

  9. ruby-on-rails - 即使没有挂起的迁移,Rails 迁移也非常缓慢 - 2

    我的生产Rails应用程序需要167秒来运行rakedb:migrate。可悲的是,没有要运行的迁移。我试图在检查是否有待处理的迁移时调整运行的迁移,但随后检查花费了同样长的时间。我心目中唯一的“借口”是数据库并不小,那里有1M条记录,但我看不出这有什么关系。我查看了日志,但没有任何迹象表明出了什么问题。我在运行ruby2.2.0rails4.2.0有没有人知道为什么会这样,是否有什么办法可以解决? 最佳答案 运行rakedb:migrate任务还会调用db:schema:dump任务,这将更新您的db/schema.rb。因此,即

  10. ruby - 由于 GEM_HOME 的要求,启动 Ruby 应用程序非常慢 - 2

    我目前正在开发一个ruby​​应用程序,但它运行得非常(非常!)慢..到目前为止,我已经尝试了几件事,我可以将其缩小到主要问题:Ruby正在尝试在$LOAD_PATH的每个目录中查找它的需求。基本上我所观察到的是,ruby正在查看大量文件,试图查看那里是否存在需求。如果找不到它们,它将转到下一个目录。好消息是我可以通过strace看到这种情况。有很多这样的输出:open("/boa_proj_build/nsteen/.gem/gems/i18n-0.7.0/lib/commander/help_formatters/base.rb",O_RDONLY|O_CLOEXEC)=-1ENO

随机推荐