创建索引的语句如下:
pgdb=# create index supplier_fk1 on supplier(s_nationkey);
CREATE INDEX
时间: 241.663 ms
pgdb=# create index customer_fk1 on customer(c_nationkey);
CREATE INDEX
时间: 4671.995 ms
pgdb=# create index partsupp_fk1 on partsupp(ps_suppkey);
CREATE INDEX
时间: 20420.121 ms
pgdb=#
pgdb=# create index partsupp_fk2 on partsupp(ps_partkey);
CREATE INDEX
时间: 14487.640 ms
pgdb=# create index orders_fk1 on orders(o_custkey);
CREATE INDEX
时间: 38558.701 ms
pgdb=# drop index orders_custkey;
DROP INDEX
时间: 98.454 ms
pgdb=# create index lineitem_fk1 on lineitem(l_orderkey);
CREATE INDEX
时间: 103888.944 ms
pgdb=# create index lineitem_fk2 on lineitem(l_partkey,l_suppkey);
CREATE INDEX
时间: 187944.419 ms
pgdb=# \di
List of relations
Schema | Name | Type | Owner | Table
--------+---------------+-------+----------+----------
public | customer_fk1 | index | postgres | customer
public | customer_pkey | index | postgres | customer
public | lineitem_fk1 | index | postgres | lineitem
public | lineitem_fk2 | index | postgres | lineitem
public | lineitem_pkey | index | postgres | lineitem
public | nation_pkey | index | postgres | nation
public | orders_fk1 | index | postgres | orders
public | orders_pkey | index | postgres | orders
public | part_pkey | index | postgres | part
public | partsupp_fk1 | index | postgres | partsupp
public | partsupp_fk2 | index | postgres | partsupp
public | partsupp_pkey | index | postgres | partsupp
public | region_pkey | index | postgres | region
public | supplier_fk1 | index | postgres | supplier
public | supplier_pkey | index | postgres | supplier
(15 rows)可以用9.0版新引入的pg_relation_size函数获得某个表的空间占用,代替了以往版本的pg_relation_size,后者也依然可以使用。
select pg_table_size('customer');
pg_table_size
---------------
296615936
(1 row)
pgdb=# SELECT nspname || '.' || relname AS "relation",
pgdb-# pg_size_pretty(pg_relation_size(C.oid)) AS "size"
pgdb-# FROM pg_class C
pgdb-# LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
pgdb-# WHERE nspname NOT IN ('pg_catalog', 'information_schema')
pgdb-# ORDER BY pg_relation_size(C.oid) DESC
pgdb-# ;
relation | size
-------------------------------+------------
public.lineitem | 9257 MB
public.orders | 2093 MB
public.partsupp | 1385 MB
public.lineitem_fk2 | 1285 MB
public.lineitem_fk1 | 1285 MB
public.lineitem_pkey | 1285 MB
public.part | 325 MB
public.orders_fk1 | 321 MB
public.orders_pkey | 321 MB
public.customer | 283 MB
public.partsupp_fk1 | 171 MB
public.partsupp_pkey | 171 MB
public.partsupp_fk2 | 171 MB
public.part_pkey | 43 MB
public.customer_fk1 | 32 MB
public.customer_pkey | 32 MB
public.supplier | 18 MB
public.supplier_fk1 | 2208 kB
public.supplier_pkey | 2208 kB
pg_toast.pg_toast_2618 | 208 kB
pg_toast.pg_toast_2619 | 80 kB
pg_toast.pg_toast_2618_index | 16 kB
public.nation_pkey | 16 kB
public.region_pkey | 16 kB
pg_toast.pg_toast_2619_index | 16 kB利用前面第一步产生的查询脚本,执行测试的命令行如下,查询结果输出到不同的log文件,以便于比较,分别在创建索引前后执行,注意在更改表存储和索引类型后,更新统计信息。