Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>@tereško if tis possible, can you give the pros and cons of orm with respect to pure sql according to your experience and also i will google the topic at same time. – <a href="https://stackoverflow.com/users/375510/jaison-justus">Jaison Justus</a> </p> </blockquote> <p>Well .. explaining this in 600 characters would be hard.</p> <p>One thing I must clarify: this is about <strong>ORMs in PHP</strong>, though i am pretty sure it applies to some Ruby ORMs too and maybe others.</p> <p>In brief, you should avoid them, but if you <em>have to</em> use an ORM, then you will be better of with Doctrine 2.x , it's the lesser evil. (Implements something similar to <a href="http://martinfowler.com/eaaCatalog/dataMapper.html" rel="nofollow noreferrer">DataMapper</a> instead of ActiveRecord).</p> <h2>Case against ORMs</h2> <p>The main reason why some developers like to use ORMs is also the worst thing about them: <strong>it is easy to do simple thing in ORM</strong>, with very minor performance costs. This is perfectly fine.</p> <p><strong>1. Exponential complexity</strong></p> <p>The problem originates in people to same tool for everything. <em>If all you have is a hammer (..)</em> type of issue. This results in creating a <a href="http://martinfowler.com/bliki/TechnicalDebt.html" rel="nofollow noreferrer">technical debt</a>.</p> <p>At first it is easy to write new DB related code. And maybe, because you have a large project, management in first weeks <em>(because later it would case additional issues - read <a href="http://en.wikipedia.org/wiki/The_Mythical_Man-Month" rel="nofollow noreferrer">The Mythical Man-Month</a>, if interested in details)</em> decides to hire more people. And you end up preferring people with ORM skills over general SQL.</p> <p>But, as project progresses, you will begin to use ORM for solving increasingly complex problems. You will start to hack around some limitations and eventually you may end up with problems which just cannot be solved even with all the ORM hacks you know ... and now you do not have the SQL experts, because you did not hire them.</p> <p>Additionally most of popular ORMs are implementing <a href="http://martinfowler.com/eaaCatalog/activeRecord.html" rel="nofollow noreferrer">ActiveRecord</a>, which means that your application's business logic is directly coupled to ORM. And adding new features will take more and more time because of that coupling. And for the same reason, it is extremely hard to write good unit-tests for them.</p> <p><strong>2. Performance</strong></p> <p>I already mentioned that even simple uses of ORM (working with single table, no <code>JOIN</code>) have some performance costs. It is due to the fact that they use wildcard <code>*</code> for selecting data. When you need just the list of article IDs and titles, there is no point on fetching the content.</p> <p>ORMs are really bad at working with multiple tables, when you need data based on multiple conditions. Consider the problem:</p> <blockquote> <p>Database contains 4 tables: <code>Projects</code>, <code>Presentations</code>, <code>Slides</code> and <code>Bulletpoints</code>. </p> <ul> <li>Projects have many Presentations</li> <li>Presentations have many Slides</li> <li>Slides have many Bulletpoitns</li> </ul> <p>And you need to find content from all the <code>Bulletpoints</code> in the <code>Slides</code> tagged as "important" from 4 latest <code>Presentations</code> related to the <code>Projects</code> with ids 2, 4 and 8.</p> </blockquote> <p>This is a simple JOIN to write in pure SQL, but in any ORM implementation, that i have seen, this will result in 3-level nested loop, with queries at every level.</p> <hr> <p><strong>P.S.</strong> there are other reasons and side-effects, but they are relatively minor .. cannot remember any other important issues right now.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload