Worldscope

Difference between Fact Tables and Dimension Tables

Palavras-chave:

Publicado em: 31/08/2025

Fact Tables vs. Dimension Tables in Data Warehousing

In data warehousing, fact tables and dimension tables are the foundational building blocks of a star schema, which is widely used for analytical processing. This article will explain the differences between these two table types, highlighting their purpose and structure.

Fundamental Concepts / Prerequisites

To understand the differences between fact and dimension tables, you should have a basic understanding of data warehousing principles and the concept of a star schema. The star schema consists of a central fact table surrounded by dimension tables. Familiarity with relational databases is also helpful.

Fact Tables: Recording Business Events

Fact tables store quantitative data that measures business events or transactions. These tables typically contain foreign keys referencing dimension tables, along with numeric measures (facts) that are aggregated and analyzed. Fact tables are generally large and grow rapidly.

Code Example: Sample Fact Table (Sales)


CREATE TABLE Sales (
    SaleID INT PRIMARY KEY,
    CustomerID INT,
    ProductID INT,
    DateID INT,
    QuantitySold INT,
    SaleAmount DECIMAL(10, 2),
    FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID),
    FOREIGN KEY (ProductID) REFERENCES Product(ProductID),
    FOREIGN KEY (DateID) REFERENCES DateDimension(DateID)
);

-- Sample Data Insertion
INSERT INTO Sales (SaleID, CustomerID, ProductID, DateID, QuantitySold, SaleAmount)
VALUES
(1, 101, 201, 20231026, 2, 50.00),
(2, 102, 202, 20231026, 1, 25.00),
(3, 101, 201, 20231027, 3, 75.00);

Code Explanation

The Sales table above is a fact table. The SaleID is the primary key. CustomerID, ProductID, and DateID are foreign keys referencing dimension tables (Customer, Product, and DateDimension, respectively). QuantitySold and SaleAmount are the numeric measures (facts) that can be analyzed, e.g., calculating total sales, average quantity sold, etc.

The FOREIGN KEY constraints ensure that each sale is associated with a valid customer, product, and date, maintaining data integrity.

Dimension Tables: Describing Business Context

Dimension tables store descriptive attributes about the data in the fact table. They provide context and allow users to filter, group, and analyze the facts based on various dimensions like customer, product, location, time, etc. Dimension tables are generally smaller than fact tables and change less frequently.

Code Example: Sample Dimension Table (Customer)


CREATE TABLE Customer (
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(255),
    CustomerAddress VARCHAR(255),
    CustomerCity VARCHAR(255),
    CustomerState VARCHAR(2),
    CustomerZipCode VARCHAR(10)
);

-- Sample Data Insertion
INSERT INTO Customer (CustomerID, CustomerName, CustomerAddress, CustomerCity, CustomerState, CustomerZipCode)
VALUES
(101, 'John Doe', '123 Main St', 'Anytown', 'CA', '91234'),
(102, 'Jane Smith', '456 Oak Ave', 'Somecity', 'NY', '10001');

Code Explanation

The Customer table is a dimension table. CustomerID is the primary key. The other columns (CustomerName, CustomerAddress, etc.) describe attributes of a customer. This allows you to analyze sales based on customer demographics, location, or other relevant characteristics. Similar dimension tables would exist for products and dates.

Complexity Analysis

The complexity of querying data involving fact and dimension tables depends heavily on the specific query and the size of the tables. However, a few general observations can be made:

  • Time Complexity: Queries involving joins between fact and dimension tables can have a time complexity that ranges from O(n) to O(n log n) or worse, depending on the join algorithm used by the database system. Indexing the foreign key columns in the fact table and the primary key columns in the dimension tables can significantly improve query performance. The choice of query execution plan plays a big role.
  • Space Complexity: The space complexity is primarily determined by the size of the fact and dimension tables. Fact tables are typically much larger than dimension tables due to the high volume of transactional data they store. Data warehousing solutions often employ techniques like partitioning and data compression to manage the space requirements.

Alternative Approaches

While the star schema with fact and dimension tables is a common approach, other data modeling techniques exist, such as the snowflake schema. In a snowflake schema, dimension tables can be further normalized into multiple related tables. This reduces data redundancy but can increase the complexity of queries due to the need for more joins.

Another approach is using a Data Vault modeling technique. This is a hybrid approach that is more resistant to changes in business requirements but introduces significant complexity.

Conclusion

Fact tables and dimension tables are crucial components of data warehousing. Fact tables store quantitative data about business events, while dimension tables provide descriptive context. Understanding the differences between them is essential for designing effective data warehouses that support meaningful business analysis. The Star schema that uses both fact and dimension tables is still widely in use due to its flexibility and ease of access. The use of the star schema is the most common approach for building data warehouses.