5 ms·
I know SQL inside and out, and I hate it for anything put trivial queries that involve a single table and a single WHERE clause. Every time I write a query of
by crazygringo 12y ago
I know SQL inside and out, and I hate it for anything put trivial queries that involve a single table and a single WHERE clause.
Every time I write a query of reasonable complexity, I have to figure out what I want the database to do (in an imperative sense -- read from this table first, then look up this in that table, then do this other thing with this other table for each row), and then figure out how to translate this into a declarative SQL statement, just so the database's query interpreter can turn that back into an imperative program it can run.
The whole thing is just a huge unnecessary round-trip of thinking, and SQL is limited in that there are many simple things it can't do, and you never really have any guarantee that the database's query analyzer will execute your query imperatively in the manner you hope for (which can lead to terrible performance problems, and hacks like index "hints" still don't always solve it). I'd MUCH rather be able to program against the database imperatively, directly.
So despite using SQL day-in, day-out, I totally agree that it is weird, obtuse, and a kind of "non-language" (in the sense that any query is limited to a single declarative statement, with a host of limitations, instead of an imperative programming language that allows you to write whole programs of execution).
- nwenzel 12y agoGood points all the way around. Do you think SQL's obtuseness comes from it's ability to do more than (perhaps) it should? Maybe it really only should be used for simple queries with simple where clauses. But because it can be used for more, we choose to do so. It's based on relational algebra making it fairly predictable (query optimizer aside which is completely unpredictable). It operates on relations and returns relations. "Select * from MyRelation;" returns a relation. So we could also write "select * from (select * from MyRelation);" and get the same result. You can continue that nested select as many times as you like and with as much complexity as you like and it would still work. Though undoubtedly it gets difficult to follow. Adding to the obtuseness, SQL is not typed. So for "select * from MyRelation where SomeField = 1;" you could replace the 1 with another select statement, which would return a relation. But as long as that relation had a single column and a single row, it would be interpreted as a single value and successfully replace the 1. Also adding to the obtuseness as you get more complex is that it variable scope for table aliases can be difficult to follow. Table alias names can be used across various select, where, from and other clauses to link them together through a common filter but it happens in the single SQL statement. I think it's that common issue of reducing a "program" into a so gel statement that causes the issues I run into. At times, I feel that I am writing what basically amounts to a stored procedure in a single statement. That's when I start getting mad at SQL. Though, it's really my own choices that made it a problem.
- sanderjd 12y agoThis seems to be a good example of not allowing a fundamentally different paradigm to "sink in" but rather attempting to impose a paradigm you're already comfortable with onto it. Relational databases really aren't imperative systems, and while SQL is a pretty good declarative language for relational systems, it is indeed very frustrating to attempt to use it imperatively.