Most active commenters
  • sgarland(4)

←back to thread

SQL Design Patterns (2010)

(vadimtropashko.wordpress.com)
149 points mci | 19 comments | | HN request time: 1.088s | source | bottom
1. alphazard ◴[] No.45077668[source]
I always tell people to worry about the data structures that you want the database to maintain for you, and not worry about the SQL. You can always use Google to look up the SQL, or now ChatGPT to generate it for you. SQL is a not-that-great language and it intentionally hides what's going on. It is also different enough between databases that you need to pay attention. So learning to design/think in terms of SQL is probably not worth doing.

The set of data structures that you use to model and index a dataset is worth understanding, and designing in that space is a skill worth learning.

replies(4): >>45078199 #>>45078627 #>>45078711 #>>45081027 #
2. yakshaving_jgt ◴[] No.45078199[source]
For posterity, how would you recommend the average working programmer should go about doing that?
replies(2): >>45078239 #>>45079355 #
3. ryanjshaw ◴[] No.45078239[source]
Read code from other projects
4. sgarland ◴[] No.45078627[source]
Frankly, this is terrible advice. If you’re not designing your data model around the language it’s going to be queried in, how do you expect to get decent performance out of the database?

Also, in no way does SQL hide anything - it’s a declarative language, and will produce exactly what you tell it to, provided you understand what it is you asked it to do. The query engine is somewhat of a black box, but that is completely orthogonal.

replies(2): >>45078761 #>>45079856 #
5. awesome_dude ◴[] No.45078711[source]
I don't get this - my database is going to be normalised to whatever is optimal (3rd normal form generally, denormalised for higher load/sharded/caching)

The indexing is where the main optimisations take place - hashmap indexes, or clustering indexes for priority queues.

What am I missing?

6. paulddraper ◴[] No.45078761[source]
SQL is a declarative language so it —- by definition —- hides the execution.

Not really sure what you’re trying to argue here.

replies(3): >>45079245 #>>45079749 #>>45082341 #
7. sgarland ◴[] No.45079245{3}[source]
Parent made it sound - to me - that you put an input in and hope for the best. If you understand the operators, you can quite confidently predict an output given an input.
replies(1): >>45079453 #
8. alphazard ◴[] No.45079355[source]
An intro data structures course is worth watching if you haven't taken one. There are plenty of them on YouTube. Try to follow along with a language that has an explicit pointer type. Go is a good choice. Java and Python are worse choices (for this particular thing) IMO.

Assuming you are familiar with trees and hashmaps, you have all the important building blocks. You can imagine a database as a bunch of trees, hashmaps and occasionally other stuff, protected by a lock. First you acquire the lock, then you update some of the data structures, and maybe that requires you to update some of the other data structures (like indexes) for consistency. Then you release the lock.

By default, most data will live in a BTree with an integer primary key, and that integer is taken from a counter that you increment for new inserts. Indexes will be BTrees where the key is stuff you want to query on, and the value is the primary key in the main table.

Using just those data structures you should be able to plan for any query or insert pattern. It helps to figure this out yourself in a programming language for a few practice cases, so you know you can do it. Eventually it will be easy to figure out what tables and indexes you need in your head. In the real world, this stuff is jotted down in design docs, often as SQL or even just bullets.

That's really all you need, and that's where I recommend getting out of the rabbit hole. Query planners are pretty good. You can usually just write SQL and if you did the work to understand what the tables and indexes should be, the planner will figure out how to use them to make the query fast.

replies(1): >>45079772 #
9. halfcat ◴[] No.45079453{4}[source]
> If you understand the operators

That’s the point. In an imperative language if you don’t yet understand (or make a typo, or whatever), you can just print/console.log and find out.

I’ve seen junior devs, data analysts, and LLMs spin their wheels trying to figure out why adding a join isn’t producing the output they want. I don’t think they would figure it out using SQL alone if you gave them a month.

replies(2): >>45081238 #>>45083340 #
10. crazygringo ◴[] No.45079749{3}[source]
You missed the "performance" part.

Depending on how you write your query and how you structure your data, a query can take 0.005 seconds or 500 seconds.

SQL hiding the execution is an extremely leaky abstraction. To get the performance you need, you have to plan your possible queries in advance together with how to structure the data.

I mean, it doesn't matter if you only have 100 rows per table, but once you're dealing with multiple tables with millions of rows each it's everything.

replies(1): >>45083574 #
11. belfthrow ◴[] No.45079772{3}[source]
Java is a bad language for this compared to go? Is this legitimate advice on a serious programming blog. Pretty unbelievable honestly.
12. chasil ◴[] No.45079856[source]
> Also, in no way does SQL hide anything - it’s a declarative language, and will produce exactly what you tell it to.

Ha ha, no, SQL implementations can conform to the standard in unexpected ways.

NULL = NULL

Is that true or false? We didn't know until 2003.

https://en.wikipedia.org/wiki/Null_(SQL)#Criticisms

replies(2): >>45080197 #>>45083323 #
13. gfody ◴[] No.45080197{3}[source]
that's not true or false it's null
14. baq ◴[] No.45081027[source]
You’re kinda right, but designing for a particular RDBMS with awareness of queries which will be performed thus indexes necessary (…or not ;) is really not that far away from what you propose. The only issue is beginner SQL learning material says ‘it’s declarative, don’t worry about what’s happening as long as you get a good result’ and that just isn’t true in any non-trivial applications of SQL.
15. SkiFire13 ◴[] No.45081238{5}[source]
The equivalent of `print`/`console.log` in SQL would be using subqueries/CTE and run them to see the intermediate result (just like `print`/`console.log` show you intermediate results of the executions in an imperative language).
16. teh_klev ◴[] No.45082341{3}[source]
>hides the execution.

But you're not prevented from finding out how your query was executed. For example EXPLAIN (MySQL, Postgres) or query analyser for MSSQL.

17. sgarland ◴[] No.45083323{3}[source]
Considering that all implementations I’m aware of have evaluated that expression to NULL, this isn’t a very pragmatic example. NULL is unknown, and every type has it as a possibility.

The SQL2003 changes were more about how constraints should treat NULL than anything intrinsic about NULL itself.

18. sgarland ◴[] No.45083340{5}[source]
Then you back off, and go back to first principles. Create the minimum example of the problem, and as a sibling comment mentioned, break it down to its constituent parts and observe what happens in each.
19. greekrich92 ◴[] No.45083574{4}[source]
If you use bubblesort instead of quicksort it will take longer as well. Knowing the language and understanding the schema solves this.