异步运行MySQLi准备语句

时间:2016-02-06 06:01:23

标签: php mysql asynchronous mysqli

我有以下查询,用于为我的用户制作图表。唯一的问题是有时这会使服务器过载,我可能会挂起查询。

我注意到山羊的答案允许使用基本查询方法的狡猾的mysqli连接:

How to set a maximum execution time for a mysql query?

但是,出于安全原因,我希望能够使用预准备语句。异步查询的文档非常有限。

如何使用预处理语句集成异步mysqli,这些语句将在执行时间限制内返回尽可能多的行?

$query = "
SELECT 

t1.day AS day,
COALESCE(t1.amount,0) AS earnings, 
COALESCE(t2.amount,0) AS publisher_referral_earnings, 
COALESCE(t3.amount,0) AS advertiser_referral_earnings 

FROM

(
SELECT DATE_FORMAT(earning_created, '%c/%e/%Y') AS day, SUM(earning_amount) AS amount
FROM earnings
WHERE earning_referral_id = 0 
AND (earning_created > DATE_SUB(now(), INTERVAL 30 DAY))
AND earning_account_id = ?
GROUP BY DATE(earning_created)
) t1

LEFT JOIN

(
SELECT DATE_FORMAT(ep.earning_created, '%c/%e/%Y') AS day, (SUM(ep.earning_amount) * rp.referral_share) AS amount
FROM earnings AS ep
INNER JOIN referrals AS rp
ON ep.earning_referral_id = rp.referral_id
WHERE ep.earning_referral_id > 0 
AND (ep.earning_created > DATE_SUB(now(), INTERVAL 30 DAY))
AND ep.earning_account_id = ?
AND rp.referral_type = 0
GROUP BY DATE(ep.earning_created)
) t2

ON t1.day = t2.day

LEFT JOIN

(
SELECT DATE_FORMAT(ea.earning_created, '%c/%e/%Y') AS day, (SUM(ea.earning_amount) * ra.referral_share) AS amount
FROM earnings AS ea
INNER JOIN referrals AS ra
ON ea.earning_referral_id = ra.referral_id
WHERE ea.earning_referral_id > 0 
AND (ea.earning_created > DATE_SUB(now(), INTERVAL 30 DAY))
AND ea.earning_account_id = ?
AND ra.referral_type = 1
GROUP BY DATE(ea.earning_created)
) t3

ON t1.day = t3.day
";

这是mysqli:

if ($statement = $mysqli->prepare($query))
{
$statement->bind_param("iii", $account_id, $account_id, $account_id);
$statement->execute();
$statement->store_result();
$timescales_total = $statement->num_rows;
$statement->bind_result($timescale, $earnings, $publisher, $advertiser);
$statement->free_result();
$statement->close();
}

0 个答案:

没有答案