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.

Tuning Tips<br />

The following list contains some helpful database tuning tips:<br />

Database Tuning<br />

❑ Reduce the amount of data pulled. Avoid using SELECT * queries; instead pull exactly and only<br />

the fields that you need. It is very easy to get lazy and use the asterisk (*), figuring that as new<br />

columns are needed they will already be there and the query will not have to be revisited.<br />

Although this is true, you are also forcing the DBMS to retrieve data that will never be used,<br />

flushing useful data out of caches, loading the network with data that will never be used, and<br />

burdening the workstation with unused data.<br />

❑ Think carefully before creating new indexes, particularly indexes on columns other than primary<br />

keys or foreign keys. Indexes may negatively impact other areas of the application.<br />

Unfortunately, if you work in a large database with other developers, you cannot simply delete<br />

indexes, even if you created them, since you can never be sure that queries you didn’t create<br />

don’t use the index you did create to good effect.<br />

❑ Watch for filter redundancy, conditions where a filter on one column guarantees values in a different<br />

column. This problem is not uncommon and often occurs where a single condition did<br />

not provide a narrow-enough match, so a second or even third condition was added to the filter.<br />

❑ WHERE conditions such as NOT NULL may cause the optimizer to assume that the number of<br />

rows matched will be too large to be useful. Remember that optimizers try to look at the entire<br />

query and figure out how to minimize the work required to return the data. Particularly if you<br />

have a lot of filter conditions, the optimizer may simply ignore some of the conditions if the filter<br />

in that condition is too broad.<br />

❑ Use pattern matching judiciously. LIKE COL% is a valid WHERE condition, reducing the returned<br />

set to only those records with data starting with the string COL. However, COL%Y does not further<br />

reduce the returned results set since %Y cannot be effectively evaluated; the effort to do the<br />

evaluation is too large to be considered. In this case, the COL% is used, but the %Y is thrown<br />

away. For the same reason, a leading wildcard %COL effectively prevents the entire filter from<br />

being used.<br />

❑ Avoid number-to-character conversions with pattern matching, particularly with the < and ><br />

operator. Numbers and characters compare differently and can lead to odd behaviors.<br />

❑ The “not equal” operators can cause the optimizer to throw out the WHERE condition since it<br />

may consider the range of values returned to be too broad to be useful.<br />

❑ Be very careful of equality operators with real numbers and date/time values. Both of these can<br />

have small differences that are not obvious to the eye but that make an exact match impossible,<br />

thus preventing your queries from ever returning rows.<br />

❑ Cartesian joins are actually a non-join — where two tables are selected, but no join is specified.<br />

The result of Cartesian joins is that a row is returned for the product of the two tables — one row<br />

for each row in the first table and one row for each row in the second table. If each table has 10<br />

rows, 100 rows are returned. If each table has 100 rows, 10,000 rows are returned. Such queries<br />

are occasionally useful but are most often a simple mistake in syntax. Luckily, they are usually<br />

easy to spot since the results set has far more records than expected or processes very slowly.<br />

This assumes that the results set ever comes back, of course. If the two tables are large, the<br />

results set can be so huge that the query simply can’t complete in a reasonable time. The DBMS<br />

may very well time out the query.<br />

363

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

Saved successfully!

Ooh no, something went wrong!