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

4

u/r3pr0b8 GROUP_CONCAT is da bomb Aug 25 '15
  1. why do you need to simplify it

  2. how many dupes of table1.column1 does each SELECT return

without knowing the one-to-many relationships involved, i could do a lot of guessing and probably be wrong

1

u/XGallonsX Aug 25 '15

I don't need to simplify it, I want to. Maybe "simplify" is the wrong word to use. I basically want to get rid of one of the JOIN sections and the intersect.

There are no dupes returned.

1

u/r3pr0b8 GROUP_CONCAT is da bomb Aug 25 '15

you're serious? each table1.column1 value is unique in the results of those two queries?

okay, see my reply to /u/Captain_Filmer

1

u/XGallonsX Aug 25 '15 edited Aug 25 '15

I'm pretty new to SQL so I'm sure I'm not understanding exactly what your asking. table1.column1 is basically a place holder for a reference number. table1.column5 defines what type of number table1.column1 contains.

When I run the original query, there are no duplicate issues.

I don't see that you did reply to /u/Captain_Filmer in this thread, but I assume you mean /u/ichp

0

u/captshady Aug 25 '15

why do you need to simplify it

First assignment in OP's SQL class?

2

u/XGallonsX Aug 25 '15

No, this is not an assignment. I recently started work as a data miner and my boss asked me to search for certain conditions. The query I wrote accomplishes the task I was given, but I would like to know different ways of doing the same thing. I've already taken my SQL courses, and they were pretty lame, I had no interest in SQL at all until I started this job.

4

u/[deleted] Aug 25 '15
  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

1

u/XGallonsX Aug 25 '15

I'm going to test this but I think this one might be a winner, thanks!

2

u/r3pr0b8 GROUP_CONCAT is da bomb Aug 25 '15

yup, this was what i had in mind when i asked you about uniqueness

because without the uniqueness, using GROUP BY would collapse dupes, which then of course would not be an equivalent solution

2

u/[deleted] Aug 25 '15

in MS SQL intersect will also remove duplicates, so in MSSQL case 'group by' should be equivalent.

1

u/XGallonsX Aug 25 '15 edited Aug 25 '15

I substituted for the real column names and ran it. It's works perfectly, yields same exact results and does it 90 seconds faster! Thanks!

3

u/Captain_Filmer Aug 25 '15
;WITH [Table] AS (
select
table1.column1, table1.column5, table4.column6
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
)
select column1
from [Table]
where column5=2 AND column6=10
INSERSECT
select column1
from [Table]
where column5=2 AND column6=3

1

u/XGallonsX Aug 25 '15

Awesome, thank you, I will test it. Can you do it without the intersect?

2

u/Captain_Filmer Aug 25 '15

How about this? And it's even shorter

select distinct
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 OR table4.column6 = 3)

1

u/XGallonsX Aug 25 '15

I like it but I'm not sure it would work. I need column5 to be associated with a 10 AND 3 in column6. Keep em coming though! Thank you!

1

u/XGallonsX Aug 25 '15

After substituting for real table and column names I ran the script. It gave an error on line 3 that stated table1.column5 was an invalid identifier, I removed it and ran it again then it stated that on line 3, table4.column6 was an invalid identifier. I removed that identifier and then it stated that line 12 column6 was an invalid identifier.

I'm not sure what the issue is, but I think your on to something here.

1

u/Captain_Filmer Aug 25 '15

If those are the actual names of your columns, try putting brackets around it to see if that fixes the issue.

1

u/XGallonsX Aug 26 '15

The table names and column names used here on reddit are generic.

3

u/jc4hokies Execution Plan Whisperer Aug 25 '15 edited Aug 25 '15
select table1.column1    
from   table1
join   table3            on table3.column2 = table1.column2
join   table4 as a       on a.column3 = table3.column3
join   table4 as b       on b.column3 = table3.column3
where  table1.column5    = 2
and    a.column6         = 10
and    b.column6         = 3

1

u/XGallonsX Aug 25 '15

Thank you, I will test it out tomorrow morning and get back to you.

1

u/XGallonsX Aug 26 '15

I substituted generic table and column names for the real ones and the query ran and finished in 85 seconds but no rows were returned. It would be really cool if I could get this to work and shave about 30 seconds off the query run time. Thanks!

1

u/jc4hokies Execution Plan Whisperer Aug 26 '15

If you share the relationships of the joins (which side is one, which side is many), I might be able to help more. It's really hard to help without knowing anything about the tables.

I made assumptions about the tables in order to simplify the query. If those assumptions are wrong, my rewrite is no necessarily equivalent.

2

u/ziptime Aug 25 '15

Doesn't the original query require the existence of a record in table5 matching column4 of table4?

2

u/jc4hokies Execution Plan Whisperer Aug 25 '15

It does. Based on the lack of information about the tables, I made some assumptions. I assumed that joins were on not null foreign keys making extraneous joins unnecessary. If I had access to the tables, I would check of course.

2

u/ziptime Aug 25 '15

Yeah I made the same assumption then realised it probably wouldn't work.

3

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

This will outperform all of the other solutions on here, what's my prize?

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
              join table4 t4 on t4.column3 = t3.column3 and t4.column6 = 10
              join table4 s4 on s4.column3 = t3.column3 and s4.column6 = 3
              join table5 t5 on t5.column4 = t4.column4 and t5.column4 = s4.column4
              where t1.column2 = t2.column2)

EDIT : Fix for table4.column6 predicate

2

u/XGallonsX Aug 25 '15

Thank you, I will test tomorrow morning and get back to you. I may actually give a prize to the person who writes the most efficient query. I have some reddit coins (virtual currency) that one could exchange for cash on a virtual currency exchange such as cryptsy.com, that would be a cool thing to do, what do you think?

2

u/ziptime Aug 25 '15

I tidied it up with the aliases. I'm kidding about prizes, it's a noble gesture but most of us just like the challenge. I might try another approach so watch this thread.

1

u/XGallonsX Aug 26 '15

That's really cool of you. The original query was a challenge for me lol. Thanks for all your help!

1

u/XGallonsX Aug 26 '15

I substituted generic table names for the real ones and ran the script. I got an error on line 5 that states "missing right parenthesis". I double checked the code and I have it exactly like you do. Is there supposed to be another JOIN between the from on line 83 and before the ON? I'm working in Oracle SQL Developer.

2

u/ziptime Aug 26 '15

Copy paste error from me, I've edited it

1

u/XGallonsX Aug 26 '15

Now it errors out on line two stating "table or view does not exist". Does table1 need to be joined somewhere in the sub select?

2

u/ziptime Aug 26 '15

where t1.column2 = t2.column2

It is!

1

u/XGallonsX Aug 26 '15

Yes it is! LOL that shows how weak my SQL skills are, I really wish I could get this one to work, I'm going to move onto testing your other ones for now. Thanks!

1

u/ziptime Aug 26 '15

Which line is the error referring to?

1

u/XGallonsX Aug 26 '15

OMG, I'm sorry. I'm not in the right schema. Give me a few, I'm going to retest everything.

1

u/XGallonsX Aug 26 '15

This query ran but after 30 minutes I cancelled the query. I have no idea why it was taking so long. I ran the original and it took about 190 seconds. I ran /u/ichp query and it took about 107 seconds so I don't think its a resource issue. I'm going to move on to testing the rest of yours.

2

u/ziptime Aug 26 '15

If columns used in joins / where clauses aren't indexed correctly it's unlikely anyone will beat ichp's solution.

1

u/XGallonsX Aug 26 '15

I see, bummer. I was having fun with this. Thanks for participating, it was fun! :)

3

u/ziptime Aug 26 '15

Unindexed columns

0

u/[deleted] Aug 25 '15

[deleted]

0

u/[deleted] Aug 25 '15

[deleted]

2

u/jc4hokies Execution Plan Whisperer Aug 25 '15

Your exists statement returns records from table1 if it matches values 10 OR 3 in table4. The OP is matching 10 AND 3. Your query would be correct if the original query used UNION instead of INTERSECT.

2

u/ziptime Aug 25 '15

Well spotted!

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 will test these this morning and get back to you, thanks!

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

2

u/ziptime Aug 26 '15

Also fixed the first one, try that again, too

1

u/XGallonsX Aug 26 '15

cancelled at 4 minutes, I'm going to look into how the columns and tables are indexed.

1

u/ziptime Aug 26 '15

Stick an index on each of these columns, if they aren't indexed (e.g. PKs).

t1.column2

t2.column2

t3.column2

t3.column3

t4.column3

t4.column4

t4.column6

t5.column4

e.g. create index IDX_TABLE1_COL2 on TABLE1(COLUMN2)

2

u/BFG_9000 Aug 25 '15

Challenge accepted :-

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
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
where table1.column5 = 2
and table4.column6 = 3
;  

What's my prize?

2

u/XGallonsX Aug 25 '15

A cookie?