Hey,
This article is completely written by me. I just asked ChatGPT to help me choose the key points, so forgive me if it’s not perfect.
Today, I was trying to break the boredom of repeated studying, so I went to LeetCode to solve some SQL problems. There was an easy one that required using CHAR_LENGTH to check the content length of a tweet and determine if it’s valid.
That made me think: why would I even use CHAR_LENGTH, or care that it exists, when I can just calculate the length in my application logic (like using .Length in C#)?
The answer is simple: in many real-world scenarios, you need to filter data in the database to avoid fetching huge amounts of unnecessary data into your application.
Imagine a scenario where I want to check product descriptions. If a description is less than 50 characters, I mark it as low quality or exclude it from the results. What should I do?
Fetch all products (imagine something like Amazon), then filter in my application?
Or filter directly in the database using CHAR_LENGTH, and only fetch what I actually need?
The key measure here is not where the computation happens — it’s how much data is being transferred. An extra computation inside the database is far cheaper than moving millions of rows to your application and then filtering them.
That said, I’m not saying everything should be done in the database. There are cases where filtering in the application makes more sense — for example, when the business logic is complex, or when the dataset is already small.
PS: I’ve read that using functions like CHAR_LENGTH can affect index usage, but I don’t fully understand the cost yet. I’ll read more about it, and if I form a solid understanding, I’ll write about it.
I know this is a simple idea, but I wanted to get it out of my head and into words.
Top comments (0)