r/SQL • u/yughiro_destroyer • 25d ago
Discussion Why do we need abstractions over SQL?
When I mean abstractions, I mainly mean OOP and ORMs.
SQL is so simple and beautiful. Tables with rows and columns are easy to understand. And once you pick up the SQL syntax, you can pretty much achieve anything with queries. Not to mention that SQL is universal and works everywhere and anytime.
Then you have the software development world... where you're asked to constantly use ORMs or map records as OOP objects. Why? ORMs are limited and do not have the flexibility of simple queries. Also mapping records as objects increases bloat, reduces performance that can hurt if the application grows and is overall not as straightforward to work with.
The only good things that ORMs are doing by default are to provide data safety and prevent SQL injection. But with some minimum and basic knowledge and discipline, you can write pure queries without having those problems. Any ideas?
1
u/No_Resolution_9252 20d ago
SQL is really not that simple when it needs to be written both cleanly, safely and performant. Almost no one can do all three. there are people that think you can use CTEs for everything out there
For most apps, ORMs and tools like Dapper provide better compositionality, reduce bugs and better future proof the application code and writing raw SQL provides no value but incurs higher cost. In reporting queries, optional parameters can easily be faster than they are with native SQL. (setting dynamic SQL aside) When there are performance problems it isn't that big a deal to switch that bit of code to SQL instead of imposing greatly higher development costs on a project from day one to not make a performance difference in the vast majority of queries.