본문 바로가기
데엔- Udacity

Udacity - Data Modeling - Relational Data Models

by 녕나 2021. 11. 13.

Relational Data Models 의 중요성

  • 정형화된 데이터 모델: 데이터 포맷이 정형화 되어있어서 SQL 날리기 편함
  • 테이블의 추가/삭제/변경이 간편하고 쉬움
  • 데이터의 integrity(정확도)가 보장
  • SQL 사용 가능

OLAP vs OLTP

OLAP (Online Analytical Process)

  • allow for complex analytical and ad hoc queries, including aggregations. These type of databases are optimized for reads.
  • 월별 사용량 등 통합 쿼리에 특화

OLTP (Online Transactional Process)

  • allow for less complex queries in large volumes. The types of queries for these databases are read, insert, update, and delete.
  • 특정 상품의 가격 등 specific한 쿼리에 특화

Normalization vs Denormalization

- DB structuring에 2가지 것이 필요하다

Normalization: 데이터 중복 X, 데이터 무결성 O 을 위해 하는 작업

  • 데이터 삭제/업데이트 할때 db끼리의 영향성을 없앰
  • 새로운 데이터가 추가되어도 DB 리팩토링 필요 X
  • NoSQL에 반대되게끔 하는 구조
  • 6개의 Normal Form이 있지만, 현업(프로덕션) 레벨에서는 3개의 Normal Form만 충족하면됨
  • 1 -> 2 -> 3 순으로 Normal Form을 만족해야함

1 Normal Form

  • Each cell contains unique and single values
  • Be able to add data without altering tables
  • Separate different relations into different tables
  • Keep relationships between tables together with foreign keys

2 Normal Form

  • Have reached 1NF
  • All columns in the table must rely on the Primary Key

3 Normal Form

  • Must be in 2nd Normal Form
  • No transitive dependencies
  • Remember, transitive dependencies you are trying to maintain is that to get from A-> C, you want to avoid going through B. (1개의 정보만 알고도 C를 구할 수 있는데 다른 정보도 같은 row에 껴있다면 분리해주는 것이 편함)

 

Denormalization: read 속도 및 성능을 올리기위해 어느정도 data duplicate를 허용해서 테이블 리팩토링.

  • normalization 다음에 denormalization 작업을 보통 함
  • Normalized가 엄청된 테이블은 필연적으로 JOIN문을 많이 쓸 수 밖에 없다 -> read 오버헤드 높음

Fact and Dimension Tables

Fact Tables 과 Dimension Tables는 DB에 대한 간략한 설명이다

Star schma:

  • 아래와 같이 fact table 중앙, 그 주변에 dimension table을 도식화 한 것이 star schema
  • 아주 유명한 data mart schema 이다
  • 더 쉽고/원하는 데이터 형태로 추출하기 위해 만들어짐
  • 장점 단점
    간단한 쿼리문 denormalization issue
    빠른 aggregation data duplicacy

Fact_Sales: Fact table, 나머지 4개: Dimension tables

Snowflake schemas:

  • star schema와 비슷하지만 1개의 fact table에 multiple dimension tables가 붙어 있는 경우
  •  

중앙: Fact table에 붙어있는 multi dimensional Dimension tables


Useful PostgresSQL attributes

NOT NULL

  • 이 속성이 추가되면 해당 column은 NULL 값을 가질 수 없음

UNIQUE

  • multiple column들이 각각 unique 속성 가질 수 있음
  • 해당 column은 유니크 값만 갖게됨

PRIMARY KEY

  • single column만이 primary key uniqueness 가질 수 있음
  • 해당 column은 unique한 값만 갖게됨
  • 해당 테이블의 식별자 역할