Table of Agenda
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
제 8회 수요 세미나
1. [PostgreSQL] Vacuum의 거의 모든 것 (4차)
2. [MySQL] Lock (1차)
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것 (4차)
발표자: 연구컨텐츠팀 이근오
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
Page Layout ( 1/2 )
Dump
0.
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
SELECT * from heap_page_items(get_raw_page('t2', 0));
Page Layout ( 1/2 )
Dump
0.
00 7c 9f c0 00 7c 9f 80
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
SELECT * from heap_page_items(get_raw_page('t2', 0));
create table t2 ( c1 char(19),c2 char(8),c3 char(8) );
insert into t2 values('A1001','A','A');
insert into t2 values('A1002','A','A');
Dump
all line pointers on a heap page
1.
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
SELECT * from heap_page_items(get_raw_page('t2', 0));
update t2 set c2='B' where c1 = 'A1001' ;
Dump
2.
새로운 위치에변경데이타 “B” 레코드를 추가.
ItemIdData 부분도 추가됨.
“A1001” 원본은 “xmax” 값을 변경
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
update t2 set c2='B' where c1 = 'A1001' ;
tab.sql
2.
select xmin,xmax,ctid,* from t1;
select txid_current() ;
select xmin,xmax,ctid,* from t2;“A1001” item 의 xmin 변경됨.
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
SELECT * from heap_page_items(get_raw_page('t2', 0));
vacuum t2 ;
Dump
3.
“순서가 변경됨”• A1002가 제일 아래로 내려감• A1001 위치도 아래로 변경• A1001 원래 위치의 데이터는 그대로
두고 ItemIdData만 삭제.
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
vacuum t2 ;
tab.sql
3.
select xmin,xmax,ctid,* from t1;
select txid_current() ;
select xmin,xmax,ctid,* from t2;
변경안됨
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
vacuum freeze t2 ;
tab.sql
4.
select xmin,xmax,ctid,* from t1;
select xmin,xmax,ctid,* from t2;
1807012522에서 변경됨
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
vacuum full t2 ;
tab.sql
5.
select xmin,xmax,ctid,* from t1;
select xmin,xmax,ctid,* from t2;
136677에서 변경됨 변경안됨
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
vacuum full t2 ;
Dump
5.
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
autovacuum_analyze_scale_factor = 0.1
autovacuum_analyze_threshold = 50
6.
1. 1000건 insert
2. autovacuum_naptime=60s 이상 wait 후 확인 : autoanalyze OK.
3. autovacuum_analyze_scale_factor=0.1 인 100건 insert wait 후 확인 : autoanalyze NOT OK.
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
autovacuum_analyze_scale_factor = 0.1
autovacuum_analyze_threshold = 50
6.
4. autovacuum_analyze_threshold=50 인 50건 insert wait 후 확인 : autoanalyze NOT OK.
5. Only 1건 insert wait 후 확인 : autoanalyze OK.
autovacuum analyze 대상 조건변경건수(insert포함) > ( reltuples * 0.1(autovacuum_analyze_scale_factor) )
+ 50(autovacuum_analyze_scale_factor)
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
autovacuum_vacuum_scale_factor = 0.2
autovacuum_vacuum_threshold = 50
7.
1. 1000건 insert wait 후 확인 : autovacuum NOT OK.
2. autovacuum_vacuum_scale_factor=0.2 인 200건 update wait 후 확인 : autovacuum NOT OK.
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
[PostgreSQL] Vacuum의 거의 모든 것(4차)
autovacuum_vacuum_scale_factor = 0.2
autovacuum_vacuum_threshold = 50
7.
3. autovacuum_vacuum_threshold=50 인 50건 update wait 후 확인 : autovacuum NOT OK.
4. Only 1건 update wait 후 확인 : autovacuum OK.
autovacuum vacuum 대상 조건변경건수(insert제외) > ( reltuples * 0.2(autovacuum_vacuum_scale_factor) )
+ 50(autovacuum_vacuum_threshold)
02. [MySQL] MySQL Lock
2-1. Global Read Lock
2-2. Table Lock
2-3. String Lock
2-4. Name Lock
Table of Agenda
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
FLUSH TABLES WITH READ LOCK;UNLOCK TABLES;
• 서버 전체(모든 스토리지 엔진)에 Global Read Lock획득
• Table Cache에 있는 테이블을 Flush (기존의 문장이 Table Lock을 가지고 수행 중이라
면 대기함)
• SELECT문을 제외한 문장은 Global Read Lock이 해제될 때까지 대기 (State: Waiting
for global read lock)
• 주로 DB전체의 일관성 있는 백업을 받기 위해 사용
2-1. Global Read Lock
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
(root@localhost)[(none)] 13:48:51>show status like 'open_tables';+---------------+-------+| Variable_name | Value |+---------------+-------+| Open_tables | 54 |+---------------+-------+1 row in set (0.00 sec)
(root@localhost)[(none)] 13:48:53>flush tables with read lock;Query OK, 0 rows affected (9.06 sec)
(root@localhost)[(none)] 13:49:10>show status like 'open_tables';+---------------+-------+| Variable_name | Value |+---------------+-------+| Open_tables | 1 |+---------------+-------+1 row in set (0.00 sec)
(root@localhost)[(none)] 14:00:20>unlock tables;Query OK, 0 rows affected (0.00 sec)
2-1. Global Read Lock
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
(root@localhost)[exem_i] 13:32:41>select connection_id();+-----------------+| connection_id() |+-----------------+| 14286 |+-----------------+1 row in set (0.00 sec)
(root@localhost)[exem_i] 13:33:16>lock table foo write;Query OK, 0 rows affected (10.97 sec)
2-1. Global Read Lock
(root@localhost)[(none)] 13:34:37>select connection_id();+-----------------+| connection_id() |+-----------------+| 13 |+-----------------+1 row in set (0.00 sec)
(root@localhost)[(none)] 13:34:44>flush tables with read lock;
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
2-1. Global Read Lock
(root@localhost)[exem_i] 13:33:34>show processlist;+-------+------+-----------+--------+---------+------+------------------------------+----------------------------+| Id | User | Host | db | Command | Time | State | Info |+-------+------+-----------+--------+---------+------+------------------------------+----------------------------+| 13 | root | localhost | NULL | Query | 20 | Waiting for global read lock | flush table with read lock || 14286 | root | localhost | exem_i | Query | 0 | starting | show processlist |+-------+------+-----------+--------+---------+------+------------------------------+----------------------------+
2 rows in set (0.00 sec)
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
LOCK TABLES tbl_name [[AS] alias] lock_type [, tbl_name [[AS] alias] lock_type...UNLOCK TABLES
• Lock_type : READ [LOCAL] | [LOW_PRIORITY] WRITE
• MySQL레벨에서 테이블 단위로 설정되는 LOCK
• MyISAM, MEMORY Storage Engine에서는 데이터를 변경하는 즉시 묵시적으로 획득 후 변경 후 반환
• 해당 테이블에 LOCK_TABLE 권한 필요
• 서버 레벨이지만 MyISAM스토리지 엔진을 기준으로 한 LOCK이며 InnoDB에서는 사용을 권장하지 않음
• READ LOCK :
해당세션과 다른 세션 모두 Select문장만 수행가능하며 Local 옵션을 사용 하는 경우 Concurrent Insert가능
다른 세션에서 획득한 READ LOCK끼리 호환가능
• WRITE LOCK :
해당 세션에서만 DML,DDL이 가능하며 다른 세션의 읽기,쓰기, TABLE LOCK과 호환불가
LOW_PRIORITY 옵션을 사용하는 경우 Lock 요청 큐에서 낮은 우선순위를 갖게 됨
2-2. Table Lock
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
(root@localhost)[exem_i] 15:20:06>select connection_id();+-----------------+| connection_id() |+-----------------+| 14286|+-----------------+
1 row in set (0.00 sec)
(root@localhost)[exem_i] 14:36:22>lock table foo read;Query OK, 0 rows affected (0.00 sec)
(root@localhost)[exem_i] 14:36:51>select * from foo;+------+------+| c1 | c2 |+------+------+| 31 | a || 2 | b || 123 | a |+------+------+
3 rows in set (0.00 sec)
(root@localhost)[exem_i] 14:36:57>update foo set c1=1 where c2='a';ERROR 1099 (HY000): Table 'foo' was locked with a READ lock and can't be updated
2-2. Table Lock
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
(root@localhost)[exem_i] 15:20:06>select connection_id();+-----------------+| connection_id() |+-----------------+| 13 |+-----------------+
1 row in set (0.00 sec)
(root@localhost)[exem_i] 15:28:07>select * from foo;+------+------+| c1 | c2 |+------+------+| 31 | a || 2 | b || 123 | a |+------+------+
3 rows in set (0.00 sec)(root@localhost)[exem_i] 15:28:11>update foo set c1=1 where c2='a';(root@localhost)[exem_i] 15:27:31>show processlist;+-------+------+-----------+--------+---------+------+---------------------------------+----------------------------------+| Id | User | Host | db | Command | Time | State | Info |+-------+------+-----------+--------+---------+------+---------------------------------+----------------------------------+| 13 | root | localhost | exem_i | Query | 15 | Waiting for table metadata lock | update foo set c1=1 where c2='a' || 14286 | root | localhost | exem_i | Query | 0 | starting | show processlist || 25063 | root | localhost | NULL | Sleep | 2833 | | NULL |+-------+------+-----------+--------+---------+------+---------------------------------+----------------------------------+
3 rows in set (0.00 sec)
2-2. Table Lock
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
(root@localhost)[exem_i] 15:20:06>select connection_id();+-----------------+| connection_id() |+-----------------+| 13 |+-----------------+
1 row in set (0.00 sec)(root@localhost)[exem_i] 15:34:08>lock table foo read;Query OK, 0 rows affected (0.00 sec)
(root@localhost)[exem_i] 15:34:15>lock table foo write;
(root@localhost)[exem_i] 15:31:58>show processlist;+-------+------+-----------+--------+---------+------+---------------------------------+----------------------+| Id | User | Host | db | Command | Time | State | Info |+-------+------+-----------+--------+---------+------+---------------------------------+----------------------+| 13 | root | localhost | exem_i | Query | 5 | Waiting for table metadata lock | lock table foo write || 14286 | root | localhost | exem_i | Query | 0 | starting | show processlist || 31080 | root | localhost | NULL | Sleep | 147 | | NULL |+-------+------+-----------+--------+---------+------+---------------------------------+----------------------+
3 rows in set (0.00 sec)
2-2. Table Lock
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
mysql> lock tables foo read;Query OK, 0 rows affected (0.00 sec)
mysql> show open tables from exem_i;+----------+--------+--------+-------------+| Database | Table | In_use | Name_locked |+----------+--------+--------+-------------+| exem_i | t1 | 0 | 0 || exem_i | myisam | 0 | 0 || exem_i | foo | 1 | 0 |+----------+--------+--------+-------------+
3 rows in set (0.00 sec)
mysql> unlock tables;Query OK, 0 rows affected (0.00 sec)
mysql> show open tables from exem_i;+----------+--------+--------+-------------+| Database | Table | In_use | Name_locked |+----------+--------+--------+-------------+| exem_i | t1 | 0 | 0 || exem_i | myisam | 0 | 0 || exem_i | foo | 0 | 0 |+----------+--------+--------+-------------+
3 rows in set (0.00 sec)
2-2. Table Lock
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
GET_LOCK(str,timeout)RELEASE_LOCK(str)IS_USED_LOCK(str)IS_FREE_LOCK(str)
• TABLE,ROW 단위가 아닌 Client간의 String에 대한 Lock
• Client들은 동일한 String에 Lock을 획득할 수 없음.
• GET_LOCK() : 해당 Lock 획득(획득에 성공하면 1리턴 실패하면 2를 리턴)
• RELEASE_LOCK() : 해당 Lock 해제(해제에 성공하면 1리턴, LOCK이 존재하지 않으면 NULL값 리턴)
• IS_USED_LOCK() : 해당 Lock을 누가 사용 중인지 확인(사용중인 세션의 Connection_id값 반환 or NULL)
• IS_FREE_LOCK() : 특정 String이 사용가능한지 확인(가능하면 1을 리턴, 사용 중이면 0을 리턴, Error발생시 NULL)
• 주로 특정 어플리케이션들에서 서로의 데이터 중복을 확인하기 위해 사용
2-3. User Lock
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
mysql> select connection_id();+-----------------+| connection_id() |+-----------------+| 243397 |+-----------------+
1 row in set (0.00 sec)
mysql> select is_used_lock('locked_string');+-------------------------------+| is_used_lock('locked_string') |+-------------------------------+| NULL |+-------------------------------+
1 row in set (0.00 sec)
mysql> select is_free_lock('locked_string');+-------------------------------+| is_free_lock('locked_string') |+-------------------------------+| 1 |+-------------------------------+
1 row in set (0.00 sec)
mysql> select get_lock('locked_string',600);+-------------------------------+| get_lock('locked_string',600) |+-------------------------------+| 1 |+-------------------------------+
1 row in set (0.00 sec)
2-3. User Lock
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
2-3. User Lock
mysql> select connection_id();+-----------------+| connection_id() |+-----------------+| 243396 |+-----------------+1 row in set (0.00 sec)
mysql> select is_used_lock('locked_string');+-------------------------------+| is_used_lock('locked_string') |+-------------------------------+| 243397 |+-------------------------------+1 row in set (0.00 sec)
mysql> select is_free_lock('locked_string');+-------------------------------+| is_free_lock('locked_string') |+-------------------------------+| 0 |+-------------------------------+1 row in set (0.00 sec)
mysql> select get_lock('locked_string',600);
mysql> show processlist;+--------+------+-----------+--------+---------+------+-----------+--------------------------------------+| Id | User | Host | db | Command | Time | State | Info |+--------+------+-----------+--------+---------+------+-----------+--------------------------------------+| 243396 | root | localhost | exem_i | Query | 5 | User lock | select get_lock('locked_string',600) || 243397 | root | localhost | exem_i | Sleep | 179 | | NULL || 243555 | root | localhost | exem_i | Query | 0 | starting | show processlist |+--------+------+-----------+--------+---------+------+-----------+--------------------------------------+
3 rows in set (0.00 sec)
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.
RENAME TABLE tbl_name1 TO tbl_name2DROP TABLE tbl_name
• 테이블을 RENAME하거나 DROP할 경우 묵시적으로 테이블 명에 Name Lock
• RENAME할 경우 기존이름과 변경될 이름에 모두 Lock획득
• TABLE LOCK과 호환성 없음
• SHOW OPEN TABLES 커맨드로 Name_lock이 걸린 테이블 확인 가능
mysql> show open tables from exem_i;+----------+--------+--------+-------------+| Database | Table | In_use | Name_locked |+----------+--------+--------+-------------+| exem_i | abc | 0 | 0 || exem_i | t1 | 0 | 0 || exem_i | myisam | 0 | 0 || exem_i | foo | 0 | 0 |+----------+--------+--------+-------------+
4 rows in set (0.00 sec)
2-4. Name Lock
Research & ContentsTHANK YOU
Blog
Video
NAVER http://cafe.naver.com/playexem
ITPUB http://blog.itpub.net/31135309/
Wordpress https://playexem.wordpress.com/
Slideshare http://www.slideshare.net/playexem
Youtube https://www.youtube.com/channel/UC5wKR_-A0eL_Pn_EMzoauJg
Tudou http://www.tudou.com/home/maxgauge/
교육 문의: 연구컨텐츠팀 김숙진
© Copyrights 2001~2016, EXEM CO.,LTD. All Rights Reserved.