10/16

AI·2025년 10월 16일

정규화

이상현상(삽입, 삭제, 수정) => 일관성, 무결성 회복
select때는 이상현상 없음

함수의 종속성

정규화 과정

제 1정규형

어떤 이유로 분리하는가 - 키는 동일하면 값이 달라지는 속성때문에 분리



제 2정규형

멀티 키를 보여했을 때, 한 쪽에 의존적인 경우 => 분리시키기

제 3정규형

키가 아닌 속성에 의존적일때 분리

연습

1,3 정규형


2차 정규화

역정규화

트랜잭션

commit, rollback, savepoint
오류 생기면 자동 rollback
원자성, 일관성, 독립성, 지속성

use test1;
CREATE TABLE `customer` (
  `customer_id` int NOT NULL,
  `customer_nm` varchar(45) NOT NULL,
  PRIMARY KEY (`customer_id`)
);

insert into customer values (1, '홍길동');
insert into customer values (2, '이길동');
insert into customer values (3, '삼길동');

select *from customer;
select @@autocommit;

-- 트랜잭션 처리
-- 1. autocommit -> false
-- 2. start transcation - autocommit 무시
--    commit, rollback - autocommit 원래값 복원
set autocommit=0;
select @@autocommit; -- autocommit = false

insert into customer values (4, '사길동'); -- 여기서만 보임

commit; -- 다른 곳에서도 4, 사길동 보임

insert into customer values (5, '오길동');
insert into customer values (6, '육길동');
rollback; -- 5,6 취소

insert into customer values (5, '오길동');
savepoint s1;
insert into customer values (6, '육길동');
rollback to s1; -- 6만 취소

update customer set customer_nm = '오길도2' where customer_id=5;
rollback; -- savepoint도 삭제


start transaction;
내용
commit; rollback;

동시성 제어

갱신 손실 문제 - 쓰기/쓰기
절대 발생되면 안됨
=> lock 메카니즘

use madang;
select * from book where bookid=1;

start transaction;
update book set price = 8000 where bookid=1; -- 여기서만 값 변경, 다른 애들 대기
commit;
select * from book where bookid=1;

update book set price = price+100 where bookid=1;

update book set price = price+100 where bookid=2; -- row 단위로 lock한다는 것을 알 수 있음

update book set publisher = 'aaa' where bookid=1; -- 동일한 price가 아닌 publisher해도 lock 걸림

lock은 row 단위로 걸림

데드락


=> 트랜잭션 짧게 가져가기


한 쪽은 데드락, 다른 쪽은 진행됨
=> 데드락이 발생하면, 어떤 트랜잭션이 에러발생?
다 다름. ex. 락을 적게 가지고 있는 곳가 아니라 락을 통해서 row수가 가장 적게 영향을 받는 트랜잭션, 같다면, 리소스가 적은 거

start transaction;
update book set price = 8000 where bookid=1;
update book set price = 8000 where bookid=2;
commit;
start transaction;
update book set price = 8000 where bookid=2;
update book set price = 8000 where bookid=1;
commit;

Dirty Read

READ UNCOMMITTED	:
	The lowest isolation level.
	Transactions can see changes made by other uncommitted transactions.
	다른 트랜잭션에서 commit 을 하지 않아도 현재 트랜잭션에서는 commit 된 데이터가 보인다.
READ COMMITTED		:
	A slightly higher level.
	Transactions see only changes that have been committed by other transactions.
	다른 트랜잭션에서 commit 을 해야 만 현재 트랜잭션에서는 commit 된 데이터가 보인다.
    가장 많이 사용
REPEATABLE READ		:
	A higher isolation level.
	A transaction sees a consistent snapshot of the database throughout its execution.
	This prevents non-repeatable reads.
	다른 트랜잭션에서 commit 을 해도 현재 트랜잭션에서는 commit 된 데이터가 보이지 않는다. 현재 트랜잭션이 commit해야 보인다.
	
SERIALIZABLE		:
	The highest level. Transactions are completely isolated from each other, as if they were executed one after the other.
create table `users` (
  `id` int not null,
  `name` varchar(20) default null,
  `age` int default null,
  primary key (`id`)
);

insert into users values(1,'홍길동', 30);
select * from users;
-- read
set transaction isolation level read uncommitted;

start transaction;
select * from users where id=1; -- 30

select * from users where id=1; -- 20 -> 30
commit;

set transaction isolation level read committed;

start transaction;
select * from users where id=1; -- 30

select * from users where id=1; -- 30 다른 트랜잭션 update는 했지만, commit은 안했기에

select * from users where id=1; -- 20
commit;
-- write
start transaction;
update users set age=20 where id=1;
rollback;
commit;
--
set transaction isolation level read committed;

start transaction;
select * from users where age between 10 and 30; -- 1개 존재

select * from users where age between 10 and 30; -- 1개 존재; 다른 트랜잭션 insert 했지만, commit은 안했기에

select * from users where age between 10 and 30; -- 2개 존재
commit;
--
set transaction isolation level repeatable read;
start transaction;
select * from users where age between 10 and 30; -- 1개 존재

select * from users where age between 10 and 30; -- 1개 존재; 다른 트랜잭션 insert 했지만, commit은 안했기에

select * from users where age between 10 and 30; -- commit을 해도 1개 존재 
commit;
select * from users where age between 10 and 30; -- 2개 존재
start transaction;
insert into users values (2, '이길동', 20);
commit;

의도락

잘못사용하면, 느려짐

회복

0개의 댓글