Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of ORM?

Tags:

oop

database

orm

I ever developed several projects based on python framework Django. And it greatly improved my production. But when the project was released and there are more and more visitors the db becomes the bottleneck of the performance.

I try to address the issue, and find that it's ORM(django) to make it become so slow. Why? Because Django have to serve a uniform interface for the programmer no matter what db backend you are using. So it definitely sacrifice some db's performance(make one raw sql to several sqls and never use the db-specific operation).

I'm wondering the ORM is definitely useful and it can:

  1. Offer a uniform OO interface for the progarammers
  2. Make the db backend migration much easier (from mysql to sql server or others)
  3. Improve the robust of the code(using ORM means less code, and less code means less error)

But if I don't have the requirement of migration, What's the meaning of the ORM to me?

ps. Recently my friend told me that what he is doing now is just rewriting the ORM code to the raw sql to get a better performance. what a pity!

So what's the real meaning of ORM except what I mentioned above? (Please correct me if I made a mistake. Thanks.)

like image 310
Zhu Tao Avatar asked Jul 05 '09 09:07

Zhu Tao


2 Answers

You have mostly answered your own question when you listed the benefits of an ORM. There are definitely some optimisation issues that you will encounter but the abstraction of the database interface probably over-rides these downsides.

You mention that the ORM sometimes uses many sql statements where it could use only one. You may want to look at "eager loading", if this is supported by your ORM. This tells the ORM to fetch the data from related models at the same time as it fetches data from another model. This should result in more performant sql.

I would suggest that you stick with your ORM and optimise the parts that need it, but, explore any methods within the ORM that allow you to increase performance before reverting to writing SQL to do the access.

like image 96
Steve Weet Avatar answered Nov 11 '22 05:11

Steve Weet


A good ORM allows you to tune the data access if you discover that certain queries are a bottleneck.

But the fact that you might need to do this does not in any way remove the value of the ORM approach, because it rapidly gets you to the point where you can discover where the bottlenecks are. It is rarely the case that every line of code needs the same amount of careful hand-optimisation. Most of it won't. Only a few hotspots require attention.

If you write all the SQL by hand, you are "micro optimising" across the whole product, including the parts that don't need it. So you're mostly wasting effort.

like image 45
Daniel Earwicker Avatar answered Nov 11 '22 05:11

Daniel Earwicker