PostgreSQL Index Tuning and Performance Optimization
SQL
|
Tuesday, 26 July 2016
PostgreSQL is an object-relational database (ORDBMS) – i.e. an RDBMS, with additional (optional use) “object” features – with an emphasis on extensibility and standards compliance. As a database server, its primary functions are to store data securely and return that data in response to requests from other software applications.
The DVDRental Postgres Sample DB
First, download the file and put it somewhere on your drive. It’s a TAR file (an archive) and you’ll use it directly when loading.
To load this up, do this:
This will create the sample DB. Then, you’ll need to run pg_restore.
If the bin is in your path, you should be able to run pg_restore straight away.
Now, run it:
That’s it - you should be good to go.
EXPLAIN
EXPLAIN keyword shows the execution plan of a statement with related information
EXPLAIN ANALYZE
Improving Query Performance with Indexes
Index is a specific structure that organizes data and references to the data in such a manner that it is easier as well as faster to look up associated relevant data.
B-Tree Index
Use with:
Equality Queries (<, <=, =, >=, >)
Range Queries (BETWEEN, IN)
Advantages:
Retrieving faster data
Faster SELECT queries
Disadvantages:
Inserting slower data
Slower INSERT, UPDATE, DELETE queries
Creating Index
Creating Multicolumn Index (maximum 32 columns)
Cover Index - Index containing all columns needed for a query
Unique Index
Index used to enforce uniqueness of a column’s value, or the uniqueness of the combined values of more than one column is known as Unique Index.