Live data from Hacker News

SQL Anti-Patterns

datamethods.substack.com

111–120 of 222 posts

Re: SQL Anti-Patterns

#111

Earlier quoted context omitted.

Style opinions are borderline irrelevant without appropriate linters.

Go and use Google BigQuery auto-formatter in a complex query with CASE and EXTRACT YEAR FROM date, and you will have a totally different opinion. How that auto-formatter indents is borderly almost a hate crime. A thousand times better to indent manually.

I've even seen the BigQuery formatter change the behaviour of a query, by mixing a keyword from a comment into the real code.

Re: SQL Anti-Patterns

#112
post #104
post #84

Earlier quoted context omitted.

Or, for a boolean type, that XOR is the same as the inequality operator.

Maybe it’s confusing because it’s misnamed?

This is like saying the non-negative integers under addition, lists under append, and strings under concatenation are all just misnamings of the semigroup operator.

https://hackage.haskell.org/package/base-4.21.0.0/docs/Data-...

Re: SQL Anti-Patterns

#113

Earlier quoted context omitted.

I've been told similar nasty things for adding LIMIT 1 to queries that I expect to return at most a single result, such as querying for an ID. But on large tables (at least in sqlite, mysql, and maybe postgress too) the database will continue to search the entire table after the given record was found.

Only if your table is missing an unique index on that column, which it should have to enforce your assumption, so yeah LIMIT 1 is a code (or schema in the case) smell.

IDs are typically unique primary key. But in my experience, adding LIMIT 1 would on average halve the time taken to retrieve the record.

I'll test again, really the last time I tested that was two decades ago.

Re: SQL Anti-Patterns

#114
> SQL is one of those languages that looks simple on the surface but grows in complexity as teams and systems scale.

The funny thing is it's actually several of those languages. :-)

Re: SQL Anti-Patterns

#115

Earlier quoted context omitted.

Only if your table is missing an unique index on that column, which it should have to enforce your assumption, so yeah LIMIT 1 is a code (or schema in the case) smell.

IDs are typically unique primary key. But in my experience, adding LIMIT 1 would on average halve the time taken to retrieve the record. I'll test again, really the last time I tested that was two decades ago.

You are certainly doing something wrong if that's true.

I'm curious, can you demo this?

Re: SQL Anti-Patterns

#116
the points are fine and helpful, but they seem like a note from the author to themself rather than a cheatsheet that tries to be exhaustive.

was surprised to not see anything about dates/time.

Re: SQL Anti-Patterns

#117
post #55

Earlier quoted context omitted.

Set theory... There are self-identifying "senior software engineers" that cannot understand what even an XOR is, even after you draw out the entire truth table, all four rows.

I am surprised at common it is for software engineers to not treat booleans properly. I can’t tell you how many times if seen ‘if(IsFoo(X) != false)’ It never used to bug me as a junior dev, but once a peer pointed this out it became impossible for me to ignore.

The most egregious one I saw, I was tracking down a bug and found code like this:

    bool x;

    ...

    if (x == true) {
        DoThing1();
    } else if (x == false) {
        DoThing2();
    }
And of course neither branch was hit, because this is C, and the uninitialized x was neither 0 nor 1, but some other random value.

Re: SQL Anti-Patterns

#118

Earlier quoted context omitted.

Only if your table is missing an unique index on that column, which it should have to enforce your assumption, so yeah LIMIT 1 is a code (or schema in the case) smell.

IDs are typically unique primary key. But in my experience, adding LIMIT 1 would on average halve the time taken to retrieve the record. I'll test again, really the last time I tested that was two decades ago.

That seems like your RDBMS wasn't handling something right there or there wasn't a unique index on the column.

Do you recall what the database server was?

Re: SQL Anti-Patterns

#119
post #37

Earlier quoted context omitted.

The very next ask will be "order the zipcodes by number of customers" at which point you'll be back to aggregations, which is where you should have started

distinct seems like an aggregation to me

[deleted]

Re: SQL Anti-Patterns

#120

"Instead you should: query WHERE name = ‘abc’ create an indexed UPPER(name) column" Should there be an "or" between these 2 points, or am I missing something? Why create an UPPER index column and not use it?

I think they reversed the 2 expressions. You should use “WHERE UPPER(name) = ‘ABC’” if you want to use the index.
Post reply on HN