News for hackers. Stuff that matters.
Re:It's not Ruby that's slow, it's your database by caladin on Tuesday November 08, 2022 @08:01PM (#33526336)
I agree but I think Hibernate is an extreme. There is a middleground, query builders, which enable dynamic query construction (e.g. dynamically appending filter predicates) with reduced cognitive load of something like Hibernate.

I personally was heavily in the camp of "write raw queries ideally with code generation for statically typed/generated code" (as exist in Rust, Go, TypeScript, etc.), but I have since tempered my position since it does become a bit brittle and repetitive. Lately I've been playing with Jooq and it seems great.

There are tradeoffs everywhere though, so with Jooq you still aren't 1-to-1 with raw SQL, there is a bit to learn, but I consider it a worthwhile investment (and a minor one relative to an actual ORM).

Re:It's not Ruby that's slow, it's your database by int_19h on Tuesday November 08, 2022 @08:26PM (#33526571)
A pure query builder is just writing SQL on syntax tree level, more or less. This makes sense for the same reason why you want your macros to operate on ASTs and not raw text. But I would argue that it's still much closer to plain text SQL in the code than to any ORM.
Re:It's not Ruby that's slow, it's your database by rahimnathwani on Tuesday November 08, 2022 @08:29PM (#33526593)
Right, but it lives with your application code and has the same syntax as the application code. That's probably preferable to SQL stored procedures (which often live outside source control).
Re:It's not Ruby that's slow, it's your database by nicoburns on Tuesday November 08, 2022 @09:42PM (#33526982)
I’ve found that all this does is make the query less readable. SQL is purpose made for writing queries, and avoids unnecessary syntax noise you get when trying to fit the query into a host language based dsl.
Re:It's not Ruby that's slow, it's your database by int_19h on Tuesday November 08, 2022 @11:43PM (#33527595)
That really depends on the language - specifically, on whether it already has constructs that can map nicely (e.g. LINQ in C#), or macros to define them, or syntax that is generally amenable to DSLs even without macros in the picture (e.g. Lisps).

SQL itself is also not a particularly well-designed query language. E.g. the order of the query doesn't reflect the natural data flow (SELECT .. FROM .. is reversed - compare to XQuery's FLWOR, for example), there are warts like WHERE vs HAVING etc. A good DSL can do much better.

Re:It's not Ruby that's slow, it's your database by lukaseder on Wednesday November 09, 2022 @03:43AM (#33528855)
SQL is powerful. A DSL that "fixes" things in this area getting all the other language feature interactions right isn't trivial, all the while users have to learn yet another language. Take PRQL for example: https://prql-lang.org. It looks nice, but the examples are very basic. What about window functions, grouping sets, lateral, DML, recursive SQL, pattern matching, pivot/unpivot etc. Might be doable, but perhaps, they've already made a decision that won't enable one of those features without adding new kludges.

Besides, every single "fix" will be a proprietary solution, while SQL is an ISO/IEC standard that's here to stay and universally adopted.

> A good DSL can do much better.

Stonebraker's QUEL was "better", before SQL, and yet, where is QUEL today?

Re:It's not Ruby that's slow, it's your database by lmm on Wednesday November 09, 2022 @06:59AM (#33530295)
> Besides, every single "fix" will be a proprietary solution, while SQL is an ISO/IEC standard that's here to stay and universally adopted.

And yet in practice the fixes end up more portable. How many of the things on your list of non-basic SQL have consistent syntax across databases, yet alone consistent behaviour?

Re:It's not Ruby that's slow, it's your database by lukaseder on Wednesday November 09, 2022 @04:24PM (#33538052)
All of them
Re:It's not Ruby that's slow, it's your database by int_19h on Wednesday November 09, 2022 @07:11PM (#33540209)
A good DSL is not easy to implement, of course.

But the point here isn't just that it can be more regular than SQL. Integrating with the syntax of the host language is also a considerable advantage, ideally with static type checking.

Re:It's not Ruby that's slow, it's your database by maximilianroos on Friday November 18, 2022 @03:10PM (#33661318)
[PRQL core-dev here]

Thanks for the PRQL shout-out!

> Take PRQL for example: https://prql-lang.org. It looks nice, but the examples are very basic. What about window functions, grouping sets, lateral, DML, recursive SQL, pattern matching, pivot/unpivot etc.

Window functions are very much supported! Check out the examples on the home page & in the docs.

The others aren't yet, but not because of a policy — we've started with the most frequently used features and adding features as they're needed.

Re:It's not Ruby that's slow, it's your database by Tainnor on Wednesday November 09, 2022 @04:41AM (#33529244)
In a statically typed language, what you get from a good query builder is that "malformed SQL statements" blow up at compile-time instead of at run-time.
Re:It's not Ruby that's slow, it's your database by nicoburns on Wednesday November 09, 2022 @08:09AM (#33531031)
Some languages also provide this for SQL strings (e.g. the sqlx library in Rust) will compile-time check raw SQL strings.
Re:It's not Ruby that's slow, it's your database by JohnBooty on Tuesday November 08, 2022 @11:22PM (#33527496)

    That's probably preferable to SQL stored 
    procedures (which often live outside source control).
Stored procs definitely have some big pros and big cons, but I don't think this is one of them -- any ORM with a decent set of tools to manage migrations (ActiveRecord is one) makes this objection a non issue IMO.
Re:It's not Ruby that's slow, it's your database by hamandcheese on Wednesday November 09, 2022 @01:12AM (#33528037)
I explicitly do not want to manage stored procedures in the same way as typical migrations - if I did, I would wind up with many, many versions of the procedure in my code base as it evolved over time. This would make grepping or locating the latest version pretty annoying.

Flyway (migration tool in Java) has a notion of “repeatable” migrations, though, which would do the trick.

Re:It's not Ruby that's slow, it's your database by JohnBooty on Wednesday November 09, 2022 @01:35AM (#33528156)

    This would make grepping or locating the latest version pretty annoying
Wouldn't this be an issue with any database object managed via migrations? Do any of them make this easy for any database object?

In ActiveRecord, you have your migrations folder(s) and then you have your `structure.sql` (essentially the raw output of mysqldump or pgdump) or the equivalent.

If I need to see the literal database definition of any database object I look it up in there. Not the slickest solution but works well enough - really just a few keystrokes in my editor.

I'd be curious how other migration tools handle (or fail to handle) this.

Re:It's not Ruby that's slow, it's your database by hamandcheese on Wednesday November 09, 2022 @01:18PM (#33535686)
> Wouldn't this be an issue with any database object managed via migrations?

Like I mentioned, check out flyway repeatable migrations.

Re:It's not Ruby that's slow, it's your database by gregw2 on Wednesday November 09, 2022 @10:16AM (#33532757)
It’s not hard to get SQL DDL and stored price in source control with Liquibase or Flyway. I’ve even done TDD sproc unit (integration) tests in them. But I’m a webapp turned data engineer guy…
Re:It's not Ruby that's slow, it's your database by lukaseder on Wednesday November 09, 2022 @03:35AM (#33528810)
> There are tradeoffs everywhere though, so with Jooq you still aren't 1-to-1 with raw SQL

You're probably hinting at writing derived tables / CTEs? jOOQ will never keep you from writing views and table valued functions, though. It encourages you do so! Those objects play very well with code generation, and you can keep jOOQ for the dynamic parts, views/functions for the static parts.