30.06.2013 Views

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11: <strong>SQL</strong> Refactoring<br />

Code refactoring is the process of improving existing code in some way, while leaving its<br />

input/output behavior unaltered. The most common reason to undertake a refactoring<br />

effort is in response to performance issues, but refactoring could also be used to improve<br />

code security, error handling, maintainability (e.g. decrease coupling), and so on.<br />

The emphasis on leaving the program behavior unchanged is crucial; our refactoring<br />

must not add or remove functionality. Code refactoring may drastically alter the internal<br />

logic of a database object, and we may add new objects (columns, tables, views, indexes,<br />

and so on) to support our refactoring efforts, but the black-box behavior of the code, as<br />

a whole, must remain the same, and we must verify that this is the case through careful<br />

black-box testing.<br />

This chapter will focus on refactoring <strong>SQL</strong> code in standard <strong>SQL</strong> objects such as stored<br />

procedures and functions, rather than on full-scale schema refactoring (splitting tables,<br />

and so on). The reason for this is simply that, in my experience, most opportunities in the<br />

real world for database refactoring take this form. Opportunities for full-scale refactoring<br />

of the base tables are rarer; too many dependencies. However, it's worth bearing in mind<br />

before we start that, if the database design is fundamentally flawed, there may only be so<br />

much you can achieve by refactoring the <strong>SQL</strong> application code.<br />

Why Refactor <strong>SQL</strong>?<br />

As noted in the introduction, probably the most common reason to refactor code is to<br />

improve poor performance. Sometimes, an application is inadequately tested during<br />

development and then simply fails to meet performance requirements when deployed to<br />

production. At other times, a successful legacy application is now handling data volumes<br />

and user numbers that were never envisaged when it was designed, so it has begun to<br />

310

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

Saved successfully!

Ooh no, something went wrong!