The mystery of rdb$db_key I.First, let me state in plain words that db_keys are glorified record positions. Relational theory says nothing about implementation details. Conceptually, a relational database knows nothing about a record number, it only works with set of records. However, it's clear the implementation must deal with these details. IB makes explicit to the user such record position if the user requests this information. In Paradox and Dbase, using absolute record numbers was a common practice due to the desktop and single user nature of these products, although Paradox has multi user capabilities that makes dangerous assuming too much about absolute positions in a networked environment, because another user can drop a record that was known to exist ten minutes before. In a desktop database, a record number is a fast way to reach the desired record. Almost no translation is needed: given a record-num, the size of each record is known, so the position of the target record is easily calculated and the position to read inside the file holding the record is sent to the operating system if such information is not already in the buffer of the application. When I say "almost no translation" I'm writing from the point of view of the database. Obviously, the operating system must translate a virtual disk address in its file system and pass the raw address to the IDE/SCSI controller than in turn passes such address to the firmware of the hard disk... and you still think it's easy to identify the culprit when a read error happens? The only official information about db_key that I could find in the IB6 documentation is in Language reference and it's on chapter "System tables and views", where the fields of rdb$relations are detailed:
Not too much, after all. But you know that for tables, db_key uses 8 bytes and for a view, if such view joins three tables, then its db_key uses 24 bytes. This is important if you are working with stored procedures and want to keep db_key in a variable. You must use a char data type of the correct length. How does a db_key look in a query? Let's use isql.exe, the command-line utility with an arbitrary table: SQL> select rdb$db_key from correlativo; So, at first glance, there would be some error in the documentation. To clear the confusion, we will use another table: SQL> select rdb$db_key from images; Then you can realize that the trick is only visual: by default, db_keys are shown as hex values, so you need two hex digits to represent a byte and you have 16 hex digits, 8 bytes. The documentation is correct. One implication is that the largest position of a record is (using unsigned numbers) is In some tables, each db_key is one more than the previous while in other tables, each db_key is two more than the previous, for example, so this may be due to the record's length. See the two examples above. While personal digressions may be funny, they may convey wrong ideas to the readers, so I decided to ask an expert and here's the response from David Schnepper: So, the first lesson to learn is that db_keys don't have to be monotonically increasing, that they are raw positions related to the database itself and not to the operating system's addresses and that they change after a backup and subsequent restore. If you are interested in an analogy for db_keys, you should read about RIDs (Record Indentifiers) in SqlServer and RowIDs in Oracle. However, in SqlServer, the record identifier is only available to database drivers and not to stored procedures, whereas in Oracle it's exposed to general programmers as it's the case with Interbase.
|
This page was last updated on 2000-09-19 03:19:19 |