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

Comments

Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Conflict Serializability in database