News for hackers. Stuff that matters.
Re:SQL queries don't start with SELECT by Pxtl on Thursday October 03, 2019 @05:12PM (#21152276)
This is one of the things MS Entity Framework gets right - it enforces the "start with FROM". It actually ends with SELECT, which makes so much more sense.

Seriously, between navigational properties, group-with-children, EF to me hammers home how bad SQL is. EF is a square peg in a round hole but it does a great job at makign some trivial improvements over SQL, while still remaining true to the principles of relational databases.

Of course, the framework is a hairy mess for other reasons (lazy loading, untranslateable methods, ugly generated queries) but the alternative to SQL it presents is lovely.

Re:SQL queries don't start with SELECT by quickthrower2 on Thursday October 03, 2019 @05:26PM (#21152404)
What do you mean SQL is bad? SQL is very good and some dialects can do some amazing things. I don't think EF is much better, and EF (older Linq2Sql as well) has some gotchas because it converts your expression into SQL, except for when it doesn't and throws a runtime error that it couldn't. And there are some edge cases where you think a C# expression would convert to SQL but alas, it wont.
Re:SQL queries don't start with SELECT by talaketu on Thursday October 03, 2019 @09:20PM (#21153999)
The foundation theory is the relational model of data, and the use of operators from relational algebra. So, we use relations to represent the data in the application domain, and use the operators of relational algebra to take relational inputs and produce relational outputs. It's an extensible and composable model.

When we solve problems using relations, we benefit by thinking in terms of relations.

We can ask: How well does SQL allow us to express an algorithm in relational terms? (Let's ignore that that SQL introduces some non-relational impurities that may have practical value - eg column order, row order, duplicate rows.)

When I examine an SQL expression, and then visualize the relational operators involved, I find a very poor correspondence between the SQL expression and the relational algebra. I need to basically read the entire statement to understand the parts. (Contast to the LINQ examples, or perhaps a shell pipline.) In a way, this is the substance of the original article.

This is one way in which I mean SQL is bad.

Re:SQL queries don't start with SELECT by manigandham on Thursday October 03, 2019 @06:14PM (#21152829)
The point is that LINQ expressions (in the query syntax) can be more readable and straightforward than the equivalent statement in SQL.

EF is just an ORM that benefits from using the LINQ syntax and operators available in .NET/C#.

Re:SQL queries don't start with SELECT by quickthrower2 on Thursday October 03, 2019 @06:31PM (#21152963)
Thanks. Even on just that point, I disagree. I think they are equally readable. Just like Python and C# are equally readable to the respective cohorts that are used to programming in those languages.

Yes, EF queries are more readable to many developers who know C# well but don't want to get used to SQL. But the problem with that is DB apathy + EF leads to some real performance problems as systems scale that grinds the application to a halt with SQL queries longer than dissertations, to which the developers need a quick hack to fix. The query plan for those beasts is intractable. Which of the 10 table scans or index scans to tackle first? And how do you tackle it?

They discover "indexed views" and all is well until you start to have locking problems, so more hacks are needed. etc. By writing the damn SQL to begin with they'd have to tame the beast earlier on and address the design flaws in their schema and/or application.

Ok this is a bit of a strawman (although it is based on experience) but I think saying SQL is less readable is a red herring. It is more readable for a lot of use cases. It's a DSL designed for the job, you really can't get much better (except for Elastic Search use cases, etc.). It's slightly old fashioned but that's just fashion.

Re:SQL queries don't start with SELECT by manigandham on Thursday October 03, 2019 @07:39PM (#21153417)
This isn't about ORMs or database performance. It's just a comparison of query languages, with LINQ being more natural for data models with lots of joins and nested records that are common today (especially since that's how people work with objects inside their programming language).

SQL is far from perfect. It would be nice to see some modern SQL dialects that follow the LINQ syntax and are natively supported by RDBMS.

Re:SQL queries don't start with SELECT by scarface74 on Friday October 04, 2019 @08:28AM (#21156940)
Linq is horrible for outer joins, the syntax is completely unintuitive to the point that I’ll just use raw sql.
Re:SQL queries don't start with SELECT by taeric on Thursday October 03, 2019 @10:29PM (#21154324)
With a lot of nested joins, both options suck. You are best abstracting. So, likely a view.
Re:SQL queries don't start with SELECT by alexvoda on Friday October 04, 2019 @03:00AM (#21155447)
> "Yes, EF queries are more readable to many developers who know C# well but don't want to get used to SQL."

My main field is databases. Been working with SQL for ~15 years. My knowledge of LINQ is much less than SQL (partly also because I was writing plenty of SQL before LINQ existed). I also think SQL is a big big mess full of leaky abstractions that forces you to do things in unituitive ways. And yet it is incredibly powerful. But don't listen to me. Read any book by C.J.Date (especially this one http://www.thethirdmanifesto.com/), for all the reasons SQL is poorly designed.

> "But the problem with that is DB apathy.....leads to some real performance problems....."

I completely agree. Databases are very powerful and more people should know how to properly use them so that the rest of us would have fewer headaches.

> " It's a DSL designed for the job, you really can't get much better"

The problem is that due to many reasons it is the only such DSL in wide spread use. Any alternatives are just footnotes.

Re:SQL queries don't start with SELECT by mcbits on Thursday October 03, 2019 @10:05PM (#21154190)
"select id, name from ..."

"from c in customers select ..."

Fill in the blank. They're not equally readable to the IDE that's trying to provide $1,199 worth of code completion for a partially typed query.

Re:SQL queries don't start with SELECT by taeric on Thursday October 03, 2019 @10:28PM (#21154317)
Strictly, to the IDE they should both be equally readable. In many cases, an IDE could fill in the from just based on the columns selected.
Re:SQL queries don't start with SELECT by gigatexal on Thursday October 03, 2019 @08:13PM (#21153641)
Yeah give me raw sql any day: it’s far more readable to me than the unnecessary abstractions we write to make things easier
Re:SQL queries don't start with SELECT by munk-a on Thursday October 03, 2019 @05:19PM (#21152346)
I do prefer this structure, but it differing for this one product is more of a cost than I'd like to pay for it. SQL is wonderful because so much of it is platform independent - nearly everything can be expressed in a common SQL manner and, in practice, I tend to avoid dialect specific SQL whenever possible - this just ups the comprehension immensely.

There are a bunch of tech that's tried to reinvent the wheel with SQL like Redis and Splunk... both of those systems are just a pain to interact with - their variants have strengths, sure, but they lose the ease of declaration and universality of SQL. I'd rather see a SQL variant emerge (if it were to) as a logically compatible declarative language that various DB vendors can add support for.

Re:SQL queries don't start with SELECT by nightski on Thursday October 03, 2019 @05:57PM (#21152668)
The thing is that LINQ's purpose is not to reinvent SQL. While EF has an adapter which converts LINQ expressions to SQL, you can use LINQ against objects and other sources for which it is incredibly useful. In fact, it's better suited to those tasks as it doesn't map 1:1 with SQL. But as someone who uses EF and LINQ on a daily basis, it is possible to learn how the mapping is performed and get the results you want consistently even in some pretty advanced scenarios.
Re:SQL queries don't start with SELECT by scarface74 on Friday October 04, 2019 @08:35AM (#21156986)
Side note, I fell in love with the Mongo LINQ driver. There is no impedance mismatch. While I never used LINQ to avoid learning sql, I would create my complex Mongo queries in LINQ first.
Re:SQL queries don't start with SELECT by jessaustin on Thursday October 03, 2019 @07:13PM (#21153259)
Would there be any conceptual barrier to a particular DB engine getting an updated parser that could handle "FROM... SELECT..." as well as "SELECT... FROM..."?
Re:SQL queries don't start with SELECT by nojvek on Thursday October 03, 2019 @08:56PM (#21153893)
There shouldn’t be. Engines don’t run sql directly. They make an AST, make an intermediate representation of instructions and then execute.

So if it results in same AST, the parser should be flexible. Someone should make PRs for mysql, slqlite and Postgres.

If those three have it, other engines will catch up real fast.

Re:SQL queries don't start with SELECT by mariushn on Tuesday October 08, 2019 @03:14AM (#21189575)
Re:SQL queries don't start with SELECT by tehlike on Thursday October 03, 2019 @07:15PM (#21153268)
Technicality, but it is not ef, it is the way linq is designed.
Re:SQL queries don't start with SELECT by balfirevic on Friday October 04, 2019 @09:07AM (#21157232)
It's an important point, LINQ is the foundation that has applications beyond and outside of Entity Framework.
Re:SQL queries don't start with SELECT by gigatexal on Thursday October 03, 2019 @08:10PM (#21153625)
I wish we wrote SQL this way actually. But yes it’s the one thing the entity framework got right.
Re:SQL queries don't start with SELECT by flamtap on Friday October 04, 2019 @09:23AM (#21157376)
Somewhat off-topic, but if you enjoy writing LINQ to SQL (like EF), I think you will really enjoy LINQPad.

https://www.linqpad.net/

It's my primary tool for querying against databases, because like you, I really prefer writing LINQ queries to SQL. It won't let you do most DB admin tasks, but that's what SSMS is for.

Re:SQL queries don't start with SELECT by yellowapple on Thursday October 03, 2019 @08:45PM (#21153842)
Elixir's Ecto makes a similar design choice, starting with the from, then joins, then filters, then a select.
Re:SQL queries don't start with SELECT by Volrath89 on Thursday October 03, 2019 @08:54PM (#21153879)
> Of course, the framework is a hairy mess for other reasons (lazy loading, untranslateable methods, ugly generated queries) but the alternative to SQL it presents is lovely.

What is wrong with lazy loading enabled by default?

Re:SQL queries don't start with SELECT by tomnipotent on Thursday October 03, 2019 @09:06PM (#21153934)
Hidden side effects. Iterating over a list of something in one layer shouldn't trigger database queries in another.

Ideally, you should know ahead of time what data you need and how to pre-load it without relying on something that could cause multiple N+1 issues based on the depth of lazy loading.

Re:SQL queries don't start with SELECT by sterileopinions on Thursday October 03, 2019 @07:16PM (#21153277)
>This is one of the things MS Entity Framework gets right - it enforces the "start with FROM"

It gets it "right" (right being a very loosely defined term here) by happenstance, because you're operating off of an object. Not because it was some higher level design choice or discussion.

Re:SQL queries don't start with SELECT by alexvoda on Friday October 04, 2019 @04:50AM (#21155932)
It gets it (this order of operations issue) right because it is actually easy to get it right. It is in fact remarkable that SQL got it wrong.