In a lot of ways its a perfect optimization as far as long term performance goes. Just remove the rollback, commit that shit, push it to master, and call it a day.
A table like that will have foreign key relations in other tables with cascading deletes. It is at least likely to. So the database will have to cascade all those deletes or in the very least have to calculate the deletes.
I know tons of large enterprise database on really well known companies that definitely don’t care about foreign key integrity at all and usually only use indexes, if anything
What are the performance characteristics of a DELETE vs a COUNT?
Delete is a lot slower. COUNT often doesn't needs to touch the table because you can count the entries in the primary key instead, which leaves the data pages unlocked and available for other queries. Some engines store the count with the index, making this ridiculously fast.
Delete on the other hand has to lock every row, delete the item from the data page, delete the item from all indexes, and check for foreign key violations or cascade deletions.
Databases are extremely optimized and they generally don't actually run the query as you write it. Many DBs preprocess the queries into tree structures and run optimization procedures on them before the data is even checked. Caches, indexes, additional layers of memorization then all work to make this even faster. It's quite foolish to think you can out-perform the DB
How did you make this image? It seems to auto adapt to light mode and dark mode ... is there like a black layer with 80% opacity so on dark mode it is black and light mode it is grey?
I generated this from ray.so. After making the code block, simply toggle the background button (turn it off), then click on the code block first and press Ctrl + S. It will export your image like this.
This looks an awful lot like C# and MSSQL to me, and if I remember correctly there is indeed a transaction.CreateCommand() although it has been a little while so I'm not 100% sure. That said you absolutely can have commands with no transactions... Or maybe it just defaults to AutoCommit() = true which is effectively the same thing
IIRC, and this may vary by engine/product, every SQL command exists within a transaction implicitly. In this case is it just saying "Don't use the implicit transaction, use this one?"
For me the real pain is, I can ask the DB connection for a command, and I can ask the DB connection for a transaction. Why can't I ask the transaction for a command?
i mean this is abysmal but the main bit i got annoyed at for some reason is the constant usage of var. i mean i'm assuming you could be more explicit with this? i dunno i guess that's just me not liking var.
405
u/Rainmaker526 May 31 '26 edited May 31 '26
What are the performance characteristics of a DELETE vs a COUNT?
This might be an unconventional optimization, but if it makes a large enough difference...
5
u/my_new_accoun1 May 31 '26
How did you make this image? It seems to auto adapt to light mode and dark mode ... is there like a black layer with 80% opacity so on dark mode it is black and light mode it is grey?
435
u/NeverMakesMistkes May 31 '26
2
u/stupidpunk138 Jun 03 '26
After call it you know how much employee are in DB; this is sure...
160
u/Wurstgewitter May 31 '26
Nice one, personally I do SELECT GROUP_CONCAT(id) FROM users; and then just count the commas + 1
74
u/TichShowers May 31 '26
Hope the table never implements soft deletion.
42
u/MichiRecRoom May 31 '26
For me, the real horror is the
command.transaction = transaction. It implies that a command can exist without an attached transaction.Seriously, I'd hope that something like
transaction.CreateCommand()is supported. This code as-is feels genuinely wrong to look at.12
u/4ngryMo May 31 '26
Just commit the transaction and return
0.10
u/Hottage [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 31 '26
Disgusting.
Didn't even make it an extension method smh my head.
9
u/robilco May 31 '26
DBA's hate this one trick!
30
u/Zerodriven May 31 '26
Title implies getting count. Approved. Merged into 5 million user system 0 comments.
"Why do we have no users?" - Ask our DBA team, we don't know what they do with our data, our code is 100% not the problem.
8
u/Kevdog824_ May 31 '26
Works 100% of the time the first time
5
u/_giga_sss_ May 31 '26
Cascade*
1
u/levi73159 Jun 02 '26
This is c# right
1
u/russellvt Jun 02 '26
That could be a "neat" race condition ... LOL
3
u/Brilliant-Parsley69 May 31 '26
They should have wrapped it into a service and s repository layer. It would be more readable and less confusing, duh. 🙄
3
u/RichCorinthian May 31 '26
No.
The number returned may be higher than the number of rows deleted in the named table. (If there’s a trigger without
SET NOCOUNT ON, for example).3
u/the-midnight-train May 31 '26
Database admins hate this trick
2
u/icebreaker374 May 31 '26
I know very little outside of PowerShell, and even I had to stop and read this a second time to make sure I was reading it right LOL
2
u/BasieP2 Jun 01 '26
He forgot a dispose.. The command is an IDisposable as well
2
u/NoNameSwitzerland May 31 '26
Should that be more like DELETE FROM employees where active=1 or something like that? You probably do not want to count retired employees.
1
u/ItIsTooMuchForMe May 31 '26
JFC
1
u/a1rwav3 May 31 '26
Why do a select when you can put a table lock lol
1
u/Wide_Language4620 May 31 '26
Haha
1
u/EagleCoder May 31 '26
At least put the
rollbackin afinallyblock...1
u/Slow_Eye_1783 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Jun 01 '26
i mean this is abysmal but the main bit i got annoyed at for some reason is the constant usage of var. i mean i'm assuming you could be more explicit with this? i dunno i guess that's just me not liking var.
1
u/boomybx Jun 02 '26
What font and color theme is that?
1
u/SimplexFatberg Jun 02 '26
Move over OOP, PWFOP is here - Playing With Fire Oriented Programming
1
u/One-Celebration-3007 Jun 04 '26
the method names hurt to look at
1
u/Hot_Vanilla_3621 Jun 05 '26
Bitter developer who needed a raise?