Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning PostgreSQL 11 - Third Edition

You're reading from  Learning PostgreSQL 11 - Third Edition

Product type Book
Published in Jan 2019
Publisher
ISBN-13 9781789535464
Pages 556 pages
Edition 3rd Edition
Languages
Authors (2):
Salahaldin Juba Salahaldin Juba
Profile icon Salahaldin Juba
Andrey Volkov Andrey Volkov
Profile icon Andrey Volkov
View More author details
Toc

Table of Contents (18) Chapters close

1. Relational Databases 2. PostgreSQL in Action 3. PostgreSQL Basic Building Blocks 4. PostgreSQL Advanced Building Blocks 5. SQL Language 6. Advanced Query Writing 7. Server-Side Programming with PL/pgSQL 8. OLAP and Data Warehousing 9. Beyond Conventional Data Types 10. Transactions and Concurrency Control 11. PostgreSQL Security 12. The PostgreSQL Catalog 13. Optimizing Database Performance 14. Testing 15. Using PostgreSQL in Python Applications 16. Scalability 17. What's Next? 18. Other Books You May Enjoy

Query rewriting

Writing a query in several ways enables the developer to detect some issues in coding best practices, planner parameters, and performance optimization. The example of returning the guru IDs, names, and the count of success_stories can be written as follows:

postgres=# \o /dev/null
postgres=# \timing
Timing is on.
postgres=# SELECT id, name, (SELECT count(*) FROM success_story where guru_id=id) FROM guru;
Time: 144,929 ms
postgres=# WITH counts AS (SELECT count(*), guru_id FROM success_story group by guru_id) SELECT id, name, COALESCE(count,0) FROM guru LEFT JOIN counts on guru_id = id;
Time: 728,855 ms
postgres=# SELECT guru.id, name, COALESCE(count(*),0) FROM guru LEFT JOIN success_story on guru_id = guru.id group by guru.id, name ;
Time: 452,659 ms
postgres=# SELECT id, name, COALESCE(count,0) FROM guru LEFT JOIN ( SELECT count(*), guru_id FROM success_story group by...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}