Beginning SQL
Beginning SQL Beginning SQL
13 Database Tuning SQL is a language that describes how you retrieve data from a database. Just as in any language, you can say things that make sense but that may not mean what you intend or that can be misinterpreted and cause results that are not what you expect. By now you should be pretty good at expressing yourself in SQL and you should be getting the results, or at least the data, that you intend. On the other hand, you might get exactly the data you expect but it takes a long time to get the data back. That is the subject that this chapter addresses. Specifically, this chapter addresses the following questions: ❑ Why does your query take so long to execute? ❑ Is this just normal, or is there something you can do to get the results faster? ❑ When does it matter? In fact, tuning SQL is just one piece of the bigger puzzle, which is tuning the system itself. Indepth system tuning is outside the scope of this chapter, but an overview of the subject is useful since it is possible to get large improvements in speed at this level. You may find that the database is administered by professionals and that they have already done all that can be done at this level. On the other hand, you may find yourself playing system administrator and realize that you are the only one around who can look at whether some of these issues can be addressed. Or you may find that the database is being administered by a network administrator with no real experience as a database administrator and that a little friendly advice to him will get you a long way. For all these reasons, this chapter covers some subjects that you might not expect to find in a book on basic SQL. Tuning Hardware If you have any control over the subject, the first place to start tuning is at the hardware level. It is a common misconception that servers don’t necessarily need to be extremely powerful machines, and so you may find that while the workstations are the latest and greatest multi-gigahertz machines with a half-gigabyte of RAM, the server is a five-year-old machine running at a few hundred megahertz with a few hundred megabytes of memory. Indeed, since servers tend to be
- Page 686: contention. You could wrap your new
- Page 690: Summary Transactions ensure that us
- Page 696: Chapter 12 328 SQL security is cent
- Page 700: Chapter 12 Creating User IDs Figure
- Page 704: Chapter 12 3. In the SQL window, ty
- Page 708: Chapter 12 ❑ You can assign a dif
- Page 712: Chapter 12 Privileges Privileges ar
- Page 716: Chapter 12 338 You could then give
- Page 720: Chapter 12 You might implement this
- Page 724: Chapter 12 Another shortcut, using
- Page 728: Chapter 12 As you can imagine, givi
- Page 732: Chapter 12 The situation gets a lit
- Page 736: Chapter 12 Summary Database securit
- Page 742: Perhaps this doesn’t sound remote
- Page 746: When an application asks for data f
- Page 750: The point of all the discussion of
- Page 754: How Do You Do It? And herein lies t
- Page 758: The slots are often implemented as
- Page 762: On the other hand, an index on a tr
- Page 766: Tuning Tips The following list cont
- Page 770: Database Tuning Exercises 1. Create
- Page 776: Appendix A Exercise 2 Solution 368
- Page 780: Appendix A ( 8, ‘Jack’, ‘John
- Page 784: Appendix A ZipCode, Email, DateOfJo
13<br />
Database Tuning<br />
<strong>SQL</strong> is a language that describes how you retrieve data from a database. Just as in any language,<br />
you can say things that make sense but that may not mean what you intend or that can be misinterpreted<br />
and cause results that are not what you expect. By now you should be pretty good at<br />
expressing yourself in <strong>SQL</strong> and you should be getting the results, or at least the data, that you<br />
intend. On the other hand, you might get exactly the data you expect but it takes a long time to get<br />
the data back. That is the subject that this chapter addresses. Specifically, this chapter addresses<br />
the following questions:<br />
❑ Why does your query take so long to execute?<br />
❑ Is this just normal, or is there something you can do to get the results faster?<br />
❑ When does it matter?<br />
In fact, tuning <strong>SQL</strong> is just one piece of the bigger puzzle, which is tuning the system itself. Indepth<br />
system tuning is outside the scope of this chapter, but an overview of the subject is useful<br />
since it is possible to get large improvements in speed at this level. You may find that the database<br />
is administered by professionals and that they have already done all that can be done at this level.<br />
On the other hand, you may find yourself playing system administrator and realize that you are<br />
the only one around who can look at whether some of these issues can be addressed. Or you may<br />
find that the database is being administered by a network administrator with no real experience as<br />
a database administrator and that a little friendly advice to him will get you a long way. For all<br />
these reasons, this chapter covers some subjects that you might not expect to find in a book on<br />
basic <strong>SQL</strong>.<br />
Tuning Hardware<br />
If you have any control over the subject, the first place to start tuning is at the hardware level. It is<br />
a common misconception that servers don’t necessarily need to be extremely powerful machines,<br />
and so you may find that while the workstations are the latest and greatest multi-gigahertz<br />
machines with a half-gigabyte of RAM, the server is a five-year-old machine running at a few hundred<br />
megahertz with a few hundred megabytes of memory. Indeed, since servers tend to be