5 ms·
Always do a select with your criteria before doing a Delete or update. Don’t ask me how I learned this.
by freetanga 3y ago
Always do a select with your criteria before doing a Delete or update.
Don’t ask me how I learned this.
- mjcohen 3y agoSame thing on a Unix/Linux level: When using find, I always do it first with -print until I see the files I want. Only then do I add the actual action I want.
- lemper 3y agohow did you learn it?
- gumballindie 3y agoCame here to say exactly that! I do a select count and then a limit against the returned count. At least it may reduce the blast radius.
- sgarland 3y agoThis can also bite you if your dataset is larger than the buffer pool (or whatever other RDBMS calls it), and the particular table you’re querying isn’t commonly accessed. Turns out when you start loading millions of rows of useless data into memory, the useful data has to get kicked out, and that makes query latency skyrocket.
- menacingly 3y agoIn most of my scenarios, I'd actually rather cause a catastrophic global change than a silent subtle corruption of a handful of rows
- gumballindie 3y agoThat may be fun in a trivial setup such as op’s but when millions of customers or billions of transactions are affected it’s a nightmare. A competent engineer runs queries against a local and then a uat db, verifies results and then on prod. But if you must do it in prod then it must be limited in scope.
- menacingly 3y agoWe're on the same page with the best approach, I just don't consider corrupting an unpredictable subset of my database much of an improvement. It's not closer to correct, it's just still incorrect.
- jojobas 3y ago>Don’t ask me how I learned this. Joke's on you, we all learned it this exact way.
- seanthemon 3y agoThis gives my heart pains I can't explain to my therapist..
- maccard 3y agoLuckily I learned from someone else. I did figure out start with a transaction the hard way though
- fernandotakai 3y agoi can still feel the pain in the pit of my stomach when i saw the amount of rows affected. thankfully my boss at the time was a amazing db admin and he helped me fixing my mistake.
- chrisandchris 3y agoThat, and some DB tools (line JetBrains DataGrip) block UPDATEs and DELETEs without a WHERE condition.
- tgv 3y agoThen start a transaction, and only commit when the row numbers match.
- _nalply 3y agoOnce I wanted to do `rm -fr *~` to delete backup files, but the `~` key didn't register... Now I have learnt to instinctively stop before doing anything destructive and double-check and double-check again! This also applies to SQL `DELETE` and `UPDATE`! I know that `-r` was not neccessary but hey that was a biiiig mistake of mine!
- lostmsu 3y agoI click in Explorer. Have to confirm too. Never been confused.
- laurensr 3y agoYou remind me of that time I wanted to `rm -rf ./*` but the dot hadn't registered... I now avoid that statement.
- _shantaram 3y agoOne time I was debugging the path resolver in a static site generator I was writing. I generated a site ~/foo, thinking it would do /home/shantaram/foo, but instead it made a dir '~' in the current directory. I did `rm -rf ~` without thinking. Command took super long, wondered what was going on, ctrl-c'd in horror... that was fun.
- xp84 3y agoI’m really curious: without cheating by using the GUI, what would be the proper way to delete such an obnoxiously-named directory? Would “$PWD/~” work?
- r3jjs 3y agoA few ways. rm ./~ is likely the easiest. Another option, shell dependent, would be to turn off shell globbing. The `GNU` version of `find` has a `-maxdepth` option so find . -iname '~' -maxdepth 1 -exec rm {} \; would work, but I don't like relying on `GNU` extensions.
- 3y ago
- iJesus 3y agoGone are the days of hard deletes in my approach; it's exclusively soft deletes now!
- justinclift 3y agoAt a previous place I worked, if they were working on the cli (eg in psql or similar) they'd always use these two steps, either of which would provide adequate protection: 1. Start a transaction before even thinking of writing the delete/update/etc (BEGIN; ...) 2. Always write the WHERE query out first, THEN go back to the start of the line and fill out the DELETE/UPDATE/etc. It worked well, and it's a habit I've since tried to keep on doing myself as well.