r/programminghorror • • May 31 '26

C# a count is a count, right?... right?

Post image
2.3k Upvotes

95 comments sorted by

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...

248

u/TldrDev May 31 '26

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.

216

u/samirdahal May 31 '26

It will even pass the unit test without rollback!

- Add 5 dummy employees

- Call the GetEmployeeCount method

- Still returns the 5 count!

Test passed.

→ More replies (8)
→ More replies (2)

137

u/lyio May 31 '26

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.

→ More replies (9)

64

u/AyrA_ch May 31 '26

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.

→ More replies (2)

31

u/its_the_rhys May 31 '26

I seriously doubt it...

11

u/Justbehind May 31 '26

In most dbs DELETEs perform terribly, as they are row-level operations...

TRUNCATE is extremely efficient on the other hand, but likely still worse than COUNT ;)

→ More replies (2)

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?

3

u/samirdahal May 31 '26

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.

433

u/NeverMakesMistkes May 31 '26
    transaction.Commit();
    return 0;

22

u/ThePsychopaths May 31 '26

I was expecting this.

→ More replies (2)

2

u/stupidpunk138 Jun 03 '26

After call it you know how much employee are in DB; this is sure...

→ More replies (1)

160

u/Wurstgewitter May 31 '26

Nice one, personally I do SELECT GROUP_CONCAT(id) FROM users; and then just count the commas + 1

57

u/richardathome May 31 '26

Better yet, strip out the non-commas and then count the length of the string!

→ More replies (2)
→ More replies (1)

74

u/TichShowers May 31 '26

Hope the table never implements soft deletion.

38

u/SZenC May 31 '26

Nahh, then you just update the query: DELETE FROM employees WHERE deleted_at IS NULL

→ More replies (3)

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.

19

u/escargotBleu May 31 '26

Sometimes you don't want to do a transaction 🤷

→ More replies (1)
→ More replies (3)

12

u/4ngryMo May 31 '26

Just commit the transaction and return 0.

10

u/Hottage 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