←back to thread

SQL Design Patterns (2010)

(vadimtropashko.wordpress.com)
149 points mci | 8 comments | | HN request time: 0.805s | source | bottom
1. potatoproduct ◴[] No.45077193[source]
Not ashamed to admit that I never really thought about the distinct operator 'being redundant' as its essentially just a group by.
replies(3): >>45077523 #>>45078843 #>>45079717 #
2. morkalork ◴[] No.45077523[source]
distinct has always felt like a query smell to me. Too many junior analysts abusing it because they don't know the schema well and are over-joining entities
replies(2): >>45078136 #>>45078258 #
3. dspillett ◴[] No.45078136[source]
DISTINCT is often a smell at the head (or middle) of a complex query as you are throwing away processed information, sometimes a lot of it, late in the game. Much better to filter it out earlier and not process it further, where possible. Filtering earlier, as well as reducing waste processing time (and probably memory use), increases the chance of the query planner being able to use an index for the filter which could greatly decrease the IO cost of your query.
4. ryanjshaw ◴[] No.45078258[source]
Sometimes the number of joins is fine but they don’t understand the data properly and should be spending more time understanding why multiple rows are being returned when they expect one (eg they need to filter on an additional field).

I wish SQL had a strict mode syntax that forces you to use something like `select one` (like LINQ’s Single()) or `select many` to catch these kinds of bugs.

5. stevage ◴[] No.45078843[source]
Huh, I have always just thought of it as a syntactic shortcut.
6. paulddraper ◴[] No.45079717[source]
SELECT DISTINCT is often a code smell. (Not always.) If you see it, there’s a 70% chance it got slapped on to fix an issue that should have been solved a different way.

SELECT DISTINCT ON is different, and useful.

replies(1): >>45081149 #
7. aspaviento ◴[] No.45081149[source]
I had a teacher who had specific rules for exams when we wrote SQL statements:

- For a question worth 2 points, if you use the word "DISTINCT" when it wasn't needed, you lose 0.5 points.

- If you don't use "DISTINCT" when it was necessary, you lose all 2 points.

replies(1): >>45083817 #
8. 9dev ◴[] No.45083817{3}[source]
Oh yes, introducing a little game theory anxiety into exam questions sounds like a wonderful little torturing tool!