15 Advanced PostgreSQL Commands with Examples
转自:http://www.thegeekstuff.com/2009/04/15-practical-postgresql-database-adminstration-commands/ 1. 如何找到postgreSQL数据库中占空间最大的表?$ /usr/local/pgsql/bin/psql test Welcome to psql 8.3.7,the PostgreSQL interactive terminal. Type: copyright for distribution terms h for help with SQL commands ? for help with psql commands g or terminate with semicolon to execute query q to quit test=# SELECT relname,relpages FROM pg_class ORDER BY relpages DESC; relname | relpages -----------------------------------+---------- pg_proc | 50 pg_proc_proname_args_nsp_index | 40 pg_depend | 37 pg_attribute | 30 如果你只想要最大的那个表,可以用limit参数来限制结果的数量,就像这样: # SELECT relname,relpages FROM pg_class ORDER BY relpages DESC limit 1; relname | relpages ---------+---------- pg_proc | 50 (1 row)
2. 如何计算postgreSQL数据库所占用的硬盘大小?pg_database_size这个方法是专门用来查询数据库大小的,它返回的结果单位是字节(bytes)。: # SELECT pg_database_size('geekdb'); pg_database_size ------------------ 63287944 (1 row) 如果你想要让结果更直观一点,那就使用**pg_size_pretty**方法,它可以把字节数转换成更友好易读的格式。 # SELECT pg_size_pretty(pg_database_size('geekdb')); pg_size_pretty ---------------- 60 MB (1 row) 3. 如何计算postgreSQL表所占用的硬盘大小?下面这个命令查出来的表大小是包含索引和toasted data的,如果你对除去索引外仅仅是表占的大小感兴趣,可以 使用后面提供的那个命令。 # SELECT pg_size_pretty(pg_total_relation_size('big_table')); pg_size_pretty ---------------- 55 MB (1 row) 如何查询不含索引的postgreSQL表的大小? 使用**pg_relation_size**而不是**pg_total_relation_size**方法。 # SELECT pg_size_pretty(pg_relation_size('big_table')); pg_size_pretty ---------------- 38 MB (1 row) 4. 如何查看postgreSQL表的索引?Syntax: # d table_name 让我们看下面这个例子,注意如果你的表有索引的话,你会在命令输出内容的后面那部分找到一个标题Indexes,在这个例子中,pg_attribut表有两个btree类型的索引,默认情况下postgreSQL使用的索引类型都 是btree,因为它适用于绝大多数情况。 test=# d pg_attribute Table "pg_catalog.pg_attribute" Column | Type | Modifiers ---------------+----------+----------- attrelid | oid | not null attname | name | not null atttypid | oid | not null attstattarget | integer | not null attlen | smallint | not null attnum | smallint | not null attndims | integer | not null attcacheoff | integer | not null atttypmod | integer | not null attbyval | boolean | not null attstorage | "char" | not null attalign | "char" | not null attnotnull | boolean | not null atthasdef | boolean | not null attisdropped | boolean | not null attislocal | boolean | not null attinhcount | integer | not null Indexes: "pg_attribute_relid_attnam_index" UNIQUE,btree (attrelid,attname) "pg_attribute_relid_attnum_index" UNIQUE,attnum) 5. 如何创建一个指定类型的索引?默认情况下的索引都是btree类型的,但是你可以用下面的方法来指定新索引的类型。 Syntax: CREATE INDEX name ON table USING index_type (column); # CREATE INDEX test_index ON numbers using hash (num); 6. 如何在postgreSQL中使用事务?如何开始一个事务? # BEGIN -- 开始事务 如何提交或回滚一个事务? 只有当你调用COMMIT命令后,你在BEGIN命令后所做的所有操作才会真正的被提交到postgreSQL数据库。另外你还 可以使用ROLLBACK命令来回滚事务中做的所有操作。 # ROLLBACK -- 回滚当前事务 # COMMIT -- 提交当前事务 7. 如何查看postgreSQL数据库对某个SQL查询的执行计划?# EXPLAIN query; 8. 如何通过在服务端执行查询来显示执行计划?下面这个命令会在服务器端执行查询,但是并不会把查询结果给用户,而是返回它实际的执行计划。 # EXPLAIN ANALYZE query; 9. 怎么生成一个序列的数字并把它们插入到一个表中?下面这个命令将会生成1到1000这一千个数字并插入到numbers表中。 # INSERT INTO numbers (num) VALUES ( generate_series(1,1000)); 10. 如何统计postgreSQL表里面的行数?这个命令可以查询出表里所有记录的条数。 # select count(*) from table; 这个命令会查询出表中指定列的值不为空的所有行数. # select count(col_name) from table; 这个命令会查询出表中按制定列的值去重后的总行数。 # select count(distinct col_name) from table; 11. 如何查询表中某列**第二大**的值?查询某列最大的值 # select max(col_name) from table; 查询某列中第二大的值 # SELECT MAX(num) from number_table where num < ( select MAX(num) from number_table ); 12. 如何查询表中某列**第二小**的值?查询某列最小的值 # select min(col_name) from table; 查询某列第二小的值 # SELECT MIN(num) from number_table where num > ( select MIN(num) from number_table ); 13. 如何列出postgreSQL数据库中基本的数据类型?下面截取了部分内容,这个命令可以展示可用的数据类型和它们所占用的字节数。 test=# SELECT typname,typlen from pg_type where typtype='b'; typname | typlen ----------------+-------- bool | 1 bytea | -1 char | 1 name | 64 int8 | 8 int2 | 2 int2vector | -1
14. 如何把某一次查询的结果保存为一个文件?# o output_file # SELECT * FROM pg_class; 上面这个查询的结果将会被保存到到"output_file"文件中。当重定向被激活后,之后的所有查询都不再会把结果 打印在屏幕上了。如果要再次打开屏幕输出,需要再执行一次不带任何参数的 o 命令。 # o 我们之前的文章还有提到过,你可以使用pg_dump和psql来备份和恢复你的数据库 15. 存储加密后的密码PostgreSQL数据库可以使用下面的crypt命令来加密数据。这可以用来方便的用来保存你的用户名和密码。 # SELECT crypt ( 'sathiya',gen_salt('md5') ); PostgreSQL crypt方法可能存在的问题: crypt在你的环境下可能会用不了,并提供下面这个报错信息。 ERROR: function gen_salt("unknown") does not exist HINT: No function matches the given name and argument types. You may need to add explicit type casts. 解决方法: 为了解决这个问题,你需要安装 postgresql-contrib-版本 这个包,然后在psql中执行下面这个命令。 # i /usr/share/postgresql/8.1/contrib/pgcrypto.sql 原文后的评论在第13个命令中,那个typtype='b'是什么意思?typtype='b'表示basetype。b==basetype. PostgreSQL有这么几种数据类型: composite types,domains,and pseudo-types. http://developer.postgresql.org/pgdocs/postgres/extend-type-system.html 获取第二大/小的值效率问题如果要查询一个表中某列第二小的值,这样查询要快得多: SELECT m FROM mytable ORDER BY m LIMIT 1 OFFSET 1; 如果m列有索引的话。 COUNT(*)效率问题在大表上执行count(*)会有比较明显的效率问题 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |