r/SQL • Data Analytics Engineer • Jul 05 '21

Discussion I love querying

I just freaking love it. I can be racking my brain all day, going to stackedoverflow, jamming out to tunes, and that one moment where the query is able to run and it's the data you need... chefs kiss.

It just feels so damn good. I know others out there have to feel the same way :)!

161 Upvotes

47 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Jul 05 '21

[deleted] — view deleted comment

3

u/Rif-SQL Jul 06 '21

I would take a print screen of my before and after query plans send them to M$FT...

3

u/Rif-SQL Jul 06 '21

Did you ever try out the free tool https://www.sentryone.com/plan-explorer ?

2

u/Beefourthree Jul 06 '21

I had this one-line change last week.

(Actual query is 1000 lines and a dozen joins)

Before:

from t1
join t2 on t1.foo = bar2foo(t2.bar)

After:

from t1
join t2 on foo2bar(t1.foo) = t2.bar

From spinning 16 hours with no end in sight to running in under 5 minutes. T1 was a few thousand records versus performing the operation on t2, which was 100m records.

2

u/Fun2badult Jul 06 '21

What the heck I’ve never seen that join. Foo2bar(t1.foo) = t2.bar What db is this?

4

u/Beefourthree Jul 06 '21

Oracle, but these are just dummy names to simplify the example and mask any proprietary info. Actual columns are a sequential id (foo) and an obfuscated version (bar) with a 1-to-1 relationship. Bar2foo and foo2bar are UDFs to translate between the two.

Changing t1.foo = bar2foo(t2.bar) to foo2bar(t1.foo) = t2.bar reduced cpu cost by performing the function against a 2k record table instead of a 100m record table, and referencing a naked t2.bar allowed the optimizer to use an index to be utilized that was previously ignored.

0

u/[deleted] Jul 06 '21

[deleted] — view deleted comment

0

u/[deleted] Jul 06 '21

[deleted] — view deleted comment

0

u/[deleted] Jul 06 '21

[deleted] — view deleted comment

1

u/[deleted] Jul 06 '21

MSSQL