Pergunta

I have a query that looks something like:

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

So basically there is a subquery for every row in the database (50,000+). Now for each trademan there could be around 1,000 - 2,000 quotes.

So I could either use this sub query to count them.

Or,

I could do the query to get all the tradesmen.

select tradesman.id from tradesmen;

Do one query to get all the quotes counts

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

Then loop over each tradesman and pull out of the array the count for each tradesman.

How fast is mysql? would the second method provide significant benefit or is either method going to be acceptable?

For general reference my actual query is:

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`

Update

Using the answer from ctrahey I have changed the query.

So, we have three versions of the query now..

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`

My modified version:

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

The original:

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`

and whilst they are all returning almost the same results there are differences in the last four fields (hence I am removing all other rows from the results array here).

From original query (correct results):

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'),

From ctrahey's query:

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'),

From my modified query:

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'),
Foi útil?

Solução

Neither! Do a proper JOIN (where the real value in an RDBMS is):

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

This query will be exactly what you need and lightening fast!

edit your real query

The only caveat needed in your real query is the NULLIF bit inside the count of accepted quotes, as (IIRC) COUNT() will count false/0, but not 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`
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top