PostgreSQL DBA
2.02K subscribers
140 photos
1 video
20 files
245 links
Sharing knowledge about postgresql database
Download Telegram
Index Scans (Index Scan, Index Only Scan): Using an index to locate rows. Index Scan requires fetching the actual row data from the table after using the index, while Index Only Scan can retrieve all necessary data directly from the index itself (highly efficient).
Bitmap Scans (Bitmap Index Scan, Bitmap Heap Scan): Used when multiple indexes might apply to a query. The database creates bitmaps representing rows matching each index condition and then combines these bitmaps before fetching the rows from the table. Can be useful when combining multiple filter conditions.
Cost: The estimated cost of each operation. The planner uses a cost model to estimate the resources required to perform each operation. Higher costs usually indicate potential bottlenecks.
Rows: The estimated number of rows returned by each operation. Significant discrepancies between estimated and actual rows (observed with EXPLAIN ANALYZE) can indicate inaccurate statistics, leading the planner to choose a suboptimal plan.
Filtering: Where clauses that filter the data. If a filter is being applied after a large amount of data has already been processed, this is a sign the query could be optimized.
Indexes are crucial for improving query performance, but they also add overhead to write operations. Choosing the right indexes and maintaining them properly is essential.
Composite indexes index multiple columns. The order of columns in a composite index is important. The most frequently used column in WHERE clauses should come first.
Partial indexes index only a subset of the table's rows, based on a condition. This can reduce index size and improve performance when queries frequently filter on that condition.
Statistics: PostgreSQL relies on statistics to estimate the cost of different query plans. Inaccurate statistics can lead to suboptimal plan choices. Regularly update statistics using the ANALYZE command.
Index Bloat: As data is inserted, updated, and deleted, indexes can become fragmented and bloated, leading to performance degradation. Rebuild indexes periodically using the REINDEX command.
Unused Indexes: Identify and remove unused indexes to reduce write overhead. The pg_stat_all_indexes view provides information about index usage.
Beyond indexing, several query optimization techniques can significantly improve performance.
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INTEGER,
order_date DATE,
order_total NUMERIC
) PARTITION BY RANGE (order_date);

CREATE TABLE orders_y2023 PARTITION OF orders
FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');

CREATE TABLE orders_y2024 PARTITION OF orders
FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');

-- Add an index to each partition
CREATE INDEX idx_orders_y2023_customer_id ON orders_y2023 (customer_id);
CREATE INDEX idx_orders_y2024_customer_id ON orders_y2024 (customer_id);
Configuration Parameters