views:

199

answers:

6

The reason why I ask this is because I need to know whether not using ORM for a social networking site makes any sense at all.

My argument why ORM does not fit into social networking sites are:

  • Social networking sites are not a product, thus you don't need to support multiple database. You know what database to use, and you most likely won't change it every now and then.
  • Social networking sites requires many-to-many relationship between users, and in the end sometimes you will need to write plain SQL to get those relations. The value of ORM is thus decreased again.
  • Related to the previous point, ORM sometimes do multiple queries in the backend to fetch its record, which sometimes may be inefficient and may cause bottleneck in the database. In the end you have to write down plain SQL query. If we know we are going to write plain SQL anyway, what is the point using ORM?

This is my limited understanding based on my limited experience. What are you're experience with building a social networking sites? Are my points valid? Is it lame to use bare SQL without worrying about using ORM? What are the points where ORM may help in building a social networking sites?

+3  A: 

Here's my response to your points:

  • ORM does not need multiple database to be effective, in fact most cases of ORM usage are not due to the ability to adapt to different databases.
  • Most modern ORM frameworks are flexible enough to fetch 'lightweight' variants of mapped classes, it really depends on how you implement them.
  • If really required to, you can write native SQL queries within the ORM frameworks. Do note that caching and performance related algorithms are often part of the these frameworks.
o.k.w
Do we really use caching that is provided by the ORM? In the end I've seen people use the caching that is provided by the Database instead or memcached.
jpartogi
I agree cache can be a double-edge sword, you'll need to understand it in order to use it properly/effectively. Here's an interesting article on it http://queue.acm.org/detail.cfm?id=1394141
o.k.w
+1  A: 

IMO, an ORM helps you write cleaner, clearer code. If you use it sloppily you can cause excessive queries, but that isn't a rule by any means. If I were you I would start using the ORM and best practices of a framework, and only drop to SQL if you find yourself needing functionality that the ORM does not provide.

Ben
A: 

Also note that in web applications, many people are moving away from SQL databases. An ORM might help you to migrate to a non-relational database (precisely because you do not have SQL in your application code). Look at the use of JDO and JPA in Google's App Engine.

Thilo
A: 

IMHO. ORM is need.

  1. It allow you to access database in OOP way, no matter multiple database or not.

    1. Cleaner code, you can define all method related to a particular table in the table class file, if you need raw sql join query, no problem, define there. it follows DRY and KISS. It is much better than you write similar raw sql query again and again.
Tommy
+12  A: 

The value of using an ORM is to help speed up development, by automating the tedious work of assigning query results to object fields, and tracking changes to object fields so you can save them to the database. Hence the term Object-Relational Mapping.

An ORM has little value for you regarding database portability, since you only use the one database you deploy on.

The runtime performance aspect of an ORM is no better than, and typically much worse than writing plain SQL yourself. The generic methods of query generation often make naive mistakes and result in redundant queries, as you have mentioned. Again, the benefit is in development time, not runtime efficiency.

Using an ORM versus not using an ORM doesn't seem to make a huge difference for scalability. Other techniques with more bang-for-the-buck for scalability include:

  • Managing indexes in the RDBMS. Improve as many algorithms as possible from O(n) to O(logn).
  • Intelligent caching architecture.
  • Horizontal scaling by database partitioning/sharding.
  • Database load-balancing and replication. Read from slave databases where possible, and write to a single master database. Index slaves and masters differently.
  • Supplement the RDBMS with complementary technology, such as Sphinx Search.
  • Vertical scaling by throwing hardware at the problem. Jeff Atwood has commented about this on the StackOverflow podcast.

Some people advocate moving your data management to a distributed architecture using cloud computing or distributed non-relational databases. This is probably not necessary until you get a very large number of users. Once you grow to a certain level of magnitude, all the rules change and you probably can't use an RDBMS anyway. But unless you are the data architect at Yahoo or Facebook or LinkedIn, don't worry about it -- cloud computing is over-hyped.

There's a common wisdom that the database is always the bottleneck in web apps, but there's also a case that improving efficiency on the front-end is at least as important. Cf. books by Steve Souders.


Julia Lerman in Programming Entity Framework (2009), p.503 shows that there's a 220% increase in query execution cost between using a DataReader directly and using Microsoft’s LINQ to Entities.

Also see Jeff Atwood's post on All Abstractions are Failed Abstractions, where he shows that using LINQ is at least double the cost of using plain SQL even in a naive way.

Bill Karwin
*"(...) typically much worse than writing plain SQL yourself"* I do not really agree, especially on the "much" (and a good ORM actually generates much better SQL than most developers but let's say this doesn't apply here).
Pascal Thivent
@Pascal: There are, no doubt, anecdotal cases for efficient ORMs as well as wasteful ORMs. My point remains that the chief benefit of an ORM is in development efficiency, not runtime efficiency.
Bill Karwin
@Bill I still think that you get both development speed without sacrificing performances with a *good* ORM. Actually, I believe that things like lazy loading, 1st level cache, 2nd level cache, query cache do help. This doesn't mean that you don't have to do some tuning. And, again, a good ORM will allow you to do it. I get your point though. Thanks for the feedback (and the nice answer) anyway.
Pascal Thivent
@Pascal: Can you give an example of a *good* ORM that meets these criteria? I'm not trying to be argumentative -- I'm sincerely curious.
Bill Karwin
@Bill Hibernate. And maybe EclipseLink (ex Oracle Toplink) but I'm not 100% sure about its caching capabilities (although I know that you could use Oracle Toplink + Oracle Coherence).
Pascal Thivent
@Pascal, thanks I will keep those in mind.
Bill Karwin
A: 

The odds of your site being big enough that scaling becomes an issue are quite small so why prematurely optimize by doing everything in raw SQL instead of an ORM? You can get fairly far by throwing better hardware at a database assuming the database and application design are decent. While you may need to write raw SQL for things like creating friend graphs what about all the little things like updating the database when someone changes there email, sends a private message, uploads a photo, etc? Using an ORM can simplify all the simple database tasks you will have to do while still allowing you to hand code where absolutely necessary.

Jared