Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
PostgreSQL Development Essentials

You're reading from   PostgreSQL Development Essentials Advanced querying, data modeling and performance tuning

Arrow left icon
Product type Paperback
Published in Sep 2016
Publisher Packt
ISBN-13 9781783989003
Length 210 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Baji Shaik Baji Shaik
Author Profile Icon Baji Shaik
Baji Shaik
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Advanced SQL FREE CHAPTER 2. Data Manipulation 3. Triggers 4. Understanding Database Design Concepts 5. Transactions and Locking 6. Indexes and Constraints 7. Table Partitioning 8. Query Tuning and Optimization 9. PostgreSQL Extensions and Large Object Support 10. Using PHP in PostgreSQL 11. Using Java in PostgreSQL

Query tuning


Before discussing query tuning, first we should know how to write a query, and then we can tune it later on. Queries can be written in multiple ways, as per the requirement; however, we have to prefer an optimal way to write queries.

For example, say that your requirement is to get the row count from a static table. You can achieve this through the following three methods:

Method 1:

postgres=# SELECT SUM(1) FROM customers;
sum
------
1000
(1 row)
Time: 4.308 ms

Method 2:

postgres=# SELECT COUNT(*) FROM customers;
count
-------
1000
(1 row)
Time: 3.128 ms

Method 3:

postgres=# SELECT reltuples FROM pg_class WHERE relname =  'customers';
reltuples 
-----------
1000
(1 row)
Time: 0.576 ms

From the preceding three possible methods, we see that Method 3 gives the best result, and Method 2 is optimal when compared to Method 1. Method 1 takes a bit more time, when compared with Method 2, since it needs to SUM all the records at once, whereas the COUNT aggregate follows a different...

lock icon The rest of the chapter is locked
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
PostgreSQL Development Essentials
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 €18.99/month. Cancel anytime
Modal Close icon
Modal Close icon