Live data from Hacker News

SQL Tips and Tricks

github.com

121–130 of 168 posts

Re: SQL Tips and Tricks

#121

A common mistake I see is that people think foreign keys will automatically create indexes. Missing indexes is a general problem in SQL. Missing indexes on columns that are in foreign keys are even worse.

In some RDBMS a foreign key will automatically create an index: https://dev.mysql.com/doc/refman/8.4/en/create-table-foreign...

I think this falls under the read the documentation fully point.

Edit: It occurs to me you likely meant on the column itself rather than on the referenced column. I don't have an example that does that.

Re: SQL Tips and Tricks

#122

Earlier quoted context omitted.

Instead of joining tables and using distinct or similar to filter rows, consiser using subquery "columns", ie in SELECT list. What does this mean? Running SELECT column1, ( SELECT column2, column3, ... FROM table_b WHERE table_a.id = table_b.a_id ) FROM table_a Results in "subquery must return only one column" as I expected. You mean returning the multiple columns as a record / composite type? Keep in mind GROUP BY c…

Sorry, was on mobile so hadn't patience to type examples. SELECT column1, ( SELECT column2 FROM table_b WHERE table_a.id = table_b.a_id ) as b_column2, ( SELECT column3 FROM table_b WHERE table_a.id = table_b.a_id ) as b_column3 FROM table_a It might look like a lot more work, but in my experience it's usually a lot faster. YMMV but check it.

Would a cross apply accomplish the same result without the risk of multiple rows?

Cross apply (select top 1 ... ) x

Re: SQL Tips and Tricks

#125

Earlier quoted context omitted.

That's a totally valid point haha.

I'll try to give some constructive criticism instead of a drive by pot shot. I'm sorry, it's just that the leading commas make my eyes bleed and I really hope the industry moves away from it. On point 3: What I do is use CTEs to create intermediate columns (with good names) and then a final one creating the final column. It's way more readable. ```sql with intermediate as ( select DATEDIFF(DAY, timeslot_date, CURRENT…

I love leading commas and am leading the charge for change.

I apologize in advance and hope you are able to come to grips with how easy it is to read and understand at some point in the near future.

Re: SQL Tips and Tricks

#126

Leading comma is nice in SELECT statements because you can comment toggle individual lines. Indenting your code to make it more readable is basically what anyone with room temperature IQ does automatically. A lot of these other tips look like they're designed to deal with SQL design flaws, like how handling nulls isn't well defined in the spec so it's up to each implementation to do whatever it wants.

A lot of databases support trailing commas in select clauses. Which is just as well. I want to scratch my eyes out every time I see someone formatting with comma starting the lines. It's the kind of foolish consistency that is a big part of performative engineering.

I host to help in the continued scratching of your eyes out in the future.

Leading commas are king. Simple as.

Re: SQL Tips and Tricks

#127
post #117

I don't get the point of the dummy value. How does it help doing anything? I can add conditions with ease without it.

Because any subsequent clauses usually start with AND, so if you're just checking data or validating it outside of production, it makes sense since you can comment to see lines you aren't checking out.

It also depends on how you write SQL, but not by much.

I wouldn't put a dummy variable into a finalized query.

Re: SQL Tips and Tricks

#128

I'll add some of mine: Learn your DB server. Check the query plans often. You might get surprised. Tweak and recheck. Usually EXISTS is faster than IN. Beware that NOT EXISTS behaves differently than EXCEPT in regards to NULL values. Instead of joining tables and using distinct or similar to filter rows, consiser using subquery "columns", ie in SELECT list. This can be much faster even if you're pulling 10+ values fr…

Agreed. Learn how to use EXPLAIN and interpret using whatever tools you prefer. Also monitor your queries. Something I did in a previous start up was install PgHero, and man did that tool help me optimise and prioritise performance.

Re: SQL Tips and Tricks

#130

My tips for working with complex Stored Procedures. 1. At the beginning of the proc, immediately copy any permanent tables into temporary tables and specify/limit/filter only for the rows you need. 2. In the middle of the proc, manipulate the temporary tables as needed. 3. At the end of the proc, update the permanent tables enclosed within a transaction. Immediately rollback transaction/exit the proc, if an error is…

And remember that these rules may be completely valid in one vendor's database, but another's may have very different priorities/characteristics/trade offs.

Also, the version of the database can matter, too.

Post reply on HN