Introduction
This article is intended to explain the main storage structures used in database systems through examples in Microsoft SQL Server and Oracle Database. Choosing the right structure is an important strategy for database developers and architects in order to access data efficiently and have a good quality of service in applications such as performance and availability.
Thus we're going to analyze the types of tables and indexes we may face when designing the data requirements of an enterprise solution in order to result in long-live databases.
Tables in SQL Server
1.1 Heap tables
Heap tables are the simplest data storage structure and also the most common creation of table through the SQL statement ''create table"; however it might change over time when you create an index on it.
Heap tables use a classic data structure called heap which is basically an area of space (in disk or memory) where data rows are placed where they fit best. The most important characteristic is that the data rows are unordered. Data rows are grouped by pages logically inside the table thus the logical address of a row in a data file is given by a row id that comprises a page number within the file and a slot number identifying the row inside the page. The number of rows that will fit on a page is not fixed.
A new row is inserted by appending it to the end of the pages, and it is deleted by setting up the slot it ocuppies as empty. When another new row is inserted, the empties slots are checked to see if one of them fits or not then the row is appended to the end of the last page. As this process goes over time, then some gaps may appear and sometimes these gaps may represent an amount of wasted space. Thus, a defragmentation technique must be applied to the pages in the data file.
Let's demonstrate the unordered behavior of heap tables using some simple sequence of SQL code which can be tested in any RDBMS such as Oracle Database and Microsoft SQL Server. I've tested this code in Microsoft SQL Server 2005 (see Listing 1).
create table tbl_heap (nid int)
Messages: Command(s) completed successfully.
insert into tbl_heap(nid) values(1)
Messages: (1 row(s) affected).
insert into tbl_heap(nid) values(2)
Messages: (1 row(s) affected).
insert into tbl_heap(nid) values(3)
Messages: (1 row(s) affected).
delete from tbl_heap where nid=2
Messages: (1 row(s) affected).
insert into tbl_heap(nid) values(4)
Messages: (1 row(s) affected).
select nid from tbl_heap
Output: order showed is 1,4,3
Listing 1. SQL sequence of code showing the unordered behavior of heap tables
It is remarkable to say that this storage structure has an overhead associated with the number of Input/Output (I/O) operations required to do selections of rows because when you need to access specific rows in the table, the database system must scan all the pages looking for the required rows.
1.2 Sorted tables
Because of the drawbacks of heap tables, you need a more sophisticated storage structure. Then if the rows are stored in an arbitrary order by sorting them based on the values of one or more attributes of the table, you have the advantage when searching rows to stop the scan process at the point at which those are located, and therefore the query performance increases. But we have an overhead associated with this solution and it's that when a new row is inserted, this storage structure may be changed over and over depending on the position where the row is going to reside.
A solution to this problem is to leave empty spaces on each page for being used in future insertions.
The fill factor parameter refers to the percentage of space in a page that is initially filled with living room for new rows. This is not a definitive solution because a new row might be inserted and it gets too large to fit in the left space by the fill factor, therefore the database systems must set up another page for it.
The implementation of sorted tables in Oracle Database is by the index-organized tables (IOT) where the data s physically stored and sorted by the primary key.
Let's create an IOT table using Oracle SQL (see Listing 2)
create table tbl_iot
(id int primary key,
value int,
change date)
organization index
including change
overflow
Listing 2. The creation of the tbl_iot index-organized table
As you can see in Listing 2, the organization index clause is required for the creation of the IOT table while overflow and including clause are optional. The overflow clause specifies the database systems to set up another segment, thus making the IOT tables a multisegment object, where the rows can overflow when it gets too large. The including clause tells the database systems to include the specified columns on the index block and the remaining columns are stored in the overflow.
The implementation of sorted tables in SQL Server is by using a clustered index on the primary key of the table (see Listing 3)
create table ctbl_facts
(id int not null,
value int)
go
alter table ctbl_facts
add constraint PK_ ctbl_facts primary key clustered (Id)
go
Listing 3. The creation of a clustered index on the primary key of one table
Indexes
An index on a table provides a mechanism for locating one or more rows without searching the entire table. The property to be located is specified by a set of columns of the indexed table called search key or index key which is a way of the faster access path to data and increases the overall performance of query execution.
The structure of an index consists of a set of entries together with a mechanism for locating the rows based on the search key value. The use of an index can reduce the number of data file pages retrieve, although accessing the index itself is another overhead. In the case of thousands of millions of rows using an index, it might be possible to access to a row in one or two I/O operations. It is remarkable to say that index needs maintenance and when a row is inserted or updated in the table, the index must be modified in order to be adapted to the changes.
There are two types of indexes: clustered and unclustered. In a clustered index, the entries which are close imply proximity among the corresponding data rows. A sorted index is clustered if the index entries and data rows are sorted on the same key; otherwise, the index is unclustered.
In a clustered index, the leaf node of the index tree has the actual data pages itself, while in an unclustered index, the leaf node contains pointers to data pages or clustered index data pages. For this reason of the physical order of rows in the data according to the index, you can have only one clustered index on a given table. Clustered index concept corresponds to the index-organized table (IOT) in Oracle Database and clustered index on a table in Microsoft SQL Server which we discussed in the last section.
There are fundamentally two types of index structures implemented in most database systems: B*Tree and Bitmap indexes. A B*Tree index or Balance Tree index is the most commonly used index structure and default when you create an index using the create index SQL statement. B*Tree is similar in structure to a binary tree. One property of B*Tree is that the tree is balanced, and it implies that despite the insertion and deletion of rows, any path from the tree root to a leaf node has the same length, and from the point of view of database systems the selection of a row has the same performance overhead independently of the searched row. Most B*Tree indexes will have a length of 2 or 3 levels even for thousands of millions of rows.

Gowtham RajamanickamPosted Apr 17, 2016, 7:17 AM
Helpful
Hassan HumayunPosted May 27, 2011, 4:30 PM
thanks , v helpful