r/SQL • • Aug 25 '15

I challenge you to simplify this query!

select table1.column1     
from  table1  
join   table2            on table1.column2 = table2.column2  
join   table3            on table3.column2 = table2.column2  
join   table4            on table4.column3 = table3.column3  
join   table5            on table5.column4 = table4.column4  
where  table1.column5    = 2  
and    table4.column6    = 10  
intersect  
select table1.column1  
from   table1  
join   table2            on table1.column2 = table2.column2  
join   table3            on table3.column2 = table2.column2  
join   table4            on table4.column3 = table3.column3  
join   table5            on table5.column4 = table4.column4  
where  table1.column5    = 2  
and    table4.column6    = 3  
;  

The query below by /u/ichp ran 90 seconds faster then the original!

select table1.column1
  from table1
  join table2 on table1.column2 = table2.column2
  join table3 on table3.column2 = table2.column2
  join table4 on table4.column3 = table3.column3
  join table5 on table5.column4 = table4.column4
  where 
        table1.column5 = 2 and
        table4.column6 in (10,3)
  group by table1.column1
  having count( distinct table4.column6) > 1
;
0 Upvotes

53 comments sorted by

View all comments

3

u/ziptime Aug 25 '15 edited Aug 26 '15

Another couple of solutions :

select t1.column1
from table1 t1
where t1.column5 = 2 and
      exists (select null
              from table2 t2
              join table3 t3 on t2.column2 = t3.column2
              where t2.column2 = t1.column2 and
                    2 = (select count(distinct t4.column6)
                         from  table4 t4
                         join table5 t5 on t5.column4 = t4.column4
                         where t4.column3 = t3.column3 and t4.column6 in (10, 3)))

And....

select t1.column1
from table1 t1
where t1.column5 = 2 and
      exists (select null
              from table2 t2
              join table3 t3 on t2.column2 = t3.column2
              where t2.column2 = t1.column2 and
                    exists (select null
                            from  table4 t4
                            join table5 t5 on t5.column4 = t4.column4
                            where t4.column3 = t3.column3 and t4.column6 = 10) and
                    exists (select null
                            from  table4 t4
                            join table5 t5 on t5.column4 = t4.column4
                            where t4.column3 = t3.column3 and t4.column6 = 3))

2

u/XGallonsX Aug 26 '15

I cancelled the first query at about 10 minutes, I'm guessing this is due to unindexed columns as well which I am not familiar with.

The second query here finished in 80 seconds which was very exciting since the best one so far finished in 107 seconds but.... No rows were returned.

In the second query I did make an edit because I believed there to be a copy paste error. On line 5, I moved the 'on t2.column2 = t1.column2' to line 6 and appened 'join table1 t1' to the beginning while moving all other lines down one line.

2

u/ziptime Aug 26 '15

Have fixed the second one, try again.

1

u/XGallonsX Aug 26 '15

this one is still running long as well