7

Temporarily disable all indexes of a postgresql table

 2 years ago
source link: https://fle.github.io/temporarily-disable-all-indexes-of-a-postgresql-table.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Temporarily disable all indexes of a postgresql table

When you run a large query (insert/update) on a huge table with several indexes, these indexes can seriously slow the query execution.

With Postgresql it can be very faster to disable the indexes before runing the query and reindex all the table afterwards.

You can do it like this :

  1. Disable all table indexes
UPDATE pg_index
SET indisready=false
WHERE indrelid = (
    SELECT oid
    FROM pg_class
    WHERE relname='<TABLE_NAME>'
);
  1. Run your query
UPDATE <TABLE_NAME> SET ...;
  1. Reenable all table indexes
UPDATE pg_index
SET indisready=true
WHERE indrelid = (
    SELECT oid
    FROM pg_class
    WHERE relname='<TABLE_NAME>'
);
  1. Reindex table
REINDEX <TABLE_NAME>;

I saved some time with this trick so I just wanted to share it!

__fle__


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK