SQL INNER连接执行时间太长

时间:2016-10-08 10:54:00

标签: mysql sql inner-join

我有下面的sql查询,执行时间太长,请求内部联接的替代方法来解决我的问题吗?因为当我删除3或4个连接时,查询执行得更快。

SELECT  p.first_name, p.last_name, p.x, p.dob,'name', w.id_table1, w.type, w.secondary_type, w.primary_location, w.onset, w.created_at,  av.item as table1_edge , av1.item as sdf ,av2.item as sd, av3.item as odor, av4.item as xz, av5.value as xxx , av9.item as cc, av10.item as cxz                        
                    FROM table1 w 
                    INNER JOIN table2 a  ON w.id_table1 = a.table1_id_table1
                    INNER JOIN table3 av ON a.id_table2 = av.id_table3 and av.sub_group = 'x1'
                    INNER JOIN table3 av1 ON a.id_table2 = av1.id_table3 and av1.sub_group = 'x2'
                    INNER JOIN table3 av2 ON a.id_table2 = av2.id_table3 and av2.sub_group = 'x3'
                    INNER JOIN table3 av3 ON a.id_table2 = av3.id_table3 and av3.sub_group = 'x4'
                    INNER JOIN table3 av4 ON a.id_table2 = av4.id_table3 and av4.sub_group = 'x5'
                    INNER JOIN table3 av5 ON a.id_table2 = av5.id_table3 and av5.sub_group = 'x6' and av6.item like 's%'
                    INNER JOIN table3 av6 ON a.id_table2 = av6.id_table3 and av6.sub_group = 'x7' and av6.item like 'x%'
                    INNER JOIN table3 av7 ON a.id_table2 = av7.id_table3 and av7.sub_group = 'x8' and av6.item like 'z%'
                    INNER JOIN table3 av8 ON a.id_table2 = av8.id_table3 and av8.sub_group = 'x9' and av6.item like 'y%' 
                    INNER JOIN table3 av9 ON a.id_table2 = av9.id_table3 and av9.sub_group = 'x10'
                    INNER JOIN table3 av10 ON a.id_table2 = av10.id_table3 and av10.sub_group = 'x11'
                    INNER JOIN table0 p ON w.table0_id_table0 = p.id_table0
                    where w.created_at between '1991-12-09 00:00:00' and now() and user_id_user = 4

数据:
table0
FIRST_NAME
姓氏
DOB
...
table1
id_table1
类型
secondary_type
...

3 个答案:

答案 0 :(得分:1)

也许你可以让你更简单地查询:

SELECT  p.first_name, p.last_name, p.x, p.dob,'name', 
    w.id_table1, w.type, w.secondary_type, w.primary_location,
    w.onset, w.created_at, av.item,
    IF(av.sub_group='x1', 1, 0) as val_x1, 
    IF(av.sub_group='x2', 1, 0) as val_x2, 
    IF(av.sub_group='x3', 1, 0) as val_x3,
               ..........
    IF(av.sub_group='x11', 1, 0) as val_x11                         
FROM table1 w 
INNER JOIN table2 a ON w.id_table1 = a.table1_id_table1
left join table3 av on a.id_table2 = av.id_table3
INNER JOIN table0 p ON w.table0_id_table0 = p.id_table0
where w.created_at between '1991-12-09 00:00:00' 
    and now() and user_id_user = 4
    and  ( (av.sub_group in ('x1', 'x2', 'x3', 'x4', 'x5', 'x10', 'x11'))
       or (av.sub_group='x6' and av.item like 's%') 
       or (av.sub_group='x7' and av.item like 'x%') 
       or (av.sub_group='x8' and av.item like 'z%') 
       or (av.sub_group='x9' and av.item like 'y%')
      )

我们只使用表JOIN制作一个table3,并将所有条件移至where子句中。使用val_x1 .... val_x11,我们可以知道av.item中的哪一个值。

答案 1 :(得分:1)

您是否尝试使用子查询代替需要更长时间执行的内部联接?

示例:

select a.id, b.id, c.id
from table a
inner join (
   select id
   from tableb
   where b = 'x1'
) as b on b.id = a.id
inner join (
   select id
   from tablec
   where c = 'x2'
) as c on c.id = a.id

答案 2 :(得分:0)

您似乎以EAV格式存储内容(entity-attribute-value)。如果你不明白这意味着什么,你可以查看维基百科上的定义。

一种解决方案是使用聚合:

SELECT p.first_name, p.last_name, p.x, p.dob, 'name',
       w.id_table1, w.type, w.secondary_type, w.primary_location, 
       w.onset, w.created_at, 
       MAX(CASE WHEN av.sub_group = 'x1' THEN av.item END) as table1_edge,
       MAX(CASE WHEN av.sub_group = 'x1' THEN av.item END) as sdf,
       . . .                      
FROM table1 w INNER JOIN
     table2 a 
     ON w.id_table1 = a.table1_id_table1 INNER JOIN
     table3 av
     ON a.id_table2 = av.id_table3 
WHERE av.sub_group IN ('x1', 'x2', . . . ) AND
      w.created_at between '1991-12-09' and now() AND
      user_id_user = 4
GROUP BY p.first_name, p.last_name, p.x, p.dob, 
         w.id_table1, w.type, w.secondary_type, w.primary_location, w.onset, w.created_at;

您还需要适当的索引。我猜这些是table1(user_id_user, created_at, id_table1)table3(id_table3, sub_group, item)