gpt4 book ai didi

MySQL子查询效率

转载 作者:行者123 更新时间:2023-11-29 05:29:44 25 4
gpt4 key购买 nike

我有这个查询,它将大量值添加到“统计”表中。当查询运行时,它会选择值来填充子查询中的表。我想知道这是否可以更有效地进行,或者我是否做错了什么。我对 MySQL 不太熟悉,所以任何帮助都会很棒:)

这里是查询:

UPDATE mediastats SET 
mediastats_members = (SELECT count(*) FROM status WHERE status_media_id = :id),
mediastats_avscore = (SELECT AVG(status_rating) FROM status WHERE status_media_id = :id),
mediastats_done = (SELECT count(*) FROM status WHERE status_status = 'done' AND status_media_id = :id),
mediastats_doing = (SELECT count(*) FROM status WHERE status_status = 'doing' AND status_media_id = :id),
mediastats_redoing = (SELECT count(*) FROM status WHERE status_status = 'redoing' AND status_media_id = :id),
mediastats_dropped = (SELECT count(*) FROM status WHERE status_status = 'dropped' AND status_media_id = :id),
mediastats_wantto = (SELECT count(*) FROM status WHERE status_status = 'wantto' AND status_media_id = :id),
mediastats_wont = (SELECT count(*) FROM status WHERE status_status = 'wont' AND status_media_id = :id),
mediastats_stalled = (SELECT count(*) FROM status WHERE status_status = 'stalled' AND status_media_id = :id),
mediastats_rating_1 = (SELECT count(*) FROM status WHERE status_rating = 1 AND status_media_id = :id),
mediastats_rating_2 = (SELECT count(*) FROM status WHERE status_rating = 2 AND status_media_id = :id),
mediastats_rating_3 = (SELECT count(*) FROM status WHERE status_rating = 3 AND status_media_id = :id),
mediastats_rating_4 = (SELECT count(*) FROM status WHERE status_rating = 4 AND status_media_id = :id),
mediastats_rating_5 = (SELECT count(*) FROM status WHERE status_rating = 5 AND status_media_id = :id),
mediastats_rating_6 = (SELECT count(*) FROM status WHERE status_rating = 6 AND status_media_id = :id),
mediastats_rating_7 = (SELECT count(*) FROM status WHERE status_rating = 7 AND status_media_id = :id),
mediastats_rating_8 = (SELECT count(*) FROM status WHERE status_rating = 8 AND status_media_id = :id),
mediastats_rating_9 = (SELECT count(*) FROM status WHERE status_rating = 9 AND status_media_id = :id),
mediastats_rating_10 = (SELECT count(*) FROM status WHERE status_rating = 10 AND status_media_id = :id)
WHERE mediastats_media_id = :id

:id 是从 PHP 添加的。

最佳答案

下面是我在 PHP 中使用 PDO 的方式:

$sql = "
SELECT
COUNT(*) AS mediastats_members,
AVG(status_rating) AS mediastats_avscore,
SUM(status_status = 'done') AS mediastats_done,
SUM(status_status = 'doing') AS mediastats_doing,
SUM(status_status = 'redoing') AS mediastats_redoing,
SUM(status_status = 'dropped') AS mediastats_dropped,
SUM(status_status = 'wantto') AS mediastats_wantto,
SUM(status_status = 'wont') AS mediastats_wont,
SUM(status_status = 'stalled') AS mediastats_stalled,
SUM(status_rating = 1) AS mediastats_rating_1,
SUM(status_rating = 2) AS mediastats_rating_2,
SUM(status_rating = 3) AS mediastats_rating_3,
SUM(status_rating = 4) AS mediastats_rating_4,
SUM(status_rating = 5) AS mediastats_rating_5,
SUM(status_rating = 6) AS mediastats_rating_6,
SUM(status_rating = 7) AS mediastats_rating_7,
SUM(status_rating = 8) AS mediastats_rating_8,
SUM(status_rating = 9) AS mediastats_rating_9,
SUM(status_rating = 10) AS mediastats_rating_10
FROM status
WHERE status_media_id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(array("id"=>$id));
$params = $stmt->fetch(PDO::FETCH_ASSOC);

通过这种方式,您可以一次计算表的所有聚合,而不是对每个计数使用单独的子查询。

我正在使用 MySQL 的技巧—— bool 表达式的 SUM() 等于表达式为真时的 COUNT()。这是因为 MySQL bool 表达式总是返回 0 或 1,并且 0 和 1 的 SUM 等于 1 的 COUNT。

然后您可以将上述查询的结果用作 UPDATE 语句的参数数组:

$sql = "
UPDATE mediastats SET
mediastats_members = :mediastats_members,
mediastats_avscore = :mediastats_avscore,
mediastats_done = :mediastats_done,
mediastats_doing = :mediastats_doing,
mediastats_redoing = :mediastats_redoing,
mediastats_dropped = :mediastats_dropped,
mediastats_wantto = :mediastats_wantto,
mediastats_wont = :mediastats_wont,
mediastats_stalled = :mediastats_stalled,
mediastats_rating_1 = :mediastats_rating_1,
mediastats_rating_2 = :mediastats_rating_2,
mediastats_rating_3 = :mediastats_rating_3,
mediastats_rating_4 = :mediastats_rating_4,
mediastats_rating_5 = :mediastats_rating_5,
mediastats_rating_6 = :mediastats_rating_6,
mediastats_rating_7 = :mediastats_rating_7,
mediastats_rating_8 = :mediastats_rating_8,
mediastats_rating_9 = :mediastats_rating_9,
mediastats_rating_10 = :mediastats_rating_10
WHERE mediastats_media_id = :id";

$stmt = $pdo->prepare($sql);
$params["id"] = $id;
$stmt->execute($params);

从 PHP 5.3.4 开始,PDO 接受没有前导 : 的参数数组键。在查询中声明参数占位符时需要冒号,但在提供给 execute() 的值数组中不需要冒号。

关于MySQL子查询效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16042625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com