20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

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.

The point of all the discussion of cache is simply that you can tune your application to your heart’s content,<br />

but if the hardware infrastructure doesn’t support the application, all of your work yields minuscule<br />

improvements in the application speed and could cost a lot of money to get that minuscule<br />

improvement. It is critical to look at the big picture and understand exactly where the bottlenecks lie.<br />

Attack the bottlenecks in order of severity, quitting when the application runs at an acceptable speed.<br />

Okay, so you have determined that application bottlenecks exist in the application itself, and your hardware<br />

is sufficient. What next?<br />

Tuning <strong>SQL</strong><br />

There is an axiom in programming that says that 90 percent of the time is spent in 10 percent of the code.<br />

If you want to make your program run faster, you only need to optimize that 10 percent of the code. <strong>SQL</strong><br />

is programming and thus can be viewed in the same manner, and it is very useful to do so. Very few<br />

databases have only a few queries. Databases often service entire applications, and those applications<br />

have pieces and parts that query the database. Often the applications have entire subprograms, each<br />

with their own sets of queries. You need to do a little investigative work and discover which specific<br />

piece is causing the slowdown.<br />

You might be saying, “Yes, but that doesn’t matter, just show me how to optimize a query,” but that is<br />

very much putting the cart before the horse. Optimizing a query involves a set of techniques and uses a<br />

set of tools, which you get to shortly, but before you do, it is important to understand that you should<br />

not just take a shotgun approach to the subject of optimization. In the end, the intent is usually to make a<br />

process run faster, which in the end may mean making a query run faster, but which query? Since a process<br />

may be made up of dozens of subqueries, simply starting at the top and attempting to optimize<br />

every query in the process is an exercise in frustration, and perhaps in futility.<br />

Given the nature of applications, it’s impossible to tell you specifically where to look to discover what<br />

needs to be optimized, so you need to always keep the preceding advice in mind and your stopwatch in<br />

hand. As you get into production systems, you may find that you are trying to make a whole process run<br />

faster, and that involves breaking that process down into pieces and then timing the pieces. As you discover<br />

the high-cost pieces, you may very well be able to break those down into smaller pieces looking<br />

for high-cost pieces, and so forth, until you end up with one or a small number of queries that represent<br />

that 10 percent where the process is spending 90 percent of its time. Once you get there, what you learn<br />

next in this chapter becomes useful.<br />

What Does <strong>SQL</strong> Tuning Mean?<br />

Database Tuning<br />

Tuning an individual query means examining the structure of the query (and subqueries), the <strong>SQL</strong> syntax,<br />

to discover whether you have designed your tables to support fast data manipulation and written<br />

the query in an optimum manner, allowing your DBMS to manipulate the data efficiently. Queries do<br />

many things, from adding entirely new records to tables, to updating existing records, to pulling data<br />

out of multiple related tables for reporting purposes. It is this last part that many people think of when<br />

discussing optimizing queries, but in fact, a database must be viewed as a whole entity or you risk<br />

speeding up one piece while crippling another. As this chapter demonstrates, there are techniques (particularly<br />

when optimizing other people’s queries) that are essentially free, while others (specifically creating<br />

new indexes) can be applied only at a price.<br />

355

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

Saved successfully!

Ooh no, something went wrong!