简体   繁体   English

查询超过3个表时,左连接性能太慢

[英]Left Join performance is too slow when query more then 3 tables

I have 7 tables which I need to query, every table have district_id (district has it's own id as id and it's auto increment field). 我需要查询7个表,每个表都有district_id (区域有自己的id作为id,它是自动增量字段)。

Table names are : 表名是:

districts, blocks, panchayats, habitations, villages, divisions, sub_divisions

Here's the query syntax i use for test. 这是我用于测试的查询语法。

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

It works fine till 3 Left Join but when it comes to the fourth it run the query but never stop. 它工作正常,直到3左连接,但到第四个它运行查询但永远不会停止。

Objective : I try to parse all the districts and fetch count data of all the matching district_id from blocks, panchayats, habitations, villages, divisions and sub_divisions. 目标 :我尝试解析所有区域并从块,panchayats,居住地,村庄,分区和子分区获取所有匹配的district_id计数数据。

Expect result : 期待结果

districts | blocks | panchayats | habitations | villages | division | sub_divisions
-----------------------------------------------------------------------------------
ABC       | 6      | 20         | 10          | 10       | 3         | 2
-----------------------------------------------------------------------------------
DEF       | 3      | 12         | 4           | 7        | 7         | 4

I am not very good with SQL, thank you in advance for your time and help. 我对SQL不太满意,请提前感谢您的时间和帮助。

Table Structures: 表结构:

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
------------------------------------------------------------------------------

All the ID are Primary Keys, on phpMyAdmin I make district_id in every table index. 所有的ID是主键,在phpMyAdmin我做district_id每个表中的索引。

The query as you have it, will potentially create a huge result set, and then apply 4 different distinct counts on that result set. 您拥有它的查询可能会创建一个巨大的结果集,然后对该结果集应用4个不同的不同计数。 In this case, you might be better off using subqueries. 在这种情况下,您可能最好使用子查询。 Admittedly, also not a great solution... 不可否认,也不是一个很好的解决方案......

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

Did you try: 你试过了吗:

GROUP BY d.id, d.district_name

in your original query? 在您的原始查询?

I think MySQL permits to write GROUP BY in a way you did but other databases wouldn'let you write it this way. 我认为MySQL允许以你所做的方式编写GROUP BY ,但是其他数据库不会以这种方式编写它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM