Django's ORM vs. Raw SQL: When to Use Which

  • Comments posted to this topic are about the item Django's ORM vs. Raw SQL: When to Use Which

  • My opinion is a bit biased as I am a DBA as well as a DB Developer (and other roles), but I have not found an ORM that works well at scale. ALL ORM's that I've tested (I have not tested Django mind you) end up making the SQL calls in inefficient ways. I've seen them do SELECT * which is something I dislike in production level code and if the app only cares about 1 or 2 columns in a table with 100, that SELECT * is a huge waste of resources.

    On top of that, ORM's usually end up running ad-hoc queries against the database. This isn't always a bad thing, but it can (and often does) result in single use query plans ending up in the plan cache. IF you build up stored procedures for your applications, you can have a lot of plan reuse which can improve overall performance, especially with systems that are under heavy use. I have yet to find an ORM that plays nice with Stored Procedures UNLESS you basically escape out of the ORM and run the stored procedure directly.

    My opinion - ORMs are good for when you are a small shop and don't care about the technical debt and don't have a good database developer on site. They can also be good for prototyping your application or for proof of concept. But once you need to scale your app up, you may need to re-write all the code that relies on the ORM to do the heavy lifting for you.

    Just my 2 cents though. It could be that ORM's have improved since I last worked with them.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Sorry for the double post, but I just wanted to add it was a good article. I enjoyed reading it and my reply wasn't saying "don't use ORM's", it was meant to be "be careful with ORM's".

    omu - you wrote a good article!

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • I am here with Brian Gale. For me it is more a commercial than a technical post.

    I would love to see the incoming statements on the database side.

    e.g.

    # Updating a record using ORM 
    book = Book.objects.get(title='Harry Potter')
    book.published_year = 1997
    book.save()

    Will this methods first run a SELECT -> update the set locally and than run an UPDATE on the server?

    Honestly - I don't trust ORMs for multiple reasons

    • what Brian said
    • the use of cursors make me nervous 🙂
    • security (we allow stored procs only and give NO access to tables)

    Microsoft Certified Master: SQL Server 2008
    MVP - Data Platform (2013 - ...)
    my blog: http://www.sqlmaster.de (german only!)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply