tags:

views:

257

answers:

8

ORM seems to be a fast-growing model, with both pros and cons in their side. From Ultra-Fast ASP.NET of Richard Kiessig (http://www.amazon.com/Ultra-Fast-ASP-NET-Build-Ultra-Scalable-Server/dp/1430223839/ref=pd_bxgy_b_text_b):

"I love them because they allow me to develop small, proof-of-concept sites extremely quickly. I can side step much of the SQL and related complexity that I would otherwise need and focus on the objects, business logic and presentation. However, at the same time, I also don't care for them because, unfortunately, their performance and scalability is usually very poor, even when they're integrated with a comprehensive caching system (the reason for that becomes clear when you realize that when properly configured, SQL Server itself is really just a big data cache"

My questions are:

  • What is your comment about Richard's idea. Do you agree with him or not? If not, please tell why.

  • What is the best suitable fields for ORM and traditional database query? in other words, where you should use ORM and where you should use traditional database query :), which kind/size... of applications you should undoubtedly choose ORM/traditional database query

Thanks in advance

+6  A: 

ORM is pretty old, at least in the Java world. Major problems with ORM: - Object-Oriented model and Relational model are quite different. - SQL is a high level language to access data, but any OO language like C#, Java or Visual Basic.Net For more information search the web on things like 'Object-relational impedance mismatch'

Either case, a good ORM framework saves you on quite some boiler-plate code. But you still need to have knowlegde of SQL, how to setup a good SQL databasemodel. Start with creating a good databasemodel using SQL, then base your OO model on that (not the other way around)

However, the above only holds if you really need to use a SQL database. I recommend looking into NoSQL movement as well. There's stuff like Cassandra, Couch-db. While google'ing for .net solutions I found this stackoverflow question: http://stackoverflow.com/questions/1777103/what-nosql-solutions-are-out-there-for-net

Gerbrand
I said "growing fast" from .NET perspective: Entity Framework, NHibernate,...Glad to know that ORM is that old :)
Vimvq1987
You might want to check out LinQ . Also pretty old, and many people in the Java world consider it superior to existing ORM solutions in Java.
Gerbrand
LINQ is about 4 years old, it's still quite young. But I think only LINQ to SQL is considered as an ORM :-?
Vimvq1987
Old in software-years :-).LINQ is an ORM by most definitions as far as I know. Main difference between ORM like Hibernate is that the query language of LINQ integrates with the host language (like C#). When using Hibernate or similar Java ORM you have to type queries between quotes.
Gerbrand
I only partially agree. for instance, most people using NH build the OO model first, and create the appropriate db model by writing the mapping files (and using a generated db model, which saves you huge amount of time).
Stefan Steinegger
@Gerbrand: LINQ is a language feature, not a ORM :)
Vimvq1987
A: 

Programming is about writing software for business use. The more we can focus on business logic and presentation and less with technicalities that only matter at certain points in time (when software goes down, when software needs upgrading, etc), the better.

Recently I read about talks of scalability from a Reddit founder, from here, and one line of him that caught my attention was this:

"Having to deal with the complexities of relational databases (relations, joins, constraints) is a thing of the past."

From what I have watched, maintaining a complex database schema, when it comes to scalability, becomes a major pain as the site grows (you add a field, you reassign constraints, re-map foreign keys...etc). It was not entirely clear to me as to why is that. They're not using a NOSQL database though, they're in Postgres.

Add to that, here comes ORM, another layer of abstraction. It simplifies code writing, but almost often at a performance penalty. For me, a simple database abstraction library will do, much like lightweight AR libs out there together with database-specific "plain text" queries. I can't show you any benchmark but with the ORMs I have seen, most of them say that "ORM can often be slow".

Richard covers both sides of the coin, so I agree with him.

As for the fields, I really don't quite get the context of the "fields" you are asking about.

Ygam
in other words, where you should use ORM and where you should use traditional database query :), which kind/size... of applications you should undoubtedly choose ORM/traditional database query
Vimvq1987
might as well edit the question to ask that rather than "what fields...". 'fields' as used in this question is a little ambiguous :)
Ygam
edited, thanks :)
Vimvq1987
+13  A: 

I can't agree to the common complain about ORMs that they perform bad. I've seen many plain-SQL applications until now. While it is theoretically possible to write optimized SQL, in reality, they ruin all the performance gain by writing not optimized business logic.

When using plain SQL, the business logic gets highly coupled to the db model and database operations and optimizations are up to the business logic. Because there is no oo model, you can't pass around whole object structures. I've seen many applications which pass around primary keys and retrieve the data from the database on each layer again and again. I've seen applications which access the database in loops. And so on. The problem is: because the business logic is already hardly maintainable, there is no space for any more optimizations. Often when you try to reuse at least some of your code, you accept that it is not optimized for each case. The performance gets bad by design.

An ORM usually doesn't require the business logic to care too much about data access. Some optimizations are implemented in the ORM. There are caches and the ability for batches. This automatic (and runtime-dynamic) optimizations are not perfect, but they decouple the business logic from it. For instance, if a piece of data is conditionally used, it loads it using lazy loading on request (exactly once). You don't need anything to do to make this happen.

On the other hand, ORM's have a steep learning curve. I wouldn't use an ORM for trivial applications, unless the ORM is already in use by the same team.

Another disadvantage of the ORM is (actually not of the ORM itself but of the fact that you'll work with a relational database an and object model), that the team needs to be strong in both worlds, the relational as well as the oo.

Conclusion:

  • ORMs are powerful for business-logic centric applications with data structures that are complex enough that having an OO model will advantageous.
  • ORMs have usually a (somehow) steep learning curve. For small applications, it could get too expensive.
  • Applications based on simple data structures, having not much logic to manage it, are most probably easier and straight forward to be written in plain sql.
  • Teams with a high level of database knowledge and not much experience in oo technologies will most probably be more efficient by using plain sql. (Of course, depending on the applications they write it could be recommendable for the team to switch the focus)
  • Teams with a high level of oo knowledge and only basic database experience are most probably more efficient by using an ORM. (same here, depending on the applications they write it could be recommendable for the team to switch the focus)
Stefan Steinegger
Sorry, I was ill when the bounty ends, so I couldn't accept this answer :(
Vimvq1987
@Vimvq1987: no problem. I don't care about the bounty.
Stefan Steinegger
A: 

I agree with most points already made here.

ORM's are not new in .NET, LLBLGen has been around for a long time, I've been using them for >5 years now in .NET.

I've seen very bad performing code written without ORMs (in-efficient SQL queries, bad indexes, nested database calls - ouch!) and bad code written with ORMs - I'm sure I've contributed to some of the bad code too :)

What I would add is that an ORM is generally a powerful and productivity-enhancing tool that allows you to stop worrying about plumbing db code for most of your application and concentrate on the application itself. When you start trying to write complex code (for example reporting pages or complex UI's) you need to understand what is happening underneath the hood - ignorance can be very costly. But, used properly, they are immensely powerful, and IMO won't have a detrimental effect on your apps performance. I for one wouldn't be happy on a project that didn't use an ORM.

Matt Roberts
A: 

As others have said, you can write underperforming ORM code, and you can also write underperforming SQL.

Using ORM doesn't excuse you from knowing your SQL, and understanding how a query fits together. If you can optimize a SQL query, you can usually optimize an ORM query. For example, hibernate's criteria and HQL queries let you control which associations are joined to improve performance and avoid additional select statements. Knowing how to create an index to improve your most common query can make or break your application performance.

What ORM buys you is uniform, maintainable database access. They provide an extra layer of verification to ensure that your OO code matches up as closely as possible with your database access, and prevent you from making certain classes of stupid mistake, like writing code that's vulnerable to SQL injection. Of course, you can parameterize your own queries, but ORM buys you that advantage without having to think about it.

RMorrisey
+1  A: 

This site uses Linq-to-SQL I believe, and it's 'fairly' high traffic... I think that the time you save from writing the boiler plate code to access/insert/update simple items is invaluable, but there is always the option to drop down to calling a SPROC if you have something more complex, where you know you can write some screaming fast SQL directly.

I don't think that these things have to be mutually exclusive - use the advantages of both, and if there are sections of your application that start to slow down, then you can optimise as you need to.

Paddy
+1  A: 

Never got anything but pain and frustration from ORM packages. If I'd write my SQL the way they autogen it - yeah I'd claim to be fast while my code would be slow :-) Have you ever seen SQL generated by an ORM ? Barely has PK-s, uses FK-s only for misguided interpretation of "inheritance" and if it wants to do paging it dumps the whole recordset on you and then discards 90% of it :-))) Then it locks everything in sight since it has to take in a load of records like it went back to 50 yr old IBM's batch processing.

For a while I thought that the biggest problem with ORM was splintering (not going to have a standard in 50 yrs - every year different API, pardon "model" :-) and ideologizing (everyone selling you a big philosophy - always better than everyone else's of course :-) Then I realized that it was really the total amateurism that's the root cause of the mess and everything else is just the consequence.

Then it all started to make sense. ORM was never meant to be performant or reliable - that wasn't even on the list :-) It was academic, "conceptual" toy from the day one, the consolation prize for professors pissed off that all their "relational" research papers in Prolog went down the drain when IBM and Oracle started selling that terrible SQL thing and making a buck :-)

The closest I came to trusting one was LINQ but only because it's possible and quite easy to kick out all "tracking" and use is just as deserialization layer for normal SQL code. Then I read how the object that's managing connection can develop spontaneous failures that sounded like premature GC while it still had some dangling stuff around. No way I was going to risk my neck with it after that - nope, not my head :-)

So, let me make a list:

  • Totally sloppy code - not going to suffer bugs and poor perf
    • Not going to take deadlocks from ORM's 10-100 times longer "transactions"
  • Drastic reduction of capabilities - SQL has huge expressive power these days
  • Tying you up into fringe and sloppy API (every ORM aims to hijack your codebase)
    • SQL queries are highly portable and SQL knowledge is totally portable
  • I still have to know SQL just to clean up ORM's mess anyway
  • For "proof-of-concept" I can just serialize to binary or XML files
    • not much slower, zero bug libraries and one XPath can select better anyway
    • I've actually done heavy traffic web sites all from XML files
    • if I actually need real graph then I have no use for DB - nothing real to query
    • I can serialize a blob and dump into SQL in like 3 lines of code
  • If someone claims that he does it all from DB to UI - keep your codebase locked :-)
    • and backup your payroll DB - you'll thank me latter :-)))
  • NoSQL bases are more honest than ORM - "we specialize in persistence"
    • and have better code quality - not surprised at all

That would be the short list :-) BTW, modern SQL engines these days do trees and spatial indexing, not to mention paging without a single record wasted. ORM-s are actually "solving" problems of 10yrs ago and promoting amateurism. To that extent NoSQL, also known as document

ZXX
It looks like you never used a **good** ORM which makes your answer pretty useless (*"hey, I used crap and it was crap!"*). There **are** good ORMs (and some are there for nearly 20 years, e.g. TopLink), you just don't know them.
Pascal Thivent
You'd need to substantiate such statemnt with somehting. Say for a start by showing that I can also use it with C# and C++, that it is accepted as a standard so that I can transfer it as a skill from job to job. Then say a definition of a new multi-column index, a way to do paging with complex conditions, without writing SQL query. Then we can talk perf and how would JDBC let me just run a sproc, convert my data in a few lines and be done, and how I can switch between JDBC, ODBC and .NET SqlCLient in a few days without breaking a sweat. That would be just for start :-)
ZXX
I really wonder which ORM you tried.
Stefan Steinegger
A: 

ORM is far older than both Java and .NET. The first one I knew about was TopLink for Smalltalk. It's an idea as old as persistent objects.

Every "CRUD on the web" framework like Ruby on Rails, Grails, Django, etc. uses ORM for persistence because they all presume that you are starting with a clean sheet object model: no legacy schema to bother with. You start with the objects to model your problem and generate the persistence from it.

It often works the other way with legacy systems: the schema is long-lived, and you may or may not have objects.

It's astonishing how quickly you can get a prototype up and running with "CRUD on the web" frameworks, but I don't see them being used to develop enterprise apps in large corporations. Maybe that's a Fortune 500 prejudice.

Database admins that I know tell me they don't like the SQL that ORMs generate because it's often inefficient. They all wish for a way to hand-tune it.

duffymo
If you are happy with performance of Ruby and ORM you can switch to just plain persistence with files - it's like someone gives you lock-free hash-table and you put locks on all it's methods and start using it as a list :-)) I've seen people doing it - saves them for learning multi-threading and how to use more than one data structure. ORM is doing analogous thing - trading performance to serve ignorance.
ZXX