20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11<br />

Database<br />

There are perfectly valid reasons for locking the entire database. Perhaps a process requires making<br />

changes to most of the tables in the database, perhaps even the structure of a large number of tables.<br />

Getting locks to each table to be modified, in the correct order, can take a long time. It might make sense<br />

in this instance just to lock the entire database, but of course you should do this as seldom as possible.<br />

Do so at times when you have the fewest users trying to access data, and make your updates as quickly<br />

as possible.<br />

Table<br />

The next level of locking is an entire table. Say a manufacturer wants to update the price on every item<br />

they sell by 5 percent. Rather than locking each record, locking the table that contains the record, running<br />

an UPDATE query on every record in a single <strong>SQL</strong> statement, and then releasing the lock would be far<br />

faster.<br />

Page<br />

DBMSs tend to work with the physical medium that they are stored on to help them manage the data.<br />

The DBMS may break its table data down into chunks that fit in a sector on the disk. These chunks are<br />

called pages, and of course the page size often corresponds to the sector size of the hard disk. Thus, if the<br />

disk has a sector size of 16K, the DBMS applies locks to an entire page since this maps easily to lock<br />

requests sent to the operating system. For this reason, disks used for storing databases may be most efficient<br />

if formatted with the smallest sector size available (within reason). Obviously, a 2K sector locks<br />

fewer data rows than an 8K or a 32K sector. In the end, a record in a given table usually holds a specific<br />

number of bytes. If a record has five fields that hold 400 bytes, then a 2K sector locks roughly five or six<br />

such records (assuming page locks are used), but an 8K sector locks 20 records, and a 32K sector locks 80<br />

records. As you can see, large sector sizes lock very large chunks of a table even if you need to lock only<br />

a small part of the table.<br />

Row<br />

Many modern databases allow locking to the row, or record, level. This means that other users without<br />

interactions may use adjacent records in the database. Of course, this is good, but remember that each<br />

lock requires manipulation by the DBMS. If 40 percent of all the records in a table need to be updated by<br />

a mass update, and the table contains a million records, it makes much more sense to just lock the table<br />

rather than apply for 400,000 record-level locks. On the other hand, if only 100 of those million records<br />

need updating, asking for 100 record locks minimizes the inconvenience to other users who still need to<br />

use the table.<br />

Column<br />

Although it is theoretically possible to lock data at the column level, few if any DBMSs do so. The overhead<br />

to track the locks at this granularity is simply too high and the benefit too small.<br />

Locking Levels<br />

314<br />

In addition to the granularity or size of the data unit being locked, locks also have a property known as<br />

the level. The level determines how rigidly the lock is applied and whether any other user can use the<br />

data in any manner. The first locking level is the shared lock.

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

Saved successfully!

Ooh no, something went wrong!