本页主要内容为英文。翻译此页
技能与技术
PostgreSQL & SQL roadmap
Learn SQL and PostgreSQL from the first SELECT to indexes, transactions, JSON and vector search for AI apps.
- 时间
- ⏱ 4–5 weeks
- Level
- Beginner
- Steps
- 6 steps
- Progress
- 0/6 done
The roadmap
Click a step to open the full guide.
Best architecture: a highly available PostgreSQL setup
The usual production database layout.
PostgreSQL HA
- Sync standby (failover)
- Read replica (reports)
- Connection pooler
- Continuous backups (PITR)
Why it's built this way
- Connection pooling in front of the database.
- A standby automatic failover; 年 replicas reads. 年
- Point-in-time recovery backups, tested regularly.
PostgreSQL & SQL cheat sheet
The essentials on one screen. Bookmark it for revision.
Queries
| Select | SELECT a, b FROM t WHERE x > 1 ORDER BY a LIMIT 10; |
|---|---|
| Group | SELECT city, COUNT(*) FROM t GROUP BY city; |
| Join | FROM a JOIN b ON b.a_id = a.id |
| Upsert | INSERT … ON CONFLICT (id) DO UPDATE SET … |
| Window | SUM(x) OVER (PARTITION BY g ORDER BY d) |
| CTE | WITH t AS (SELECT …) SELECT … FROM t |
Admin & performance
| Explain | EXPLAIN ANALYZE SELECT …; |
|---|---|
| Index | CREATE INDEX ON orders (customer_id); |
| Index without locking | CREATE INDEX CONCURRENTLY … |
| Update stats | ANALYZE orders; |
| psql: list tables | \dt |
| psql: describe table | \d orders |
Improve performance
- Add indexes for WHERE, JOIN and ORDER BY columns, and verify with
EXPLAIN ANALYZE. - Select only the columns you need; avoid
SELECT *in apps. - Use connection pooling (e.g. PgBouncer) for many short connections.
- Paginate with keyset pagination (
WHERE id > last_id) for big tables instead of large OFFSETs. - Keep statistics fresh (autovacuum/ANALYZE) and archive old data.
- Batch inserts and updates instead of one row at a time.
Security checklist
- Always use parameterised queries to prevent SQL injection.
- Give each app its own database user with least privileges (no superuser).
- Enable TLS for connections; never expose port 5432 to the internet.
- Use row-level security for multi-tenant data when appropriate.
- Automate encrypted backups and test restores regularly.
Most important interview questions
Click a question to see a strong answer. Each step also has its own questions.
1an index and what are its trade-offs 是什么?
A data structure (usually B-tree) that speeds up lookups and sorting. It costs disk space and slows writes, so add indexes based on real query patterns.
2Explain the types of joins.
INNER returns matching rows; LEFT returns all left rows plus matches; RIGHT the reverse; FULL returns all rows from both; CROSS returns every combination.
3a deadlock and how do you avoid it 是什么?
Two transactions each wait for locks the other holds. Avoid by locking rows in a consistent order, keeping transactions short, and retrying on deadlock errors.
4How would you find and fix a slow query?
Find it (pg_stat_statements or logs), run EXPLAIN ANALYZE, look for sequential scans or bad estimates, add or adjust indexes, rewrite the query, and re-measure.
5MVCC 是什么?
Multi-version concurrency control: PostgreSQL keeps row versions so readers don't block writers; VACUUM later removes old versions.
6SQL vs NoSQL: when to choose which?
SQL for structured data, relationships and transactions (most apps). NoSQL for very flexible documents, huge scale key-value access, or specific patterns like caching.
Related 条学习路线
Backend Developer
6 steps · 5–6 months · Intermediate
Build the APIs, databases and services behind apps.
View roadmap →Data Analyst
6 steps · 3–4 months · Beginner
Turn data into answers and dashboards.
View roadmap →FastAPI
6 steps · 3–4 weeks · Intermediate
Build fast, documented Python APIs.
View roadmap →RAG: Retrieval-Augmented Generation
6 steps · 3–4 weeks · Intermediate
Build AI that answers from your own documents.
View roadmap →