DEV Community

Cover image for The Scariest Lines of Code I've Ever Written. Boooo!!!
Cesar Aguirre
Cesar Aguirre

Posted on Originally published at canro91.github.io

The Scariest Lines of Code I've Ever Written. Boooo!!!

I don't remember if it was rainy or sunny outside. It was more than five years ago. I was still becoming a senior coder.

I've written hundreds of thousands of lines of code that I don't remember anymore. But I still remember these ones.

As part of my daily tasks, I wrote this query in a stored procedure,

SELECT * FROM dbo.HugeTableWithoutIndexes
WHERE DATEDIFF(DAY, ADateColumn, @InputDate) = 0
Enter fullscreen mode Exit fullscreen mode

I was working with an email system built with Amazon SES. I needed to show all email activity: delivered, opened, bounced, and so on. I wrapped a column in a function. A deadly sin!

The table had millions of records and no indexes. My query forced a full scan of the table. The next thing I knew the server was on fire. Not literally, of course. LOL!

Those days I refused to learn SQL, convinced ORMs and NoSQL were enough. I couldn’t have been more wrong. Relational databases and SQL still reign.

Eventually I learned about indexing, scans vs seeks, and SQL Server internals. Shout out to Brent Ozar's courses.

A painful lesson I will never forget. Take your vitamins, exercise, and use indexes.

You can't escape from SQL. That's why I made it one of the lessons in Street-Smart Coding. It's the guide I wish I'd had on my journey to becoming a senior coder.

Top comments (0)