Knowledge / Artificial Intelligence and Data Science / Data warehousing and ETL

Data warehousing and ETL

Artificial Intelligence and Data Science (B.E./B.Tech) · Data engineering and deployment · free study notes for exams, placements and interviews

In short

A data warehouse is a subject-oriented, integrated, time-variant and non-volatile collection of data built for analysis and decision support, kept separate from the transactional systems that run the business. ETL (Extract, Transform, Load) is the process that pulls data out of source systems, cleans and conforms it, and loads it into the warehouse; ELT flips the order and transforms inside the warehouse engine. The topic carries steady marks in university "Data Warehousing and Data Mining" / "Data Engineering" papers and appears in GATE DA (data preprocessing, schemas) and in nearly every data-engineering or analytics interview. Indian employers — TCS, Infosys, Wipro, Accenture, Cognizant, LTIMindtree, Flipkart, Zomato, Paytm, HDFC Bank, Jio, Fractal, ZS — hire heavily for ETL and dimensional modelling, and RBI/SEBI/UIDAI-regulated data stores make the governance side examinable too.

Explained

Why a warehouse: OLTP versus OLAP

Architectures and reference models

Dimensional modelling

Slowly Changing Dimensions (SCD)

The ETL process, step by step

  1. Extract — read from sources: full extract (whole table), incremental extract (by last_modified timestamp or an increasing ID), or CDC (change data capture) from the database transaction log/binlog (lowest impact, captures deletes). Log-based CDC is preferred for large OLTP tables; trigger-based CDC is accurate but slows the source.
  2. Land in staging — write raw, untransformed data to a staging schema or object store. Staging is transient, has no indexes or constraints, and exists so extraction is never repeated and so transforms are restartable.
  3. Transform — in order: data-type casting; cleansing (nulls, defaults, trimming, case); standardisation (addresses, pincodes, state names, GSTIN format, 10-digit mobile normalisation); de-duplication and record matching/survivorship; validation against business rules with rejects routed to an error table; derivation of calculated columns; aggregation; surrogate key lookup/generation; SCD handling.
  4. Load — dimensions first (so fact lookups find keys), then facts. Initial/full load once, then incremental loads. Load patterns: insert-only append, upsert/merge (MERGE statement), truncate-and-reload for small tables, partition exchange/swap for large daily partitions.
  5. Post-load — rebuild indexes and statistics, refresh materialized views and aggregates, run reconciliation counts, publish the batch as available, update the metadata/audit log.
  6. Audit columns on every table: batch_id, source_system, load_timestamp, record_hash. A hash (checksum) of attributes is the cheap way to detect a changed dimension row.
  7. Idempotence and restartability: a job re-run for the same batch must produce the same result. Achieved with batch IDs, high-water marks, delete-then-insert of the batch's own partition, and transactional or atomic swap loads.
  8. Error handling: reject file/table + severity (warn, reject row, abort batch), row counts reconciled source vs target, and alerting. Late-arriving facts (event date older than the current batch) must look up the dimension version valid at the event date; early-arriving facts need an inferred ("late-arriving dimension") placeholder row.

ETL versus ELT, and the engineering side

Key points

Formulas and examples

Fact table rows ≈ (number of base events per period) × (number of periods retained)
Maximum cuboids in a lattice (no hierarchies) = 2n, where n = number of dimensions
With hierarchies: total cuboids = Π (Lᵢ + 1), where Lᵢ = number of levels in dimension i (the +1 is the "ALL" level)
Dimension cardinality for a Type 2 dimension ≈ N × (1 + c × t), N = entities, c = changes per entity per year, t = years retained
Row size (bytes) = Σ column widths; Table size = rows × row size × (1 + index/overhead factor) ÷ compression ratio
Compression ratio = uncompressed size ÷ compressed size
ETL throughput (rows/s) = rows loaded ÷ elapsed seconds; Load window headroom = (window seconds − elapsed seconds) ÷ window seconds
Semi-additive balance over time: period value = closing balance (last non-null) or average = Σ daily balances ÷ number of days

Example 1 — Fact table sizing and storage. A retail chain has 1,200 stores, each averaging 900 bills a day with 4 line items per bill. One fact row per line item is kept for 3 years (1,095 days).

Example 2 — SCD Type 2 row growth. A bank has N = 4,00,00,000 (4 crore) customers. On average c = 0.3 attribute changes per customer per year (address, mobile, risk band), history kept t = 7 years.

Example 3 — Cuboids in a cube lattice. A sales cube has 4 dimensions: Time, Product, Store, Customer.

Example 4 — Incremental load versus full reload. A source order table has 50,00,000 rows; 1.8% change daily. Load throughput is 25,000 rows/s and the nightly window is 02:00-04:00 (7,200 s).

Example 5 — Semi-additive measure. A savings account has daily closing balances over 5 days: ₹12,000, ₹9,500, ₹9,500, ₹40,000, ₹18,000.

AspectOLTP (source)Data warehouse (OLAP)
PurposeRun the businessAnalyse the business
WorkloadShort read-write transactionsLong read-mostly aggregate queries
DesignNormalised 3NFStar / snowflake, denormalised
DataCurrent, detailedHistorical, summarised + detailed
UsersClerks, apps, customersAnalysts, managers, data scientists
Access unitFew rows by keyMillions of rows by range scan
Backup/recoveryCritical, point-in-timeReloadable from sources
AspectStar schemaSnowflake schema
Dimension formDenormalised, single tableNormalised into sub-tables
Joins per queryFewer (one per dimension)More (one per level)
Query speedFasterSlower
StorageMore redundancyLess redundancy
MaintenanceSimple ETLMore ETL work, cleaner updates
Best forBI tools, business usersVery large, frequently changing dimensions
AspectETLELT
Transform locationSeparate ETL engineInside the target warehouse
Raw data retainedOften notYes, re-processable
NeedsStrong ETL serverStrong MPP/elastic warehouse
Sensitive dataMasked before loadingLands raw — needs in-warehouse controls
Typical toolsInformatica, DataStage, SSIS, TalendSQL + dbt in a cloud warehouse
-- SCD Type 2 merge: expire the changed row, insert the new version
UPDATE dim_customer d
   SET effective_to = :batch_date - 1, is_current = 'N'
 FROM stg_customer s
WHERE d.customer_id = s.customer_id
  AND d.is_current  = 'Y'
  AND d.attr_hash  <> s.attr_hash;

INSERT INTO dim_customer
      (customer_sk, customer_id, name, city, risk_band,
       attr_hash, effective_from, effective_to, is_current, batch_id)
SELECT nextval('dim_customer_seq'), s.customer_id, s.name, s.city, s.risk_band,
       s.attr_hash, :batch_date, DATE '9999-12-31', 'Y', :batch_id
  FROM stg_customer s
  LEFT JOIN dim_customer d
         ON d.customer_id = s.customer_id AND d.is_current = 'Y'
 WHERE d.customer_sk IS NULL          -- brand-new customer
    OR d.attr_hash <> s.attr_hash;    -- changed attributes

-- Fact load: point each event at the dimension version valid on that date
INSERT INTO fact_sales (date_sk, customer_sk, product_sk, qty, amount, batch_id)
SELECT dt.date_sk, dc.customer_sk, dp.product_sk, s.qty, s.amount, :batch_id
  FROM stg_sales s
  JOIN dim_date    dt ON dt.full_date = s.txn_date
  JOIN dim_customer dc ON dc.customer_id = s.customer_id
                      AND s.txn_date BETWEEN dc.effective_from AND dc.effective_to
  LEFT JOIN dim_product dp ON dp.product_id = s.product_id;

Where it is used

Interview questions

Common mistakes

Related topics

Big data: Hadoop and Spark · Data pipelines: Airflow and Kafka · Data lakes and lakehouses · Cloud data platforms: BigQuery, Snowflake and Databricks · DBMS: SQL, normalisation and indexing · Business intelligence and data visualisation

Open in the Knowledge reader →Find jobs →