我有 7 个表需要查询,每个表都有 district_id
(district 有它自己的 id 作为 id 并且它是自动递增字段)。
表名是:
districts, blocks, panchayats, habitations, villages, divisions, sub_divisions
这是我用于测试的查询语法。
SELECT
d.id, d.district_name,
COUNT(DISTINCT b.id) AS total_block,
COUNT(DISTINCT g.id) AS gp_total,
COUNT(DISTINCT v.id) AS vi_total,
COUNT(DISTINCT h.id) AS h_total
FROM districts d
LEFT JOIN blocks b ON b.district_id = d.id
LEFT JOIN panchayats g ON g.district_id = d.id
LEFT JOIN villages v ON v.district_id = d.id
LEFT JOIN habitations h ON h.district_id = d.id
GROUP BY district_name
它在 3 Left Join 之前工作正常,但是当涉及到第四个时,它运行查询但从不停止。
目标:我尝试解析所有地区并从街区、panchayats、居住地、村庄、分区和子分区中获取所有匹配 district_id
的计数数据。
预期结果:
districts | blocks | panchayats | habitations | villages | division | sub_divisions
-----------------------------------------------------------------------------------
ABC | 6 | 20 | 10 | 10 | 3 | 2
-----------------------------------------------------------------------------------
DEF | 3 | 12 | 4 | 7 | 7 | 4
我不太擅长 SQL,提前感谢您的时间和帮助。
表结构:
Districts
------------------------------------------------------------------------------
id | district_name | district_code | last_update(TIMESTAMP) | created_on(DATE)
------------------------------------------------------------------------------
Blocks
------------------------------------------------------------------------------
id | block_name | block_code | district_id | last_update | created_on
------------------------------------------------------------------------------
Panchayats
------------------------------------------------------------------------------
id | panchayat_name | panchayat_code | district_id | block_id | last_update | created_on
------------------------------------------------------------------------------
Habitations
------------------------------------------------------------------------------
id | name | code | district_id | block_id | panchayat_id | village_id
------------------------------------------------------------------------------
Villages
------------------------------------------------------------------------------
id | village_name | village_code | district_id | block_id | panchayat_id
------------------------------------------------------------------------------
Division
------------------------------------------------------------------------------
id | name | district_id .....
------------------------------------------------------------------------------
Sub Division
------------------------------------------------------------------------------
id | name | district_id
------------------------------------------------------------------------------
所有的 ID 都是主键,在 phpMyAdmin
上,我在每个表索引中都设置了 district_id
。
最佳答案
您所拥有的查询可能会创建一个巨大的结果集,然后对该结果集应用 4 个不同的不同计数。在这种情况下,您最好使用子查询。不可否认,这也不是一个很好的解决方案......
SELECT
d.id,
d.district_name,
(SELECT COUNT(b.id) FROM blocks b WHERE b.district_id = d.id) total_block,
(SELECT COUNT(p.id) FROM panchayats p WHERE p.district_id = d.id) gp_total,
(SELECT COUNT(v.id) FROM villages v WHERE v.district_id = d.id) vi_total,
(SELECT COUNT(h.id) FROM habitations h WHERE h.district_id = d.id) h_total
FROM districts d
关于mysql - 当查询超过 3 个表时,Left Join 性能太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361509/