sql query joins multiple tables - too slow (8 tables)

前端 未结 8 2048
借酒劲吻你
借酒劲吻你 2020-11-30 23:22

i\'m trying to join 8 tables into one in order to create index used by other application, my query is like : (my mysql skill\'s very amateur)

SELECT t1_id, t         


        
8条回答
  •  隐瞒了意图╮
    2020-11-30 23:28

    I had a similar problem with several lookup tables joining to a large table with all id fields indexed. To monitor the effect of the joins on query time execution, I ran my query several times (limiting to first 100 rows), adding a Join to an additional table each time. After joining 12 tables, there was no significant change in query execution time. By the time I had joined the 13th table the execution time jumped to a 1 second; 14th table 4 seconds, 15th table 20 s, 16th 90 seconds.

    Keijro's suggestion to use a correlated subqueries instead of joins e.g.

    SELECT t1_id, 
            (select t2_name from t2 where t1_id = t2_id), 
            (select t3_name from t3 where t1_id = t3_id), 
            (select t4_name from t4 where t1_id = t4_id), 
            (select t5_name from t5 where t1_id = t5_id), 
            (select t6_name from t6 where t1_id = t6_id), 
            (select t7_name from t7 where t1_id = t7_id), 
            (select t8_name from t8 where t1_id = t8_id), 
            (select t9_name from t9 where t1_id = t9_id)  FROM t1
    

    improved query performance dramatically. In fact the subqueries did not seem to lengthen the time to execute the query (the query was almost instanteous).

    I am a little suprised as I thought correlated subqueries perform worse than joins.

提交回复
热议问题