0

我将相同的 2 个查询应用于 3 个表,因此只需循环并替换表名。第一个 INSERT 查询始终适用于测试服务器和生产环境。第二个 DELETE 查询在两个不同的测试服务器上工作,但在生产中失败并抛出 mysql 错误 1046 No database selected。

受影响的代码提取:

    $queries = array(
        "INSERT INTO knd_bkg.@table@
            SELECT DISTINCT SQL_NO_CACHE
            B.ASIN, Author, Title, EditorialReview, DetailPageURL, current_price, salesrank, number_reviews, erotica, G.BrowseNodeId, price_salesrank_checked_on
            FROM bkd_books.club_books B
            USE INDEX (free_quality)
            RIGHT JOIN bkd_books.book_genre G on G.ASIN=B.ASIN
            WHERE EXISTS
            (
              SELECT 1 FROM knd_bkg.genres K WHERE K.browsenode=G.BrowseNodeId
            )
            AND @priceWhere@
            AND price_salesrank_checked_on > '@newerThan@'
            AND B.number_reviews >= 4
            AND B.rating >= 4.0
            AND B.language = 'english'
            AND B.hide_knd=0
            AND B.public_domain=0;",
        "DELETE T FROM knd_bkg.@table@ T
         INNER JOIN
         (
             SELECT N.* FROM knd_bkg.@table@ N
             INNER JOIN
             (
                 SELECT DISTINCT ASIN, BrowseNodeId FROM knd_bkg.@table@
                 INNER JOIN knd_bkg.genres ON knd_bkg.genres.browsenode=knd_bkg.@table@.BrowseNodeId
                 WHERE isFiction=1
             ) F
             ON F.ASIN=N.ASIN
             INNER JOIN knd_bkg.genres
             ON knd_bkg.genres.browsenode=N.BrowseNodeId
             WHERE knd_bkg.genres.isNonFiction=1
         ) D
         USING (ASIN, BrowseNodeId)
         WHERE D.ASIN=T.ASIN AND D.BrowseNodeId=T.BrowseNodeId"
    );

    $tables = array(
        "free_books" => array('priceWhere' => "B.current_price = 0", 'timePeriod' => $freeTimePeriod),
        "99_books" => array('priceWhere' => "B.current_price > 0 AND B.current_price < 100", 'timePeriod' => $paidTimePeriod),
        "399_books" => array('priceWhere' => "B.current_price >= 100 AND B.current_price < 400", 'timePeriod' => $paidTimePeriod),
    );

    connectSlaveDB();

    foreach ($tables as $table => $data) {
        $newerThan = date("Y-m-d H:i:s", strtotime("-" . $data['timePeriod'] . " hours"));
        foreach ($queries as $query) {
            $query = str_replace('@table@', $table, $query);
            $query = str_replace('@newerThan@', $newerThan, $query);
            $query = str_replace('@priceWhere@', $data['priceWhere'], $query);
            Logger::_write(LOG_VERBOSE, "API - api/bkg/rebuild query: $query");
            $result = mysql_query($query);
            if (!$result) {
                Logger::_write(LOG_ERR, "API - api/bkg/rebuild FAILED: " . mysql_error());
                $success = false;
            } else {
                Logger::_write(LOG_VERBOSE, "API - api/bkg/rebuild SUCCESS");
            }
        }
    }

和 connectSlaveDB()

function connectSlaveDB()
{
    global $dbSlave, $dbUser, $dbPass, $dbName;
    $connSlave = mysql_connect($dbSlave, $dbUser, $dbPass, TRUE);
    if (!$connSlave) {
        Logger::_write(LOG_CRIT, "Unable to connect to slave DB");
        include 'screens/error.inc.php';
        exit();
   }
    mysql_set_charset('utf8', $connSlave);
    mysql_select_db($dbName, $connSlave);
    return TRUE;
}

这些表是 MEMORY 表,但我在使用 MYISAM 时遇到了同样的错误,而且只在生产中出现。此查询在复制从属服务器上运行,不涉及主服务器,但之前的代码截断了表,所以我不认为它是权限问题。当查询从日志文件中取出时,它在 phpMyAdmin 中工作正常。在测试服务器上也很好。

我不明白 1046 No database selected 错误的原因。3 小时后,我可能会遗漏一些非常明显的东西。尤其是失败模式是这样的,对于一个数据库连接:

INSERT on free_books - success
DELETE on free_books - fail
INSERT on 99_books - success
DELETE on 99_books - fail
INSERT on 399_books - success
DELETE on 399_books - fail

请原谅丑陋的全局变量和不推荐使用的 mysql* 函数。这是一个相当过时的遗留应用程序。我知道...

4

2 回答 2

0

看起来 DELETE 中的 T 别名引起了问题,但我不确定为什么。现在通过使 DELETE 查询为:

"DELETE knd_bkg.@table@ FROM knd_bkg.@table@
     INNER JOIN
     (
         SELECT N.* FROM knd_bkg.@table@ N
         INNER JOIN
         (
             SELECT DISTINCT ASIN, BrowseNodeId FROM knd_bkg.@table@
             INNER JOIN knd_bkg.genres ON knd_bkg.genres.browsenode=knd_bkg.@table@.BrowseNodeId
             WHERE isFiction=1
         ) F
         ON F.ASIN=N.ASIN
         INNER JOIN knd_bkg.genres
         ON knd_bkg.genres.browsenode=N.BrowseNodeId
         WHERE knd_bkg.genres.isNonFiction=1
     ) D
     USING (ASIN, BrowseNodeId)
     WHERE D.ASIN=knd_bkg.@table@.ASIN AND D.BrowseNodeId=knd_bkg.@table@.BrowseNodeId"
于 2013-07-18T09:43:45.647 回答
0

您的 DELETE 查询的语法可能会导致问题

DELETE T FROM

我知道由于 mysql 错误,这有时会导致 UPDATE 查询出现问题。

重新编写查询以避免T引用应该可以解决此问题。

于 2013-07-18T10:06:08.257 回答