标签:
1) GP使用多版本控制模型(Mutltiversion Concurrency Control/MVCC)保持数据一致性;
可将MVCC看成行级别锁的一种妥协,它在许多情况下避免了使用锁,同时可以提供更小的开销。根据实现的不同,它可以允许非阻塞式读,在写操作进行时只锁定必要的记录。MVCC会保存某个时间点上的数据快照。这意味着事务可以看到一个一致的数据视图,不管他们需要跑多久。这同时也意味着不同的事务在同一个时间点看到的同一个表的数据可能是不同的。如果你从来没有过种体验的话,可能理解起来比较抽象,但是随着慢慢地熟悉这种理解将会很容易。
2) MVCC以避免给数据库事务显式锁定的方式,最大化减少锁争用以确保多用户环境下的性能;
3) GP提供了各种锁机制来控制对表数据的并发访问;
4) GP为每个事务提供事务隔离;
5) GP的锁模式:
锁模式 |
相关SQL命令 |
冲突的锁 |
ACCESS SHARE |
SELECT |
ACCESS EXCLUSIVE |
ROW SHARE |
SELECT FOR UPDATE, SELECT FOR SHARE |
EXCLUSIVE, ACCESS EXCLUSIVE |
ROW EXCLUSIVE |
INSERT, COPY |
SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, ACCESS EXCLUSIVE |
SHARE UPDATE EXCLUSIVE |
VACUUM (without FULL), ANALYZE |
SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, ACCESS EXCLUSIVE |
SHARE |
CREATE INDEX |
ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE ROW EXCLUSIVE, EXCLUSIVE, ACCESS EXCLUSIVE |
SHARE ROW EXCLUSIVE |
ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, ACCESS EXCLUSIVE |
|
EXCLUSIVE |
DELETE, UPDATE |
ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, ACCESS EXCLUSIVE |
ACCESS EXCLUSIVE |
ALTER TABLE, DROP TABLE, TRUNCATE, REINDEX, CLUSTER, VACUUM FULL |
ACCESS SHARE, ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, ACCESS EXCLUSIVE |
1) 需要表名和该表每列的值使用INSERT命令插入数据,GP是自动提交;
2) 显式的指定列名插入数据,当不知道当前表中列名时;
3) 从另一个表中获取并插入到当前表;
4) 使用一个命令插入多条记录,AO表为批量装载做了优化,不建议在AO表上使用单条的INSERT语句。
devdw=# \h insert 查看当前insert命令的帮助 Command: INSERT Description: create new rows in a table Syntax: INSERT INTO table [ ( column [, ...] ) ] { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query } [ RETURNING * | output_expression [ AS output_name ] [, ...] ]
devdw=# \d tb_cp_02 Table "public.tb_cp_02" Column | Type | Modifiers --------+--------------+----------- id | integer | rank | integer | year | integer | gender | character(1) | count | integer | Indexes: "bmidx_01" bitmap (count) Number of child tables: 5 (Use \d+ to list them.) Distributed by: (id)
devdw=# insert into tb_cp_02(id,rank,year,gender,count) values (1,2,3,‘M‘,4); 往tb_cp_02表中逐个字段的添加数据 INSERT 0 1
devdw=# select * from tb_cp_02; 查看当前tb_cp_02表中数据 id | rank | year | gender | count ----+------+------+--------+------- 1 | 2 | 3 | M | 4 (1 row)
devdw=# insert into tb_cp_02(rank,year,gender,count,id) values (3,4,‘W‘,5,2); 往tb_cp_02表中随机字段的添加数据 INSERT 0 1 devdw=# select * from tb_cp_02; id | rank | year | gender | count ----+------+------+--------+------- 1 | 2 | 3 | M | 4 2 | 3 | 4 | W | 5 (2 rows) devdw=# insert into tb_cp_02 select * from tb_cp_02; 通过查询tb_cp_02表中数据后再往其中插入数据 INSERT 0 2 devdw=# select * from tb_cp_02; id | rank | year | gender | count ----+------+------+--------+------- 1 | 2 | 3 | M | 4 1 | 2 | 3 | M | 4 2 | 3 | 4 | W | 5 2 | 3 | 4 | W | 5 (4 rows) |
Command: UPDATE 查看update的帮助 Description: update rows of a table Syntax: UPDATE [ ONLY ] table [ [ AS ] alias ] SET { column = { expression | DEFAULT } | ( column [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...] [ FROM fromlist ] [ WHERE condition ] [ RETURNING * | output_expression [ AS output_name ] [, ...] ] |
1) 更新是对数据库的现有数据进行修改,可修改全部或部分记录;
2) 每个列都可以被单独的更新,而不影响其他列;
3) 要执行更新,需要如下3方面的消息:
4) 使用UPDATE命令更新表中的记录;
5) GP中使用UPDATE的限制:GP的DK不可以被UPDATE;
1) 使用DELETE命令从指定的表中删除符合WHERE条件的记录,或者删除表中所有记录;
2) 使用TRUNCATE命令快速删除所有记录。
devdw=# \h delete 查看delete命令的帮助 Command: DELETE Description: delete rows of a table Syntax: DELETE FROM [ ONLY ] table [ [ AS ] alias ] [ USING usinglist ] [ WHERE condition ] [ RETURNING * | output_expression [ AS output_name ] [, ...] ]
devdw=# \h truncate 查看truncate命令的帮助 Command: TRUNCATE Description: empty a table or set of tables Syntax: TRUNCATE [ TABLE ] name [, ...] [ CASCADE | RESTRICT ] |
1) 事务允许将多个SQL语句放在一起作为一个整体操作,所有SQL一起成功或失败
2) 使用事务,在GP中执行事务的SQL命令:
3) 事务隔离级别:SQL标准定义了4个事务隔离级别:
依次查看begin/end/rollback/commit/savepoint的帮助说明 devdw=# \h begin Command: BEGIN Description: start a transaction block Syntax: BEGIN [ WORK | TRANSACTION ] [ transaction_mode [, ...] ]
where transaction_mode is one of:
ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED } READ WRITE | READ ONLY
devdw=# \h end Command: END Description: commit the current transaction Syntax: END [ WORK | TRANSACTION ]
devdw=# \h rollback Command: ROLLBACK Description: abort the current transaction Syntax: ROLLBACK [ WORK | TRANSACTION ]
devdw=# \h commit Command: COMMIT Description: commit the current transaction Syntax: COMMIT [ WORK | TRANSACTION ]
devdw=# \h savepoint Command: SAVEPOINT Description: define a new savepoint within the current transaction Syntax: SAVEPOINT savepoint_name
devdw=# begin; 开启事务 BEGIN devdw=# insert into tb_cp_02 values(5,6,7,‘G‘,8); 向tb_cp_02表中增加数据 INSERT 0 1 devdw=# commit; 提交 COMMIT devdw=# end; 结束事务 WARNING: there is no transaction in progress COMMIT devdw=# select * from tb_cp_02; id | rank | year | gender | count ----+------+------+--------+------- 1 | 2 | 3 | M | 4 1 | 2 | 3 | M | 4 5 | 6 | 7 | G | 8 2 | 3 | 4 | W | 5 2 | 3 | 4 | W | 5 (5 rows)
devdw=# begin; 开启事务 BEGIN devdw=# insert into tb_cp_02 values(5,6,7,‘G‘,8); 向tb_cp_02表中增加数据 INSERT 0 1 devdw=# rollback; 回滚事务 ROLLBACK devdw=# end; 结束事务 WARNING: there is no transaction in progress COMMIT devdw=# select * from tb_cp_02; id | rank | year | gender | count ----+------+------+--------+------- 1 | 2 | 3 | M | 4 1 | 2 | 3 | M | 4 5 | 6 | 7 | G | 8 2 | 3 | 4 | W | 5 2 | 3 | 4 | W | 5 (5 rows)
devdw=# begin; 开启事务 BEGIN devdw=# insert into tb_cp_02 values(5,6,7,‘G‘,8); 向tb_cp_02表中增加数据 INSERT 0 1 devdw=# savepoint a; 创建保存点a SAVEPOINT devdw=# insert into tb_cp_02 values(78,6,7,‘G‘,8); 再次向tb_cp_02表中增加数据 INSERT 0 1 devdw=# rollback to a; 回滚到保存点a ROLLBACK devdw=# end; 结束事务 此时第二条数据是没有插入成功的 COMMIT devdw=# select * from tb_cp_02; id | rank | year | gender | count ----+------+------+--------+------- 1 | 2 | 3 | M | 4 1 | 2 | 3 | M | 4 5 | 6 | 7 | G | 8 5 | 6 | 7 | G | 8 2 | 3 | 4 | W | 5 2 | 3 | 4 | W | 5 (6 rows) |
1) 事务ID管理:系统目录维护在每个数据库每在每个数据库每2百万个事务的时候,对每张表执行VACUUM是很有必要的。
2) 系统目录维护:大量的CREATE和DROP命令会导致系统表的迅速膨胀,以至于影响系统性能。
由于MVCC事务并发模型的原因,已经删除或者更新的记录仍然占据着磁盘空间。
如果数据库有大量的更新和删除操作,将会产生大量的过期记录
定期的运行VACUUM命令可以删除过期记录,回收空间。
devdw=# \h vacuum 查看vacuum的帮助 Command: VACUUM Description: garbage-collect and optionally analyze a database 垃圾回收并且可选择性的分析一个数据库 Syntax: VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table ] VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]
devdw=# vacuum full; 分析当前数据库 NOTICE: ‘VACUUM FULL‘ is not safe for large tables and has been known to yield unpredictable runtimes. HINT: Use ‘VACUUM‘ instead. ‘VACUUM FULL‘这个命令对于大的表是不安全的且花费的时间也不可预计,建议使用‘VACUUM’替换 VACUUM |
1) 配置子空间映射
2) 为优化查询进行回收和分析
GP使用基于成本的查询优化器,ANALYZE命令收集查询优化器需要的统计信息,VACUUM ANALYZE可以一起执行;
3) 对于B-tree索引,新重建的索引比存在较多更新的索引更快;
4) 重建索引可以回收过期的空间;
5) 在GP中,删除索引然后创建通常比REINDEX更快,当更新索引列时,Bitmap索引不会被更新;
1) 数据库服务日志文件
# |
Field Name |
Data Type |
Description |
1 |
event_time |
timestamp with time zone |
Time that the log entry was written to the log |
2 |
user_name |
varchar(100) |
The database user name |
3 |
database_name |
varchar(100) |
The database name |
4 |
process_id |
varchar(10) |
The system process id (prefixed with "p") |
5 |
thread_id |
varchar(50) |
The thread count (prefixed with "th") |
6 |
remote_host |
varchar(100) |
On the master, the hostname/address of the client machine. On the segment, the hostname/address of the master. |
7 |
remote_port |
varchar(10) |
The segment or master port number |
8 |
session_start_time |
timestamp with time zone |
Time session connection was opened |
9 |
transaction_id |
Int |
Top-level transaction ID on the master. This ID is the parent of any subtransactions. |
10 |
gp_session_id |
text |
Session identifier number (prefixed with "con") |
11 |
gp_command_count |
text |
The command number within a session (prefixed with "cmd") |
12 |
gp_segment |
text |
The segment content identifier (prefixed with "seg" for primaries or "mir" for mirrors). The master always has a content id of -1. |
13 |
slice_id |
text |
The slice id (portion of the query plan being executed) |
14 |
distr_tranx_id |
text |
Distributed transaction ID |
15 |
local_tranx_id |
text |
Local transaction ID |
16 |
sub_tranx_id |
text |
Subtransaction ID |
17 |
event_severity |
varchar(10) |
Values include: LOG, ERROR, FATAL, PANIC, DEBUG1, DEBUG2 |
18 |
sql_state_code |
varchar(10) |
SQL state code associated with the log message |
19 |
event_message |
text |
Log or error message text |
20 |
event_detail |
text |
Detail message text associated with an error or warning message |
21 |
event_hint |
text |
Hint message text associated with an error or warning message |
22 |
internal_query |
text |
The internally-generated query text |
23 |
internal_query_pos |
Int |
The cursor index into the internally-generated query text |
24 |
event_context |
text |
The context in which this message gets generated |
25 |
debug_query_string |
text |
User-supplied query string with full detail for debugging. This string can be modified for internal use. |
26 |
error_cursor_pos |
Int |
The cursor index into the query string |
27 |
func_name |
text |
The function in which this message is generated |
28 |
file_name |
text |
The internal code file where the message originated |
29 |
file_line |
Int |
The line of the code file where the message originated |
30 |
stack_trace |
text |
Stack trace text associated with this message |
2) 搜索数据库服务日志文件
3) 程序日志文件
标签:
原文地址:http://blog.csdn.net/mavs41/article/details/44926623