gpt4 book ai didi

php - mysql子查询还是php处理?

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

我有一个类似这样的查询:

Select 
tradesmen.id,
(SELECT COUNT(quotes.id) FROM quotes WHERE quotes.tradesman_id = tradesmen.id) AS quoted
From
tradesmen;

所以基本上数据库中的每一行(50,000+)都有一个子查询。现在每个商人可能有大约 1,000 - 2,000 个报价。

所以我可以使用这个子查询来计算它们。

或者,

我可以查询所有的商人。

select tradesman.id from tradesmen;

执行一次查询以获取所有引用计数

select tradesman_id as id, count(quotes.id) as quotes from quotes group by tradesman_id;

然后遍历每个商人并从数组中提取每个商人的计数。

mysql有多快?第二种方法会带来显着的好处还是任何一种方法都可以接受?

作为一般引用,我的实际查询是:

SELECT 
tradesmen.*,
regions.name AS region_name,
GROUP_CONCAT(ptypes_tradesmen.ptype_id SEPARATOR '|') AS ptype_ids,
(SELECT
COUNT(quotes.id)
FROM
quotes
WHERE
quotes.tradesman_id = tradesmen.id
) AS quoted,
(SELECT
COUNT(quote_intentions.id)
FROM
quote_intentions
WHERE
quote_intentions.tradesman_id = tradesmen.id
) AS intended,
(SELECT
COUNT(quotes.id) FROM quotes
WHERE
quotes.tradesman_id = tradesmen.id
AND quotes.accepted = 1
) AS awarded
FROM
(`tradesmen`)
LEFT JOIN `regions` ON `regions`.`id` = `tradesmen`.`region_id`
LEFT JOIN `ptypes_tradesmen` ON `ptypes_tradesmen`.`tradesman_id` = `tradesmen`.`id`
GROUP BY `tradesmen`.`id`

更新

使用 ctrahey 的回答,我更改了查询。

所以,我们现在有三个版本的查询..

ctraheys:

SELECT 
tradesmen.*,
regions.name AS region_name,
GROUP_CONCAT(ptypes_tradesmen.ptype_id SEPARATOR '|') AS ptype_ids,
COUNT(quotes.id) AS quoted,
COUNT(quote_intentions.id) AS intended,
COUNT(NULLIF(quotes.accepted, 0)) AS awarded
FROM (`tradesmen`)
LEFT JOIN `regions` ON `regions`.`id` = `tradesmen`.`region_id`
LEFT JOIN `ptypes_tradesmen` ON `ptypes_tradesmen`.`tradesman_id` = `tradesmen`.`id`
LEFT JOIN quotes ON quotes.tradesman_id = tradesmen.id
LEFT JOIN quote_intentions ON quote_intentions.tradesman_id = tradesmen.id
GROUP BY `tradesmen`.`id`

我的修改版本:

SELECT 
t.*,
r.name AS region_name,
GROUP_CONCAT(p.ptype_id SEPARATOR "|") AS ptype_ids,
COUNT(q.id) as quoted,
COUNT(i.id) as intended,
COUNT(NULLIF(q.accepted, 0)) as awarded
FROM (tradesmen t)
LEFT JOIN regions r ON r.id = t.region_id
LEFT JOIN quotes q ON t.id = q.tradesman_id
LEFT JOIN quote_intentions i ON t.id = i.tradesman_id
LEFT JOIN ptypes_tradesmen p ON p.tradesman_id = t.id
GROUP BY t.id

原文:

SELECT 
tradesmen.*,
regions.name AS region_name,
GROUP_CONCAT(ptypes_tradesmen.ptype_id SEPARATOR '|') AS ptype_ids,
(SELECT
COUNT(quotes.id)
FROM
quotes
WHERE
quotes.tradesman_id = tradesmen.id
) AS quoted,
(SELECT
COUNT(quote_intentions.id)
FROM
quote_intentions
WHERE
quote_intentions.tradesman_id = tradesmen.id
) AS intended,
(SELECT
COUNT(quotes.id) FROM quotes
WHERE
quotes.tradesman_id = tradesmen.id
AND quotes.accepted = 1
) AS awarded
FROM
(`tradesmen`)
LEFT JOIN `regions` ON `regions`.`id` = `tradesmen`.`region_id`
LEFT JOIN `ptypes_tradesmen` ON `ptypes_tradesmen`.`tradesman_id` = `tradesmen`.`id`
GROUP BY `tradesmen`.`id`

虽然它们都返回几乎相同的结果,但在最后四个字段中存在差异(因此我在这里从结果数组中删除了所有其他行)。

来自原始查询(正确结果):

array('id' => '53',
'ptype_ids' => '58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68',
'quoted' => '6',
'intended' => '14',
'awarded' => '3'),

来自 ctrahey 的查询:

array('id' => '53',
'ptype_ids' => '58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|40|40|40|40|40|4',
'quoted' => '2016',
'intended' => '2016',
'awarded' => '1008'),

来 self 修改后的查询:

array('id' => '53',
'ptype_ids' => '58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46',
'quoted' => '2016',
'intended' => '2016',
'awarded' => '1008'),

最佳答案

都没有!进行适当的 JOIN(RDBMS 中的真正值(value)所在):

SELECT 
tradesmen.id,
COUNT(quotes.id) as quoted
FROM
tradesmen
LEFT JOIN quotes ON quotes.tradesman_id = tradesmen.id
GROUP BY tradesmen.id

此查询将正是您所需要的,而且速度很快!

编辑您的真实查询

在您的实际查询中唯一需要注意的是接受引号计数中的 NULLIF 位,因为 (IIRC) COUNT() 计数为 false/0,但不是 NULL。

SELECT 
tradesmen.*,
regions.name AS region_name,
GROUP_CONCAT(ptypes_tradesmen.ptype_id SEPARATOR '|') AS ptype_ids,
COUNT(quotes.id) AS quoted
COUNT(quote_intentions.id) AS intended
COUNT(NULLIF(quotes.accepted, 0)) AS awarded
FROM (`tradesmen`)
LEFT JOIN `regions` ON `regions`.`id` = `tradesmen`.`region_id`
LEFT JOIN `ptypes_tradesmen` ON `ptypes_tradesmen`.`tradesman_id` = `tradesmen`.`id`
LEFT JOIN quotes ON quotes.tradesman_id = tradesmen.id
LEFT JOIN quote_intentions ON quote_intentions.tradesman_id = tradesmen.id
GROUP BY `tradesmen`.`id`

关于php - mysql子查询还是php处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136390/

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