Download presentation
Presentation is loading. Please wait.
1
Serial Number and Indexing in PostgreSQL
University of Tehran ECE Department Database Lab.
2
Serial Number Definition
CREATE TABLE Test ( id SERIAL PRIMARY KEY, name character varying (20) ) INSERT INTO Test ("name") values ('Ali'); Incorrect: INSERT INTO Test values ('Ali');
3
Serial Data Types Data Type Storage Size Range smallserial 2 bytes
bigserial 8 bytes 1 to
4
Another method for Serial Number Definition
CREATE SEQUENCE Test_id_seq; CREATE TABLE tablename ( id integer NOT NULL DEFAULT nextval(' Test_id_seq ') ); ALTER SEQUENCE Test_id_seq OWNED BY Test.id;
5
Indexing in PostgreSQL
6
CREATE TABLE test1 ( id integer, content varchar ); SELECT content FROM test1 WHERE id = 10; CREATE INDEX test1_id_index ON test1 (id);
7
Index Types PostgreSQL provides several index types:
B-tree Hash GiST SP-GiST GIN Each index type uses a different algorithm that is best suited to different types of queries. By default, the CREATE INDEX command creates B- tree indexes, which fit the most common situations.
8
B+Tree Usage equality and range queries < <= = >= >
BETWEEN and IN IS NULL or IS NOT NULL pattern matching operators LIKE and ~ if the pattern is a constant and is anchored to the beginning of the string for example, col LIKE 'foo%' or col ~ '^foo', but not col LIKE '%bar'. can also be used to retrieve data in sorted order. This is not always faster than a simple scan and sort, but it is often helpful.
9
Hash Usage CREATE INDEX name ON table USING hash (column);
Hash indexes can only handle simple equality comparisons. = operator.
10
GiST Usage not a single kind of index, but rather an infrastructure within which many different indexing strategies can be implemented. the particular operators with which a GiST PostgreSQL includes GiST operator classes for several two-dimensional geometric data types, which support indexed queries using these operators: << &< &> >> <<| &<| |&> |>> @> ~= &&
11
Example GiST indexes are also capable of optimizing "nearest-neighbor" searches, such as SELECT * FROM places ORDER BY location <- > point '(101,456)' LIMIT 10; which finds the ten places closest to a given target point. The ability to do this is again dependent on the particular operator class being used.
12
SP-GiST like GiST indexes
offer an infrastructure that supports various kinds of searches. SP-GiST permits implementation of a wide range of different non-balanced disk-based data structures, such as quadtrees, k-d trees, and radix trees (tries). PostgreSQL includes SP-GiST operator classes for two-dimensional points: << >> ~= <^ >^
13
GIN GIN indexes are inverted indexes which can handle values that contain more than one key, arrays for example. Like GiST and SP-GiST, GIN can support many different user-defined indexing strategies and the particular operators classes for one- dimensional arrays @> = &&
14
PostGIS Spatial Database
15
PostGIS Example CREATE TABLE "public". "GASSTATION"("gid" SERIAL PRIMARY KEY, "gs_id" float,"name" varchar(48),"type" varchar(16) ) SELECT AddGeometryColumn('public','GASSTATION','geom',4326,'POI NT',2) INSERT INTO "public". "GASSTATION" ( "gs_id","name","type","geom") VALUES (' e+07', 'AmirAbaad','GAS', st_geometryfromtext('POINT ( )',4326))
16
PostGIS Example SELECT "name", st_distance("geom", st_geometryfromtext('POINT( )',4326)) FROM "public". "GASSTATION" SELECT * FROM "public". "GASSTATION" WHERE st_distance("geom", st_geometryfromtext('POINT( )',4326)) < 10
17
Create GiST index in PostGIS
CREATE INDEX gs_index ON "public"."GASSTATION" USING GIST (geom); SELECT * FROM "public"."GASSTATION" WHERE _ST_Contains(geom, st_geometryfromtext('POLYGON((0 0, 0 10, , 10 0, 0 0))',4326))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.