'PostgreSQL inherited child tables indexes make select slow
I Have one parent table hotels. These table have 2 child tables:
CREATE TABLE IF NOT EXISTS hotels_active (
CHECK (status_text = 'ACTIVE'),
LIKE hotels INCLUDING ALL
) INHERITS (hotels);
CREATE TABLE IF NOT EXISTS hotels_inactive (
CHECK (status_text <> 'INACTIVE'),
LIKE hotels INCLUDING ALL
) INHERITS (hotels);
After that I create even more child tables based on USA states:
DECLARE
i text;
BEGIN
FOR i IN (SELECT unnest(enum_range(NULL::hotelsdata.StateE))) LOOP
EXECUTE 'CREATE TABLE IF NOT EXISTS hotels_active_state_' || i || ' (
CHECK (state = ''' || i || '''),
LIKE hotels_active INCLUDING ALL
) INHERITS (hotels_active);
;';
EXECUTE 'CREATE TABLE IF NOT EXISTS hotels_inactive_state_' || i || ' (
CHECK (state = ''' || i || '''),
LIKE hotels_inactive INCLUDING ALL
) INHERITS (hotels_inactive);
;';
END LOOP;
END;
I also have indexes on parent hotel table that are inherited to child state tables when we write: LIKE hotels INCLUDING ALL
CREATE INDEX IF NOT EXISTS hotels_column_1_index ON hotels (column_1);
CREATE INDEX IF NOT EXISTS hotels_column_2_index ON hotels (column_2);
CREATE INDEX IF NOT EXISTS hotels_column_3_index ON hotels (column_3);
CREATE INDEX IF NOT EXISTS hotels_column_4_index ON hotels (column_4);
Everything works fine and indexes work too, but when I have any more than 4 indexes for any columns, query planning time goes from 10ms to 700ms. It works well with less than 5 indexes for any columns. So what can be a problem? why changing number of indexes from 4 to 5 have such a bad effect?
Query is simple like that: SELECT hotels.name FROM hotels WHERE hotels.state = 'AZ';
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
