Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

The slots are often implemented as a doubly linked list (illustrated in Figure 13-4). A doubly linked list is a structure where each container holding data has two pointers to other containers, one to the previous container and another to the next container. This structure allows very fast movement or sorting of data since the data itself is never physically moved; rather the two links are simply modified to point to different neighbors. Of course, the old neighbors’ pointers also have to be modified since they no longer point to this data item, and the new neighbors’ links have to be updated to point to this data item. Figure 13-4 Index Item Data Item Pointer to Prev Pointer to Next Pointer to Rec Data Table Index Item Data Item Pointer to Prev Pointer to Next Pointer to Rec Database Tuning Index Item Data Item Pointer Prev Pointer Next Pointer Data Implementing a doubly linked list means finding the two slots that the new data belongs between, adjusting the pointers of each of these neighbor slots to point to the new data item, and then adjusting the pointers of the empty slot into which the data was placed to point to the data slots on either side. All of this is just grunt work that the DBMS has to go through, and it isn’t a reason not to build an index where one is needed, but just keep in mind that every index causes these data shuffles any time the data in that indexed column changes or is deleted. Perhaps you’ve heard the advice, “It may be faster to drop an index and rebuild it after a bulk insert.” This data shuffle is exactly the reason why that may be the case. If the number of records added, deleted, or modified is a very high percentage of the number of records already in the table, then moving the index items around may be slower than just deleting the index and rebuilding it from scratch. This is also the reason why you want to do large bulk imports during off hours (if any) so that the overhead of shuffling index items or rebuilding indexes does not impact the users. If you are going to be doing large inserts or other modifications of data to indexed tables, it can be beneficial to drop the indexes before doing the insert and then rebuild the index after the data is inserted. Obviously, given the example database used in this book, you will not be able to really see the effects. However, in a production database with millions of records already in a table and deletes or inserts of large quantities of records, you may find dropping indexes very beneficial. 359

The slots are often implemented as a doubly linked list (illustrated in Figure 13-4). A doubly linked list is<br />

a structure where each container holding data has two pointers to other containers, one to the previous<br />

container and another to the next container. This structure allows very fast movement or sorting of data<br />

since the data itself is never physically moved; rather the two links are simply modified to point to different<br />

neighbors. Of course, the old neighbors’ pointers also have to be modified since they no longer<br />

point to this data item, and the new neighbors’ links have to be updated to point to this data item.<br />

Figure 13-4<br />

Index Item<br />

Data Item<br />

Pointer to Prev<br />

Pointer to Next<br />

Pointer to Rec<br />

Data Table<br />

Index Item<br />

Data Item<br />

Pointer to Prev<br />

Pointer to Next<br />

Pointer to Rec<br />

Database Tuning<br />

Index Item<br />

Data Item<br />

Pointer Prev<br />

Pointer Next<br />

Pointer Data<br />

Implementing a doubly linked list means finding the two slots that the new data belongs between,<br />

adjusting the pointers of each of these neighbor slots to point to the new data item, and then adjusting<br />

the pointers of the empty slot into which the data was placed to point to the data slots on either side. All<br />

of this is just grunt work that the DBMS has to go through, and it isn’t a reason not to build an index<br />

where one is needed, but just keep in mind that every index causes these data shuffles any time the data<br />

in that indexed column changes or is deleted.<br />

Perhaps you’ve heard the advice, “It may be faster to drop an index and rebuild it after a bulk insert.”<br />

This data shuffle is exactly the reason why that may be the case. If the number of records added, deleted,<br />

or modified is a very high percentage of the number of records already in the table, then moving the<br />

index items around may be slower than just deleting the index and rebuilding it from scratch. This is<br />

also the reason why you want to do large bulk imports during off hours (if any) so that the overhead of<br />

shuffling index items or rebuilding indexes does not impact the users.<br />

If you are going to be doing large inserts or other modifications of data to indexed tables, it can be beneficial<br />

to drop the indexes before doing the insert and then rebuild the index after the data is inserted.<br />

Obviously, given the example database used in this book, you will not be able to really see the effects.<br />

However, in a production database with millions of records already in a table and deletes or inserts of<br />

large quantities of records, you may find dropping indexes very beneficial.<br />

359

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

Saved successfully!

Ooh no, something went wrong!