Saturday, June 30, 2012

Disadvantages of ORM

ORM has attracted a lot of attention in the last years. So let's get a bit deeper into it.

The biggest advantage of ORM is also the biggest disadvantage: queries are generated automatically
  • queries can't be optimized
  • queries select more data than needed, things get slower, more latency
    (some ORMs fetch all datasets of all relations of an object even though only 1 attribute is read)
  • compiling queries from ORM code is slow (ORM compiler written in PHP)
  • SQL is more powerful than ORM query languages
  • database abstraction forbids vendor specific optimizations

Other problems coming up with ORM
  • compiling ORM logic from phpDoc instructions or XML files is slow, but can be cached
  • ORM validates relations and field names outside the database, but can't keep relations consistent
  • ORM libraries are often used in projects without making a benchmark before
  • ORM libraries are often used because the documentation of the library says it is very fast
  • ORM libraries are often used by default without checking the project's needs
  • database abstraction is often required but changing the database never happens
  • databases are not object oriented
  • ORM violates the basic database performance principle: you get the best performance when your data is stored in the same structure it gets read

General coding problems with ORM
  • having objects instead of SQL, programmers tend to write joins directly in PHP
  • ORM code can be much longer than normal code with PHP and SQL
    (increase of complexity, error rates and maintenance efforts)
  • how to handle null values? (assign null => isset gives false)
  • people often document PHP code but not the database schemas
    (e.g. empty comments in MySQL fields and tables, docs not up-to-date)
  • new versions of ORM libraries often forbid reusing older ORM code
  • slow code is often wrapped with caching, so you always serve old data

Where can ORM be good?
  • avoid building SQL strings for simple insert, update, delete
  • using ORM with magic getters/setters in PHP
  • allow models to inherit attributes and methods from other models
  • separate models from views and controllers
  • centralize validation rules, save or delete methods to one class per entity
  • handle escaping and serialization of values automatically

Performance in numbers?
e.g. Doctrine 2, watch slide 50 and 54: Doctrine is >3 times slower than raw PHP on 20 inserts, imagine what happens with 20000 ... real numbers are much slower, see slide 47, here the authors only benchmarked flush() instead of the whole code

Coming soon: How to write a really small and fast O/R-mapper with PHP

No comments:

Post a Comment

Labels

performance (23) benchmark (6) MySQL (5) architecture (5) coding style (5) memory usage (5) HHVM (4) C++ (3) Java (3) Javascript (3) MVC (3) SQL (3) abstraction layer (3) framework (3) maintenance (3) Go (2) Golang (2) HTML5 (2) ORM (2) PDF (2) Slim (2) Symfony (2) Zend Framework (2) Zephir (2) firewall (2) log files (2) loops (2) quality (2) real-time (2) scrum (2) streaming (2) AOP (1) Apache (1) Arrays (1) C (1) DDoS (1) Deployment (1) DoS (1) Dropbox (1) HTML to PDF (1) HipHop (1) OCR (1) OOP (1) Objects (1) PDO (1) PHP extension (1) PhantomJS (1) SPL (1) SQLite (1) Server-Sent Events (1) Silex (1) Smarty (1) SplFixedArray (1) Unicode (1) V8 (1) analytics (1) annotations (1) apc (1) archiving (1) autoloading (1) awk (1) caching (1) code quality (1) column store (1) common mistakes (1) configuration (1) controller (1) decisions (1) design patterns (1) disk space (1) dynamic routing (1) file cache (1) garbage collector (1) good developer (1) html2pdf (1) internationalization (1) invoice (1) just-in-time compiler (1) kiss (1) knockd (1) legacy code (1) legacy systems (1) logtop (1) memcache (1) memcached (1) micro framework (1) ncat (1) node.js (1) openssh (1) pfff (1) php7 (1) phpng (1) procedure models (1) ramdisk (1) recursion (1) refactoring (1) references (1) regular expressions (1) search (1) security (1) sgrep (1) shm (1) sorting (1) spatch (1) ssh (1) strange behavior (1) swig (1) template engine (1) threads (1) translation (1) ubuntu (1) ufw (1) web server (1) whois (1)