search
HomeDatabaseMysql TutorialGPDB管理员笔记(一)数据库对象

GPDB管理员笔记(一)数据库对象

Jun 07, 2016 pm 03:29 PM
objectdatabasenotesmanageadministrator

数据库对象管理1、创建数据库 create database new_dbname; createdb -h localhost -p 5432 mydb2、克隆数据库 3、查看数据libo=# \l List of databases Name | Owner | Encoding | Access privileges --------------------------------------------------- l

数据库对象管理 1、创建数据库 create database new_dbname; createdb -h localhost -p 5432 mydb 2、克隆数据库 3、查看数据 libo=# \l
List of databases
Name | Owner | Encoding | Access privileges
-----------+---------+----------+---------------------
libo | gpadmin | UTF8 |
postgres | gpadmin | UTF8 |
template0 | gpadmin | UTF8 | =c/gpadmin
: gpadmin=CTc/gpadmin
template1 | gpadmin | UTF8 | =c/gpadmin
: gpadmin=CTc/gpadmin
(4 rows)
select * from pg_database;
4、数据库属性变更 libo=# alter database libo owner to libo;
ALTER DATABASE
libo=# \l
List of databases
Name | Owner | Encoding | Access privileges
-----------+---------+----------+---------------------
libo | libo | UTF8 |
postgres | gpadmin | UTF8 |
template0 | gpadmin | UTF8 | =c/gpadmin
: gpadmin=CTc/gpadmin
template1 | gpadmin | UTF8 | =c/gpadmin
: gpadmin=CTc/gpadmin
(4 rows)
5、使用gpfilespace创建文件系统 [gpadmin@mdw ~]$ gpfilespace -o gpfilespace_config
20140303:10:43:03:012223 gpfilespace:mdw:gpadmin-[INFO]:-
A tablespace requires a file system location to store its database
files. A filespace is a collection of file system locations for all components
in a Greenplum system (primary segment, mirror segment and master instances).
Once a filespace is created, it can be used by one or more tablespaces.


20140303:10:43:03:012223 gpfilespace:mdw:gpadmin-[INFO]:-getting config
Enter a name for this filespace
> libodisk

Checking your configuration:
Your system has 2 hosts with 2 primary and 2 mirror segments per host.
Your system has 1 hosts with 0 primary and 0 mirror segments per host.

Configuring hosts: [sdw2, sdw1]

Please specify 2 locations for the primary segments, one per line:
primary location 1> /home/gpadmin/GPDB/data/d1
[Error] sdw2: /home/gpadmin/GPDB/data/d1/gpseg2 - Directory conflicts with existing datadir
primary location 1> /home/gpadmin/GPDB/data/d1
[Error] sdw2: /home/gpadmin/GPDB/data/d1/gpseg2 - Directory conflicts with existing datadir
primary location 1> /home/gpadmin/GPDB/data/d1
[Error] sdw2: /home/gpadmin/GPDB/data/d1/gpseg2 - Directory conflicts with existing datadir
primary location 1> /home/gpadmin/GPDB/data/d2
[Error] sdw2: /home/gpadmin/GPDB/data/d2/gpseg3 - Directory conflicts with existing datadir
primary location 1>
primary location 1>
primary location 1>
primary location 1>
primary location 1>
primary location 1> /home/gpadmin/GPDB/data/d1/gpseg0
[Error] sdw2: /home/gpadmin/GPDB/data/d1/gpseg0 : No such file or directory

primary location 1> /home/gpadmin/GPDB/data/d1/gpseg2
[Error] sdw2: /home/gpadmin/GPDB/data/d1/gpseg2/gpseg2 - Subdirectory of existing datadir
primary location 1> /home/gpadmin/GPDB/data/d1/
[Error] sdw2: /home/gpadmin/GPDB/data/d1/gpseg2 - Directory conflicts with existing datadir
primary location 1> /home/gpadmin/GPDB/data/d3
[Error] sdw2: /home/gpadmin/GPDB/data/d3 : No such file or directory

primary location 1> /home/gpadmin/GPDB/data/d3
[Error] sdw1: /home/gpadmin/GPDB/data/d3 : No such file or directory

primary location 1> /home/gpadmin/GPDB/data/d3
primary location 2> /home/gpadmin/GPDB/data/d3

Please specify 2 locations for the mirror segments, one per line:
mirror location 1> /home/gpadmin/GPDB/data/m3
mirror location 2> /home/gpadmin/GPDB/data/m3

Configuring hosts: [mdw]

Enter a file system location for the master
master location> /home/gpadmin/GPDB/data/master
20140303:10:51:07:012223 gpfilespace:mdw:gpadmin-[INFO]:-Creating configuration file...
20140303:10:51:07:012223 gpfilespace:mdw:gpadmin-[INFO]:-[created]
20140303:10:51:07:012223 gpfilespace:mdw:gpadmin-[INFO]:-
To add this filespace to the database please run the command:
gpfilespace --config /home/gpadmin/gpfilespace_config

[gpadmin@mdw ~]$ gpfilespace -c gpfilespace_config
20140303:10:51:29:012482 gpfilespace:mdw:gpadmin-[INFO]:-
A tablespace requires a file system location to store its database
files. A filespace is a collection of file system locations for all components
in a Greenplum system (primary segment, mirror segment and master instances).
Once a filespace is created, it can be used by one or more tablespaces.


20140303:10:51:30:012482 gpfilespace:mdw:gpadmin-[INFO]:-getting config
Reading Configuration file: 'gpfilespace_config'
20140303:10:51:30:012482 gpfilespace:mdw:gpadmin-[INFO]:-Performing validation on paths
..............................................................................

20140303:10:51:30:012482 gpfilespace:mdw:gpadmin-[INFO]:-Connecting to database
20140303:10:51:31:012482 gpfilespace:mdw:gpadmin-[INFO]:-Filespace "libodisk" successfully created
创建表空间 libo=# create tablespace libospace filespace libodisk;
CREATE TABLESPACE
libo=# grant create on tablespace libospace to libo;
GRANT
libo=# set default_tablespace=libospace;
SET
libo=# create table test (id int);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE
libo=# drop table test;
DROP TABLE
libo=# create table test (i int);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE

查看现有表空间和空间文件: SELECT spcname as tblspc, fsname as filespc, fsedbid as seg_dbid, fselocation as datadir
FROM pg_tablespace pgts, pg_filespace pgfs, pg_filespace_entry pgfse
WHERE pgts.spcfsoid=pgfse.fsefsoid AND pgfse.fsefsoid=pgfs.oid ORDER BY tblspc, seg_dbid;
tblspc | filespc | seg_dbid | datadir
------------+-----------+----------+----------------------------------------
libospace | libodisk | 1 | /home/gpadmin/GPDB/data/master/gpseg-1
libospace | libodisk | 2 | /home/gpadmin/GPDB/data/d3/gpseg0
libospace | libodisk | 3 | /home/gpadmin/GPDB/data/d3/gpseg1
libospace | libodisk | 4 | /home/gpadmin/GPDB/data/d3/gpseg2
libospace | libodisk | 5 | /home/gpadmin/GPDB/data/d3/gpseg3
libospace | libodisk | 6 | /home/gpadmin/GPDB/data/m3/gpseg0
libospace | libodisk | 7 | /home/gpadmin/GPDB/data/m3/gpseg1
libospace | libodisk | 8 | /home/gpadmin/GPDB/data/m3/gpseg2
libospace | libodisk | 9 | /home/gpadmin/GPDB/data/m3/gpseg3
pg_default | pg_system | 1 | /home/gpadmin/GPDB/data/gpseg-1
pg_default | pg_system | 2 | /home/gpadmin/GPDB/data/d1/gpseg0
pg_default | pg_system | 3 | /home/gpadmin/GPDB/data/d2/gpseg1
pg_default | pg_system | 4 | /home/gpadmin/GPDB/data/d1/gpseg2
pg_default | pg_system | 5 | /home/gpadmin/GPDB/data/d2/gpseg3
pg_default | pg_system | 6 | /home/gpadmin/GPDB/data/m1/gpseg0
pg_default | pg_system | 7 | /home/gpadmin/GPDB/data/m2/gpseg1
pg_default | pg_system | 8 | /home/gpadmin/GPDB/data/m1/gpseg2
pg_default | pg_system | 9 | /home/gpadmin/GPDB/data/m2/gpseg3
pg_global | pg_system | 1 | /home/gpadmin/GPDB/data/gpseg-1
pg_global | pg_system | 2 | /home/gpadmin/GPDB/data/d1/gpseg0
pg_global | pg_system | 3 | /home/gpadmin/GPDB/data/d2/gpseg1
pg_global | pg_system | 4 | /home/gpadmin/GPDB/data/d1/gpseg2
pg_global | pg_system | 5 | /home/gpadmin/GPDB/data/d2/gpseg3
pg_global | pg_system | 6 | /home/gpadmin/GPDB/data/m1/gpseg0
pg_global | pg_system | 7 | /home/gpadmin/GPDB/data/m2/gpseg1
pg_global | pg_system | 8 | /home/gpadmin/GPDB/data/m1/gpseg2
pg_global | pg_system | 9 | /home/gpadmin/GPDB/data/m2/gpseg3
(27 rows)
查看当前的shema libo=# select current_schema();
current_schema
----------------
public
(1 row)

创建表声明分布键 => CREATE TABLE products (name varchar(40), prod_id integer, supplier_id integer)
DISTRIBUTED BY (prod_id);
=> CREATE TABLE random_stuff (things text, doodads text, etc text)
DISTRIBUTED RANDOMLY;
选择表的存储模式
GPDB提供几种灵活的存储处理模式(或者混合模式)。在创建一张新的TABLE
时,有几个选项来决定数据如何储存在磁盘上。本节介绍这几种选项,以及出
于工作负载的考虑如何实现最佳的储存模式。
 选择堆存储(Heap)或只追加(Append-Only/AO)存储
 选择行存储(Row-Orientation)或列存储(Column-Orientation)
 使用压缩(只可以是AO表)
 检查只追加(AO)表的压缩和分布情况
创建列存储表 => CREATE TABLE bar (a int, b text) WITH (appendonly=true, orientation=column)
DISTRIBUTED BY (a);
检查AO表的压缩与分布情况 GP提供了内置的函数用以检查AO表的压缩率和分布情况。这两个函数可以使
用对象ID或者TABLE的NAME作为参数。表名可能需要带模式名限定。
\
压缩率得到的是一个常见的比值类型。比如,3.19的返回值或者3.19:1,意味着
该TABLE未压缩状态下的储存尺寸是压缩下的储存尺寸的3倍多。
分布信息展示的是每个Instance存储该TABLE的ROW数量。例如,在一个有着4
个Instance的系统,其dbid范围为0 – 3,该函数返回类似下面的结果集:
=# SELECT get_ao_distribution("lineitem_comp');
get_ao_distribution
---------------------(0,7500721)
(1,7501365)
(2,7499978)
(3,7497731)
(4 rows)
通过TYPE命令的方式设置压缩配置
一个TYPE可以包含3个压缩参数。关于添加这些参数到TYPE的语法和限制,
参考相关的CREATE TYPE命令。下面的命令使用精简的方式创建压缩 CREATE TABLE t2 (c1 comptype) WITH (APPENDONLY=true, ORIENTATION=column);
这里的comptype的定义为:
CREATE TYPE comptype (
internallength = 4,
input = comptype_in,
output = comptype_out,
alignment = int4,
default = 123,
passedbyvalue,
compresstype="quicklz",
blocksize=65536,
compresslevel=1
);
重分布表数据
对于随机分布策略或者不改变分布策略的表,要重分布TABLE的数据,使用
REORGANIZW=TRUE。这在处理数据倾斜问题时可能是很必要的,在添加新的
Segment节点资源时也是必要的。比如: ALTER TABLE sales SET WITH (REORGANIZE=TRUE);
分区表维护 添加分区 ALTER TABLE sales ADD PARTITION
START (date '2009-02-01') INCLUSIVE
END (date '2009-03-01') EXCLUSIVE; 删除分区 ALTER TABLE sales DROP PARTITION FOR (RANK(1)) 清除分区数据 ALTER TABLE sales TRUNCATE PARTITION FOR (RANK(1)); 交换分区 CREATE TABLE jan08 (LIKE sales) WITH (appendonly=true);
INSERT INTO jan08 SELECT * FROM sales_1_prt_1 ;
ALTER TABLE sales EXCHANGE PARTITION FOR (DATE '2008-01-01') WITH TABLE jan08; 拆分分区 例如,将一个月分区数据拆分到一个1-15日的分区和另一个16-31日的分区:
ALTER TABLE sales SPLIT PARTITION FOR ('2008-01-01')
AT ('2008-01-16')
INTO (PARTITION jan081to15, PARTITION jan0816to31)
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),