7. Create an index

7.1. Spherical index

pgSphere uses GiST to create spherical indices. An index speeds up the executation time of operators @, &&, #, =, and !=. You can create an index with following spherical data types:

Example 49. Simple index of spherical points


CREATE TABLE test (
  pos spoint NOT NULL
);
-- Put in data now
CREATE INDEX test_pos_idx USING GIST ON test (pos);
VACUUM ANALYZE test;
          

7.2. Optimizing

There are several ways to speed up the performance of an index. One way is clustering. pgSphere provides a “cluster index”. It's just a B-tree index to allow sorting of the table data. It's recommended to create such an index before you create the spherical index. You can then drop the “cluster index”.

Example 50. An optimized spherical index


CREATE TABLE test (
  pos spoint NOT NULL
);
CREATE INDEX test_pos_tmp_idx ON test( pos );
-- put in data now
CLUSTER test_pos_tmp_idx ON test;
DROP INDEX test_pos_tmp_idx;
CREATE INDEX test_pos_idx USING GIST ON test (pos);
VACUUM ANALYZE test;

          

If your table is rareley updated, you don't need to do it again. Otherwise, do clustering from regularly.

Note: Clustering needs a lot of disk space and can take a lot of time. It depends on your system's performance and the size of your data.