To illustrate the problems related to identity gaps, let’s assume the following simplified database design for storing invoice data:
create table invoices
(invoice_nr numeric(10,0) identity,
customer_nr int,
amount money)
/* insert new invoice */
insert invoices (customer_nr, amount)
values (@new_customer, @new_amount)
This involves a database table named invoices , having an identity column called invoice_nr. When a new invoice is created, the customer number and the amount payable are inserted into the invoices table. No value is specified for the invoice_nr column, as this value will automatically be generated as a result of the identity property. The value assigned to the new invoice number will be 1 higher than the previous invoice number that was generated. This way, identity columns automatically generate unique, consecutive numbers which make ideal primary keys in database systems.
An identity gap has occurred when there is a large, unexpected jump in the value of an identity column, like in this example:
1> select invoice_nr from invoices order by 1
2> go
invoice_nr
----------
(...)
10028
10029
10030
10031
5000002
5000003
(1033 rows affected)
For some reason, the invoice inserted after nr. 10031 was not assigned number 10032 as was expected. Instead, the invoice number jumps to 5000002 and continues to count up from there. This phenomenon is referred to as an "identity gap".
Such discontinuities in identity values are often a serious application problem. For example, some applications might not be able to handle invoice numbers of more than, say, 6 digits. Indeed, identity gaps are usually discovered because of application errors resulting from the unexpected high values in an identity column.
From the point of view of a DBA or end-user, identity gaps are almost always inconvenient, and should often be fixed immediately.
It should be noted here that it is always possible that some individual identity column values are missing. This can happen when the transaction containing the insert operation is rolled back: the identity value that was already issued for this rolled-back row will not be re-used and therefore never show up as an invoice number.
In this article, an "identity gap" refers to a gap of a large number of units, and not to individual missing identity column values.
Why "identity gaps" occur
Identity gaps can occur following a rough server shutdown ("shutdown with nowait" or a crash of the server process. This is related to the algorithm the server uses to generate identity column values: essentially, a counter is kept in server memory, holding the identity value most recently issued. When a new row is inserted, this counter is incremented and the resulting value is assigned to the identity column in that row. While the new data row itself is written to disk, the new value of the in-memory counter is not. Only when the server is shut down in a normal way, this value is saved on disk.
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 下一页