If you have a slow query in a web application, then rewriting it as a raw query is probably the very last thing you should consider doing, after: Where you have logic that can't be expressed using the ORM, it's generally easy to drop down to writing raw SQL for that part. It helps make your application portable between different databases (eg so you can test using SQLite but use MySQL in production).It does a lot of the work of sanitizing data for you.It helps avoid the mental context switch between PHP and SQL.It's generally easier to express relations between tables.Using an ORM also comes with significant benefits to developers: Most ORM's have tools to handle things like the N+1 problem - for instance, Eloquent has the with() method to eager-load related tables, which is generally a lot more convenient than explicitly writing a query to do the eager-loading for you. You can get a far, far bigger improvement in performance by caching the responses than by rewriting ORM queries in raw SQL.Īn ORM does make certain types of slow inefficient queries more likely, but that's something that can be resolved by using a profiler like Clockwork to identify the slow or unnecessary queries and refactoring them away. However, this is nearly always a non-issue.įor example, while there might be some slight performance increase from writing raw SQL, it's generally dwarfed by the cost of making the query in the first place. There will always be some overhead from using any ORM.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |