0% found this document useful (0 votes)
13 views

Estructuras Postgre SQL2

Uploaded by

Alberto Rojas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Estructuras Postgre SQL2

Uploaded by

Alberto Rojas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Indexes

PostgreSQL Stock Indexes


• B-Tree
B-Tree is a general-purpose index common in relational databases

• GiST - Generalized Search Tree (GiST)


Is an index optimized for full-text search, spatial data, scientific data,
unstructured data, and hierarchical data.

• GIN - Generalized Inverted Index (GIN)


Is geared toward the built-in full text search and jsonb data type of PostgreSQL.

Elaborado por Prof. Josué Ramírez


Indexes
• SP-GiST - Space-Partitioning Trees Generalized Search Tree (SP-GiST)
Introduced in version 9.2, can be used in the same situations as GiST but can be
faster for certain kinds of data distribution.
• hash
Hash indexes were popular prior to the advent of GiST and GIN.

Elaborado por Prof. Josué Ramírez


Indexes
• Functional Indexes
PostgreSQL lets you add indexes to functions of columns. Example:
Example: CREATE INDEX fidx ON featnames_short USING btree
(upper(fullname) varchar_pattern_ops);

• Partial Indexes
Partial indexes (sometimes called filtered indexes) are indexes that cover only
rows fitting a predefined WHERE condition.
The resulting indexes can be faster because more of them can fit into RAM, plus
you’ll save a bit of disk space on the index itself.
Example:
CREATE TABLE subscribers (id serial PRIMARY KEY, name varchar(50) NOT NULL, type
varchar(50), is_active boolean);
CREATE UNIQUE INDEX uq ON subscribers USING btree(lower(name)) WHERE
is_active;

Elaborado por Prof. Josué Ramírez


Indexes
• Multicolumn Indexes

You can create functional indexes using more than one underlying
column. Example:

Example: CREATE INDEX idx ON subscribers USING btree (type,


upper(name) varchar_pattern_ops);

The PostgreSQL planner uses a strategy called bitmap index scan that
automatically tries to combine indexes on the fly, often from single-
column indexes, to achieve the same goal as a multicolumn index.

Elaborado por Prof. Josué Ramírez


Indexes
• Multicolumn Indexes

If you’re unable to predict how you’ll be querying compound fields in the


future, you may be better off creating single-column indexes and let the
planner decide how to combine them during search.

If you have a compound B-Tree index on type, upper(name) .., then there
is no need for an index on just type, because the planner can happily use
the compound index for cases in which you just need to filter by type.

Elaborado por Prof. Josué Ramírez


Indexes
Building Indexes Concurrently

• Creating an index can interfere with regular operation of a database.

• Normally PostgreSQL locks the table to be indexed against writes and performs
the entire index build with a single scan of the table.

• Other transactions can still read the table, but if they try to insert, update, or
delete rows in the table they will block until the index build is finished.

Elaborado por Prof. Josué Ramírez


Indexes
Building Indexes Concurrently

• This could have a severe effect if the system is a live production database. Very
large tables can take many hours to be indexed, and even for smaller tables, an
index build can lock out writers for periods that are unacceptably long for a
production system.

• PostgreSQL supports building indexes without locking out writes.

• This method is invoked by specifying the CONCURRENTLY option of CREATE


INDEX. When this option is used, PostgreSQL must perform two scans of the
table, and in addition it must wait for all existing transactions that could
potentially modify or use the index to terminate.
Elaborado por Prof. Josué Ramírez
Indexes
Building Indexes Concurrently

• Thus this method requires more total work than a standard index build and
takes significantly longer to complete.

• However, since it allows normal operations to continue while the index is


built, this method is useful for adding new indexes in a production
environment. Of course, the extra CPU and I/O load imposed by the index
creation might slow other operations.

Elaborado por Prof. Josué Ramírez


Indexes
Examples:
• To create a B-tree index on the column title in the table films:
CREATE UNIQUE INDEX title_idx ON films (title);

• To create an index on the expression lower(title), allowing efficient case-


insensitive searches:
CREATE INDEX ON films ((lower(title)));
(In this example we have chosen to omit the index name, so the system will choose a name,
typically films_lower_idx.)

• To create an index with non-default sort ordering of nulls:


CREATE INDEX title_idx_nulls_low ON films (title NULLS FIRST);

Elaborado por Prof. Josué Ramírez


Indexes
• To create an index with non-default fill factor:
CREATE UNIQUE INDEX title_idx ON films (title) WITH (fillfactor = 70);

• To create an index on the column code in the table films and have the
index reside in the tablespace indexspace:
CREATE INDEX code_idx ON films (code) TABLESPACE indexspace;

• To create an index without locking out writes to the table:


CREATE INDEX CONCURRENTLY sales_quantity_index ON sales_table (quantity);

Elaborado por Prof. Josué Ramírez


Bibliography

• PostgreSQL for Data Architects


Jayadevan Maymala
Copyright © 2015 Packt Publishing

• PostgreSQL 9.5.5 Documentation:


https://www.postgresql.org/docs/9.5/static/

Elaborado por Prof. Josué Ramírez

You might also like