Live data from Hacker News

SQL Tips and Tricks

github.com

71–80 of 168 posts

Re: SQL Tips and Tricks

#71
post #8

I remember doing the "WHERE 1=1" trick in my last job and it causing a... let's say "unproductive", discussion in the pull-request.

What about `WHERE true`?

I don't understand the point at all. If you need to add some condition later on, why not just add it then? What benefit is there to just marking out the spot where you might add the condition at some point in the future?

Re: SQL Tips and Tricks

#73
post #54

Earlier quoted context omitted.

Relevant for applications as well, when a table only has a few thousand entries, a scan is not the end of the world and not even an outage in waiting. I agree with you that one should seek when possible as part of normal query optimization, but depending on your data, it could also just easily be something you can live with forever.

You can’t control the growth rate of your tables, you can only estimate it. When we design for reliability we want to remove single cause failures and make a best effort to reduce dual cause failures. We definitely don’t want two failures from a single cause. What reason might the lack of indexes suddenly become a critical issue? And what other things might you be scrambling to deal with at the same time? Tables migh…

“And what’s the harm in making it?“

Increased storage and slower inserts?

Re: SQL Tips and Tricks

#74
post #19

Never use WHERE 1=1. It is both a security risk and a performance risk to run dynamic ad-hoc queries.

What is a dynamic, adhoc query? Why does adding 1=1 support that?

Lets say its 2001 and you are writing some hot e-commerce stuff in plain php. You want to filter data depending on multiple fields in the submitted form. If some field is there, you add one more "AND" clause to the "WHERE", like this: if (isset($_POST['product'])) { $query .= "AND product = " . $_POST['product']; }. So in order not to check every time if the added clause is the first one you start with "WHERE 1=1 ", as "WHERE AND ..." would not work.

Re: SQL Tips and Tricks

#75
post #56

The "readability" section has 3 examples. The first 2 are literally sacrificing readability so it's easier to write, and the last has an unreadable abomination that indenting is really not doing much.

Who splits column per line in the SELECT block and still leave 150 character wide lines? This is a fucked up definition of legibility. I can’t even get started on the commas. NOBODY CHECKS LONG LINES IN CODE REVIEWS. That was the biggest problem with AngularJS. People mishandling merges and breaking everything because the eyes start to glaze over at column 90. I’ve been on more than half a dozen teams with CRs and it…

This could be a great comment if the tone was different. I'll try to give my perspective.

SQL, unfortunately, is very verbose and has a strange mix of super-high and very low abstraction. There is also no SQL formatter out there that does a decent job, and no real consensus about how good SQL is supposed to look.

If I look at the 'indent' guideline, it contains e.g.:

  , IFF(DATEDIFF(DAY, timeslot_date, CURRENT_DATE()) >= 29, 
  LAG(overnight_fta_share, 2) OVER (PARTITION BY timeslot_date, timeslot_channel ORDER BY timeslot_activity), 
   NULL) AS C28_fta_share
Immediate SQL failures: 1) it has no easy facility to pull that DATEDIFF clause in a different variable/field. 2) The LAG line is verbose, especially if your DB doesn't allow to pull out the WINDOW clause.

Re: SQL Tips and Tricks

#76

Earlier quoted context omitted.

What about `WHERE true`?

I don't understand the point at all. If you need to add some condition later on, why not just add it then? What benefit is there to just marking out the spot where you might add the condition at some point in the future?

It is a bit silly but I think it just helps with code readability some people.

Re: SQL Tips and Tricks

#79

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.

This is only true if you comment out the last line in the SELECT clause. It’s ugly code and the justification doesn’t pass the sniff test.

Re: SQL Tips and Tricks

#80
Everybody is up in arms about the comma suggestion but everyone thinks the 1=1 is a good idea in the where clause? If I saw that in a code review I don’t know what I’d think of the author.
Post reply on HN