logoalt Hacker News

Atotalnoobtoday at 2:36 PM1 replyview on HN

You can tell the LLM to optimize the Django ORM. With ORMs, large structural changes are sometimes needed for small query text changes due to how ORMs generate the SQL.

I’m a SWE and at my previous job it fell to me to optimize some queries that used EF Core ORM. I gave opus a local db with a small export of anonymized prod data, had it generate a billion rows with similar cardinality.

Then I told opus how to get the raw SQL and told it to write unit tests with various optimizations.

It got the query down from 5+ minutes to a few seconds. I verified the final SQL and ORM structure.

All it needed was to modify some indices and fix up the ORM to properly generate the lateral join.

However, to get the join working properly required a full restructure of the ORM query.


Replies

nijavetoday at 3:25 PM

>You can tell the LLM to optimize the Django ORM

I have and have come to the conclusion it's too risky. On too many attempts it hallucinates correctness and subtly breaks edge cases

>large structural changes are sometimes needed for small query text changes due to how ORMs generate the SQL

Sometimes that's true but a lot of times you can get 90% the benefit with significantly lower risk with a much smaller targeted changes.

It can check correctness against real data but I've still hit problems where you can just look at the rewrite and identify behavior differences that don't necessarily show up in 1 off execution comparison unless you brute force your way through tons of test data.

A lot of the cases I'm running into are somewhat older code (5+ years) with poor test coverage that few people understand or remember. Some of these probably have legitimate bugs but it's risky assuming and safer to ensure an optimization is identical.