Physical database design
After completing the logical design of your database, you now move to the physical design. The purpose of building a physical design of your database is to optimize performance while ensuring data integrity by avoiding unnecessary data redundancies. During physical design, you transform the entities into tables, the instances into rows, and the attributes into columns. You and your colleagues must decide on many factors that affect the physical design, some of which are listed below.
-
How to translate entities into physical tables
-
What attributes to use for columns of the physical tables
-
Which columns of the tables to define as keys
-
What indexes to define on the tables
-
What views to define on the tables
-
How to denormalize the tables
-
How to resolve many-to-many relationships
Physical design is the time when you abbreviate the names that you chose during logical design. For example, you can abbreviate the column name that identifies employees, EMPLOYEE_NUMBER, to EMPNO. In previous versions of DB2, you needed to abbreviate column and table names to fit the physical constraint of an 18-byte limit. In Version 8, this task is less restrictive with the increase to a 30-byte maximum.
The task of building the physical design is a job that truly never ends. You need to continually monitor the performance and data integrity characteristics of the database as time passes. Many factors necessitate periodic refinements to the physical design.
DB2 lets you change many of the key attributes of your design with ALTER SQL statements. For example, assume that you design a partitioned table so that it will store 36 months' worth of data. Later you discover that you need to extend that design to hold 84 months' worth of data. You can add or rotate partitions for the current 36 months to accommodate the new design.
The remainder of this chapter includes some valuable information that can help you as you build and refine your database's physical design. However, this task generally requires more experience with DB2 than most readers of this book are likely to have.
Denormalizing tables to improve performance
"Normalizing your entities to avoid redundancy" on page 94 describes normalization only from the viewpoint of logical database design. This viewpoint is appropriate because the rules of normalization do not consider performance.
During physical design, analysts transform the entities into tables and the attributes into columns. Consider again the example in "Second normal form" on page 95. The warehouse address column first appears as part of a table that contains information about parts and warehouses. To further normalize the design of the table, analysts remove the warehouse address column from that table. Analysts also define the column as part of a table that contains information only about warehouses.
Normalizing tables is generally the recommended approach. What if applications require information about both parts and warehouses, including the addresses of warehouses? The premise of the normalization rules is that SQL statements can retrieve the information by joining the two tables. The problem is that, in some cases, performance problems can occur as a result of normalization. For example, some user queries might view data that is in two or more related tables; the result is too many joins. As the number of tables increases, the access costs can increase, depending on the size of the tables, the available indexes, and so on. For example, if indexes are not available, the join of many large tables might take too much time. You might need to denormalize your tables. Denormalization is the intentional duplication of columns in multiple tables, and it increases data redundancy.
Example: Consider the design in which both tables have a column that contains the addresses of warehouses. If this design makes join operations unnecessary, it could be a worthwhile redundancy. Addresses of warehouses do not change often, and if one does change, you can use SQL to update all instances fairly easily.
Tip: Do not automatically assume that all joins take too much time. If you join normalized tables, you do not need to keep the same data values synchronized in multiple tables. In many cases, joins are the most efficient access method, despite the overhead they require. For example, some applications achieve 44-way joins in subsecond response time.
When you are building your physical design, you and your colleagues need to decide whether to denormalize the data. Specifically, you need to decide whether to combine tables or parts of tables that are frequently accessed by joins that have high-performance requirements. This is a complex decision about which this book cannot give specific advice. To make the decision, you need to assess the performance requirements, different methods of accessing the data, and the costs of denormalizing the data. You need to consider the tradeoff: is duplication, in several tables, of often-requested columns less expensive than the time for performing joins?
Recommendations:
-
Do not denormalize tables unless you have a good understanding of the data and the business transactions that access the data. Consult with application developers before denormalizing tables to improve the performance of users' queries.
-
When you decide whether to denormalize a table, consider all programs that regularly access the table, both for reading and for updating. If programs frequently update a table, denormalizing the table affects performance of update programs because updates apply to multiple tables rather than to one table.
In Figure 4.11, information about parts, warehouses, and warehouse addresses appears in two tables, both in normal form.
Figure 4.11 Two tables that satisfy second normal form
Figure 4.12 illustrates the denormalized table.
Figure 4.12 Denormalized table
Resolving many-to-many relationships is a particularly important activity because doing so helps maintain clarity and integrity in your physical database design. To resolve many-to-many relationships, you introduce associative tables, which are intermediate tables that you use to tie, or associate, two tables to each other.
Example: Employees work on many projects. Projects have many employees. In the logical database design, you show this relationship as a many-to-many relationship between project and employee. To resolve this relationship, you create a new associative table, EMPLOYEE_PROJECT. For each combination of employee and project, the EMPLOYEE_PROJECT table contains a corresponding row. The primary key for the table would consist of the employee number (EMPNO) and the project number (PROJNO).
Another decision that you must make relates to the use of repeating groups, which you read about in "First normal form" on page 95.
Example: Assume that a heavily used transaction requires the number of wires that are sold by month in a given year. Performance factors might justify changing a table so that it violates the rule of first normal form by storing repeating groups. In this case, the repeating group would be: MONTH, WIRE. The table would contain a row for the number of sold wires for each month (January wires, February wires, March wires, and so on).
Recommendation: If you decide to denormalize your data, document your denormalization thoroughly. Describe, in detail, the logic behind the denormalization and the steps that you took. Then, if your organization ever needs to normalize the data in the future, an accurate record is available for those who must do the work.
Using views to customize what data a user sees
Some users might find that no single table contains all the data they need; rather, the data might be scattered among several tables. Furthermore, one table might contain more data than users want to see or more than you want to authorize them to see. For those situations, you can create views. A view offers an alternative way of describing data that exists in one or more tables.
You might want to use views for a variety of reasons:
-
To limit access to certain kinds of data
You can create a view that contains only selected columns and rows from one or more tables. Users with the appropriate authorization on the view see only the information that you specify in the view definition.
Example: You can define a view on the EMP table to show all columns except SALARY and COMM (commission). You can grant access to this view to people who are not managers because you probably don't want them to have access to salary and commission information.
-
To combine data from multiple tables
You can create a view that uses UNION or UNION ALL operators to logically combine smaller tables, and then query the view as if it were one large table.
Example: Assume that three tables contain data for a period of one month. You can create a view that is the UNION ALL of three fullselects, one for each month of the first quarter of 2004. At the end of the third month, you can view comprehensive quarterly data.
You can create a view any time after the underlying tables exist. The owner of a set of tables implicitly has the authority to create a view on them. A user with administrative authority at the system or database level can create a view for any owner on any set of tables. If they have the necessary authority, other users can also create views on a table that they didn't create. You can read more about authorization in "Authorizing users to access data" on page 328.
"Defining a view that combines information from several tables" on page 266 has more information about creating views.
Determining what columns to index
If you are involved in the physical design of a database, you will be working with other designers to determine what columns you should index. You will use process models that describe how different applications are going to be accessing the data. This information is important when you decide on indexing strategies to ensure adequate performance.
The main purposes of an index are as follows:
-
To optimize data access. In many cases, access to data is faster with an index than without an index. If the DBMS uses an index to find a row in a table, the scan can be faster than when the DBMS scans an entire table.
-
To ensure uniqueness. A table with a unique index cannot have two rows with the same values in the column or columns that form the index key.
Example: If payroll applications use employee numbers, no two employees can have the same employee number.
-
To enable clustering. A clustering index keeps table rows in a specified sequence, to minimize page access for a set of rows. When a table space is partitioned, a special type of clustering occurs; rows are clustered within each partition.
Example: If the partition is on the month and the clustering index is on the name, the rows will be clustered on the name within the month.
In general, users of the table are unaware that an index is in use. DB2 decides whether to use the index to access the table.
You can read more about indexes in "Defining indexes" on page 254.