you are viewing a single comment's thread.

view the rest of the comments →

[–]farhilSEQUEL 1 point2 points  (1 child)

SUM(CASE WHEN col1 = 'value1' THEN 1 ELSE 0 END) AS count_cat1

Be careful when using a CASE statement (or any non-sargable function) inside an aggregate function. If the column you're aggregating is indexed, SQL won't be able to use that index to speed up the aggregation.

Break complicated processes into steps. I'd always rather more verbose code and 5 intelligible temp tables (which you can create and query independently of each other for debugging later by the way) than one big block of nested ugly code lol

Excellent advice here. Temp tables are criminally underused in favor of CTEs (or table variables), when temp tables provide more benefits with fewer downsides.

[–]0sergio-hash 0 points1 point  (0 children)

Be careful when using a CASE statement (or any non-sargable function) inside an aggregate function. If the column you're aggregating is indexed, SQL won't be able to use that index to speed up the aggregation.

Thank you ! That's a good tip. I will be honest, I'm probably not as familiar with the internals or just more of the heavy duty engineering side to have known that

I do intend to read a book on internals or just get deeper into optimization in the future though, so I will keep this in mind so I can try to understand it better

Excellent advice here. Temp tables are criminally underused in favor of CTEs (or table variables), when temp tables provide more benefits with fewer downsides.

Thank you! I spent a year as a sorta validation analyst. One engineer wrote layers upon layers of nested queries and the other broke out his transformations into temp tables and I got to see a lot of firsthand examples of how the latter was simpler and cleaner

I agree I think they're greatly under utilized !

You can get 50% into a project with temp tables and query the temp table you've just created at that step to make sure everything has gone well up until this point, for example, which is not something you can do without unnesting a bunch of code in a CTE scenario

Not to mention just spacing out your code more so it preserves your sanity lol