SQL Fundamentals and Query Optimization Techniques for PostgreSQL
This article introduces SQL fundamentals, explains DDL/DML/DCL categories, and provides practical PostgreSQL query optimization techniques—including index selection, condition rewriting, sub‑query removal, expression and partial indexes, DDL decomposition, CTE usage, and key configuration parameters—to help developers and DBAs improve performance and maintainability.
SQL Fundamentals
SQL (Structured Query Language) is the standard language for querying relational databases. It is divided into three parts: Data Definition Language (DDL) for creating and dropping schemas, tables, views, and indexes; Data Manipulation Language (DML) for querying and modifying data (INSERT, UPDATE, DELETE); and Data Control Language (DCL) for granting privileges, defining integrity rules, and controlling transactions. Additional transaction statements such as BEGIN, COMMIT, SAVEPOINT, and ROLLBACK belong to the Data Transaction Language.
Optimize Query
All DDL, DML, and DCL statements are collectively called queries. Optimization mainly focuses on DML and DDL, as DCL rarely has performance issues. Two main goals are: (1) keep the result set unchanged while making the query faster, and (2) reduce lock granularity to improve concurrency.
Optimization Cases
Case 1 – Create Index Directly : Creating an index is common, but its benefit depends on the selectivity of the WHERE clause. If the filter condition matches most rows, the index may not improve performance.
Case 2 – Change Conditions to Use Index : Avoid applying functions or type casts to indexed columns in WHERE clauses, as this prevents the planner from using the index. Rewrite conditions to match the index definition.
Case 3 – Remove Unnecessary Sub‑queries and Outer Joins : Simplify long queries by eliminating redundant sub‑queries and outer joins, which can reduce execution time dramatically (e.g., from 3000 ms to 30 ms).
Case 4 – Eliminate Redundant Columns : Avoid SELECT * when possible; selecting only needed columns reduces memory, disk I/O, and network bandwidth.
Case 5 – Indexes on Expressions : Create expression indexes that match the functions used in WHERE clauses to enable index usage.
Case 6 – Partial Indexes : Use partial indexes when the WHERE condition is fixed and highly selective; they reduce index size and maintenance cost.
Case 7 – Decompose DDL : Split large DDL operations that acquire exclusive table locks into smaller steps to lower lock contention. PostgreSQL 11 introduced a feature that allows adding a non‑null column with a default without rewriting the whole table.
Case 8 – Comprehensive Optimization : Combine multicolumn and partial indexes with step‑wise execution for maximal benefit.
Case 9 – Use CTE : Common Table Expressions can simplify repeated sub‑queries and sometimes improve the planner’s choice, though they do not always speed up execution.
About Indexes
Indexes are crucial for query performance but incur storage and maintenance overhead. PostgreSQL provides several index types:
B‑tree : General‑purpose, supports equality and range queries.
Hash : Equality only; earlier versions had WAL limitations, fixed in PostgreSQL 10.
GiST / SP‑GiST : Framework for custom index strategies, useful for geometric data.
GIN : Inverted index for arrays, JSONB, and other multi‑value columns.
BRIN : Block Range Index, efficient for large tables with naturally ordered data (e.g., timestamps).
Index Maintenance
Identify unused indexes (excluding primary keys and unique constraints) and duplicate indexes to drop the larger one. Detect bloated indexes using pgstattuple or bloat metrics, and rebuild them with CREATE INDEX CONCURRENTLY and DROP INDEX CONCURRENTLY. PostgreSQL 12 will add REINDEX CONCURRENTLY for safer maintenance.
Parameters Affecting Query Plans
Several GUC parameters influence planner decisions, such as the enable_* family, default_statistics_target, and random_page_cost. Advanced cost parameters related to CPU, parallelism, and JIT are usually left at defaults for non‑expert DBAs.
Overall, understanding SQL fundamentals, choosing appropriate index types, regularly cleaning up unused or bloated indexes, and tuning planner parameters enable developers and DBAs to achieve efficient and maintainable PostgreSQL workloads.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
