Beginning SQL
Beginning SQL Beginning SQL
How Do You Do It? And herein lies the heart of the matter: How do you optimize a query? As you discovered previously, optimizing the query may in fact be a matter of optimizing the application itself. There are, however, certain generally accepted methods for optimizing a query, and in the sections that follow, you start to examine these specific methods. Indexes are often touted as the be-all and end-all of optimization, knowing what to index and why. Before you examine these methods, though, it is helpful to get an overview of how indexes work and how they speed up (or slow down!) a database. Indexes — What Are They? Database Tuning The first thing to understand about indexes is that they are not necessary; they are a convenience used to speed up accessing data. They are simply a means of getting at a data item in a table without having to start at the beginning of the table and examine every item in the table. Indexes on static data tables are essentially free (other than storage costs), but indexes on frequently changing data have very high costs, as this section explains. Database indexes are often compared to indexes in a book — a set of references in a table that tell where in the physical database to go to find a specific item. While it is true that indexes function in the same manner in databases as they do in books, they are implemented very differently. A book index takes a word or phrase and gives you a list of pages on which you can find the word. In databases, however, indexes are usually implemented as a balanced-tree (b-tree) structure, so called because it looks like an upside-down tree, and the number of data items in each page is kept balanced by the b-tree engine (see Figure 13-3). In any b-tree, there is a root page that holds pointers to other pages (called leaves), which hold pointers to other pages, and so forth. Each page holds the data items from the column being indexed, plus a pointer to the data record holding that item out in the table. To search for data, the DBMS comes in to the top (root) page and looks for its data there. If it doesn’t find the data, it looks up which leaf to go to that has data most similar to the data being searched for and then looks through that leaf page. If it doesn’t find the data there, the DBMS looks for the next leaf down, and so on. Once the data item is found, the actual pointer to the data record is available to pull the entire record. By placing a few hundred data items in each page (with pointers to the actual data record for each item in that page), the total depth of the structure is usually very small. I have heard numbers thrown about indicating that each page points to as many as 300 other leaves, which means that finding a single record in a table with up to 27 million records requires searching through only three levels of leaves. 357
- 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 740: Chapter 13 robust, fault-tolerant m
- Page 744: Chapter 13 Gigahertz Networks or Co
- Page 748: Chapter 13 Database Cache 354 In ad
- Page 752: Chapter 13 Again, the shotgun appro
- 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
- Page 788: Appendix A Exercise 3 Solution Firs
- Page 792: Appendix A 376 Figure A-1 ISBN is a
- Page 796: Appendix A Chapter 5 Exercise 1 Sol
- Page 800: Appendix A 380 You might receive sl
How Do You Do It?<br />
And herein lies the heart of the matter: How do you optimize a query? As you discovered previously,<br />
optimizing the query may in fact be a matter of optimizing the application itself. There are, however, certain<br />
generally accepted methods for optimizing a query, and in the sections that follow, you start to<br />
examine these specific methods. Indexes are often touted as the be-all and end-all of optimization,<br />
knowing what to index and why. Before you examine these methods, though, it is helpful to get an<br />
overview of how indexes work and how they speed up (or slow down!) a database.<br />
Indexes — What Are They?<br />
Database Tuning<br />
The first thing to understand about indexes is that they are not necessary; they are a convenience used to<br />
speed up accessing data. They are simply a means of getting at a data item in a table without having to<br />
start at the beginning of the table and examine every item in the table. Indexes on static data tables are<br />
essentially free (other than storage costs), but indexes on frequently changing data have very high costs,<br />
as this section explains.<br />
Database indexes are often compared to indexes in a book — a set of references in a table that tell where<br />
in the physical database to go to find a specific item. While it is true that indexes function in the same<br />
manner in databases as they do in books, they are implemented very differently. A book index takes a<br />
word or phrase and gives you a list of pages on which you can find the word.<br />
In databases, however, indexes are usually implemented as a balanced-tree (b-tree) structure, so called<br />
because it looks like an upside-down tree, and the number of data items in each page is kept balanced by<br />
the b-tree engine (see Figure 13-3). In any b-tree, there is a root page that holds pointers to other pages<br />
(called leaves), which hold pointers to other pages, and so forth. Each page holds the data items from the<br />
column being indexed, plus a pointer to the data record holding that item out in the table. To search for<br />
data, the DBMS comes in to the top (root) page and looks for its data there. If it doesn’t find the data, it<br />
looks up which leaf to go to that has data most similar to the data being searched for and then looks<br />
through that leaf page. If it doesn’t find the data there, the DBMS looks for the next leaf down, and so<br />
on. Once the data item is found, the actual pointer to the data record is available to pull the entire record.<br />
By placing a few hundred data items in each page (with pointers to the actual data record for each item<br />
in that page), the total depth of the structure is usually very small. I have heard numbers thrown about<br />
indicating that each page points to as many as 300 other leaves, which means that finding a single record<br />
in a table with up to 27 million records requires searching through only three levels of leaves.<br />
357