我将相同的2个查询应用于3个表,因此只需循环并替换表名。第一个INSERT查询始终适用于测试服务器和生产。第二个DELETE查询适用于两个不同的测试服务器,但是在生产失败并抛出mysql错误1046没有选择数据库。
受影响的代码提取:
$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中遇到了同样的错误,并且仅在生产时出现错误。此查询在复制从属服务器上运行,并且不涉及主服务器,但是之前的代码会对表进行TRUNCATES,因此我不认为这取决于权限。当从日志文件中取出查询时,它在phpMyAdmin中正常工作。它在测试服务器上很好用。
我没有得到1046 No database selected错误的原因。 3个小时后,我可能会遗漏一些非常明显的东西。特别是因为故障模式是这样的,对于一个DB连接:
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 *函数。这是一个过时的遗留应用程序。我知道......
答案 0 :(得分: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"
答案 1 :(得分:0)
DELETE查询的语法可能导致问题
DELETE T FROM
我知道由于mysql错误,这有时会导致UPDATE查询出现问题。
重写您的查询以避免T
引用应解决此问题。