>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.