Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorDimitri Fontaine

The book that teaches SQL to developers: Learn to replace thousands of lines of code with simple queries! SQL is code Learn how to deal with SQL in your developer workflow: versioning, testing, code reviewing, deployment. Best practice and tools are covered. Practical examples Learn advanced SQL with practical examples and datasets that help you get the most of the book! Every query solves a practical use case and is given in context. Database modelling The book covers (de-)normalisation with simple practical examples to dive into this seemingly complex topic, including Caching and Indexing Strategy. SQL for developers This book is for developers, covering advanced SQL techniques for data processing. Learn how to get exactly the result set you need in your application’s code! Write efficient SQL Writing efficient SQL is easier than it looks, and begins with database modeling and writing clear code. The book teaches you how to write fast queries! Batteries included The book comes with open data sets and instructions to get them so that you can run all the SQL queries at home, edit them and learn from them. Advanced psql setup is even included!

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# The Art of PostgreSQL: Turn Thousands of Lines of Code into Simple Queries ## 【One-Line Pitch】 A practical guide for developers who want to stop wrestling with application-level data processing and instead harness PostgreSQL's full SQL power—from advanced queries and window functions to indexing strategies and extensions—to replace thousands of lines of code with elegant, efficient database operations. ## 【Book Arc】 - **Opening (~0%–10%)**: Introduces the core philosophy that SQL is code and sets up the developer-focused approach—covering psql basics, query fundamentals with real datasets like the factbook example, and establishing the book's practical, example-driven methodology. - **Early (~10%–25%)**: Moves into software architecture and business logic in SQL, demonstrating how to implement application logic directly in queries and stored procedures, using the Chinook sample database to show real-world patterns. - **Early-Middle (~25%–35%)**: Covers developer workflow essentials—SQL testing with pgTAP, schema integrity checks, and a comprehensive indexing strategy chapter explaining PostgreSQL's index types (B-tree, GiST, SP-GiST, GIN, BRIN) and when to use each. - **Middle (~35%–50%)**: Dives into advanced query techniques: processing functions, ORDER BY and LIMIT patterns (including why to avoid OFFSET), GROUP BY variants with grouping sets and CUBE, and a deep exploration of window functions and frames. - **Late (~50%–75%)**: Explores relations and joins from a mathematical perspective, then moves into database modeling, normalization, and denormalization strategies with practical examples. - **Ending (~75%–100%)**: Showcases PostgreSQL extensions for real-world problems—using hstore for auditing changes, pg_trgm for typo-tolerant searches, intarray for tag management, and earthdistance for geolocation queries like finding the nearest pub. ## 【Key Takeaways】 - **SQL is a programming language, not a query tool** (Early): The book's central thesis is that developers should treat SQL as first-class code—versioned, tested, and reviewed like any other language—rather than something to avoid or generate through ORMs. - **psql is the professional's interface** (Early): PostgreSQL's command-line tool offers autocompletion, scripting capabilities, and formatted output that make it superior to GUI tools for serious work; mastering psql is foundational to the rest of the book. - **Business logic belongs in the database** (Early): Moving data processing from application code into SQL queries and stored procedures eliminates correctness issues and efficiency problems that arise from fetching raw data and processing it in Python, Java, or other languages. - **Index selection requires understanding access methods** (Early): PostgreSQL's six index types each solve specific problems—B-tree for general equality and range queries, GiST for 2D data like geometry, GIN for inverted indexes—and choosing correctly dramatically affects query performance. - **OFFSET is an anti-pattern for pagination** (Middle): Using keyset pagination with row-value comparisons (like `row(lap, position) > (1, 3)`) instead of OFFSET keeps queries fast regardless of page depth—a simple change with major performance implications. - **Window functions transformed SQL's capabilities** (Middle): The ability to process peer rows through frames (using functions like `array_agg()` with OVER clauses) enables complex analytics that previously required multiple queries or application-side processing. - **GROUP BY extensions enable sophisticated reporting** (Middle): Grouping sets and CUBE clauses let you generate subtotals and multi-dimensional summaries in a single query, replacing what would otherwise require multiple queries and client-side aggregation. - **Extensions solve real-world problems elegantly** (Late): PostgreSQL's extension ecosystem—hstore for change auditing, pg_trgm for fuzzy text matching, intarray for tag indexing, earthdistance for geolocation—demonstrates how the database can handle problems developers typically solve with custom application code. ## 【Reading Tips】 - **Skim the opening chapters if you're already comfortable with SQL basics**—the real value starts around the business logic and stored procedures sections where the developer-centric approach becomes clear. - **Deep-read the window functions chapter carefully**; the book itself notes "there was SQL before window functions and there is SQL after window functions," and understanding frames is essential for modern PostgreSQL work. - **Run the examples yourself**—the book includes open datasets and instructions, and the queries are designed to be edited and explored; passive reading won't build the intuition the author intends. - **Pay special attention to the indexing strategy chapter** even if you're not a DBA—understanding when PostgreSQL uses which index type will make you write better queries and know when to add indexes. - **The extension chapters near the end are practical gold mines**—even if you don't need hstore or pg_trgm specifically, they demonstrate the extensibility mindset that makes PostgreSQL unique. ## 【Coverage Limits】 The excerpts primarily cover the first half of the book (through window functions and relations) plus the table of contents for later chapters; detailed content on normalization, caching strategies, and the full extension examples is only partially represented in this guide. ##
Page 9
anges with a Trigger . . . . . . . . . . . . . . . . 366 43.4 Testing the Audit Trigger . . . . . . . . . . . . . . . . . . . . 368 43.5 From hstore Back to...
View in text
Excerpt 2
n the Schemaless Design in PostgreSQL section where we deal with a Magic™The Gathering set of cards imported from a JSON data set: 1 select jsonb_pretty(data...
View in text
Excerpt 3
ly in a B-tree index. • SP-GiST, or spaced partitioned gist SP-GiST indexes are the only PostgreSQL index access method imple- mentation that support non-bal...
View in text
Excerpt 4
ers over their entire careers. We have each driver’s points by constructor, and when constructor is NULL we have the total amount of points for the driver. T...
View in text
Excerpt 5
igned 4-byte representa- tion, you’re going to have errors. In the following example, we construct the situation in which we exhaust the id column (an intege...
View in text
Excerpt 6
is very di ferent from object modeling. There are reliable snapshots of a constantly evolving world on the one side, and transient in- ights work ows on the...
View in text
Excerpt 7
hmark it, and then see about optimizing. Materialized Views Back to the f1db databasemodel, we now compute constructor and driver points per season. In the f...
View in text
Excerpt 8
'in love with #?(.*).') 7 where users.bio ~ 'in love with'; Now about those fairies following their queen and king: 1 with fairies as 2 ( 3 select userid 4 f...
View in text
Tags
AI categories
DatabaseSQLBackend
Publisher: Self-published
Publish Year: 2019
Language: English
Pages: 457
File Format: PDF
File Size: 1.5 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…