15.01.2013 Views

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 - Object Relational Mappers<br />

Stored procedures are faster<br />

This is the excuse I held onto the longest. Write a reasonable/common SQL statement inline and then<br />

write the same thing in a stored procedure and time them. Go ahead. In most cases there’s little or no<br />

difference. In some cases, stored procedures will be slower because a cached execution plan will not be<br />

efficient given a certain parameter. Jeff Atwood called using stored procedures for the sake <strong>of</strong> better<br />

performance a fairly extreme case <strong>of</strong> premature optimization. He’s right. The proper approach is to take<br />

the simplest possible approach (let a tool generate your SQL for you), and optimize specific queries<br />

when/if bottlenecks are identified.<br />

It took a while, but after a couple years, I realized that the debate between inline and stored procedures<br />

was as trivial as the one about C# and VB.NET. If it was just a matter <strong>of</strong> one or the other, then pick<br />

whichever you prefer and move on to your next challenge. If there was nothing more to say on the<br />

topic, I'd pick stored procedures. However, when you add an O/R mapper into the mix, you suddenly<br />

gain significant advantages. You stop participating in stupid flame wars, and simply say "I want that!".<br />

Specifically, there are three major benefits to be had with O/R mappers:<br />

1. You end up writing a lot less code – which obviously results in a more maintainable system,<br />

2. You gain a true level <strong>of</strong> abstraction from the underlying data source – both because you’re<br />

querying the O/R mapper for your data directly (and it converts that into the appropriate SQL),<br />

and because you’re providing mapping information between your table schemas and domain<br />

objects,<br />

3. Your code becomes simpler- if your impedance mismatch is low, you'll write far less repetitive<br />

code. If your impedance mismatch is high you won't have to compromise your database design<br />

and your domain design - you can build them both in an optimized way, and let the O/R mapper<br />

manage the mismatch.<br />

In the end, this really comes down to building the simplest solution upfront. Optimizations ought to be<br />

left until after you've pr<strong>of</strong>iled your code and identified actual bottlenecks. Like most things, it might not<br />

sound that simple because <strong>of</strong> the fairly complex learning to do upfront, but that’s the reality <strong>of</strong> our<br />

pr<strong>of</strong>ession.<br />

<strong>Foundations</strong> <strong>of</strong> <strong>Programming</strong> Copyright © <strong>Karl</strong> <strong>Seguin</strong> www.codebetter.com<br />

45

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!