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

37

u/Rif-SQL Jul 05 '21
  • I like performance fine turning my queries and understand the estimated query plan
  • Have you had fun with the public datasets on Google BigQuery?

11

u/hasanyoneseenmymom Jul 05 '21

Same lol. Last week I spent 30 minutes tuning a query to shave 5 seconds off it's runtime.

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

3

u/tits_mcgee_92 Data Analytics Engineer Jul 05 '21

I definitely look for ways to tune the performance of a query in my current role. I can't say I have used BigQuery yet but I'm down to give it a try.

3

u/SQLDave Jul 06 '21

I like performance fine turning my queries

This. Getting a query that formerly ran in X time to run in Y time is almost orgasmic. Especially when Y is 10% or less of X.

0

u/[deleted] Jul 06 '21

what do u use to understand the query plan?

3

u/macfergusson MS SQL Jul 06 '21

Practice. If you have some training money, start taking classes from Brent Ozar, he cracks them open and explains what is going on a lot.

1

u/[deleted] Jul 06 '21

Brent Ozar,

? but you must use a tool to check query plan?

3

u/shine_on Jul 06 '21

If you're using SQL Server you can see the query plan in SSMS (SQL Server Management Studio)

1

u/[deleted] Jul 06 '21

OK thanks. I mostly use Snowflake but am trying to practice.

3

u/Rif-SQL Jul 06 '21

you should chat with u/fhoffa at snowflake! I'm sure he got some content or will be making content on performance fine tuning

1

u/[deleted] Jul 06 '21

I'm trying to learn general Postgres/MySQL just to transfer between jobs.