

Using Explain in Postgres
source link: https://fuzzyblog.io/blog/postgres/2020/04/17/using-explain-in-postgres.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Using Explain in Postgres
Apr 17, 2020
- a fog of comic book cursing that surrounded me as I worked in my attic garret like office
- a sense of my life will never be the same again (Open Source to me has had the same type of impact on my life as meeting women that I have loved and having children and discovering Ruby)
Ah yes but I digress with the lack of clarity of someone who has slept 8 hours in the past 72 but …
Anyway - MySQL - I've been using explain to analyze queries in MySQL for decades i.e.
explain SELECT foo FROM bars WHERE expression a = b
And, as of late, I've been using Postgres for many, many projects including CovidNearMe and my professional day job at the ADL and I've been thinking that explain doesn't work because my database client has, for the first time in my life been graphical, Postico, not a raw terminal and I was never able to make explain work in Postico.
Note: I just stinking tried this again and Postico has made a liar out of me. Sigh. Honest – explain didn't work like yesterday when I created this blog post. Sigh. Sleep deprivation impacts on cognition; perhaps I should not be driving a motor vehicle today …
So the other day when I need to analyze a query and Google told met hat EXPLAIN was actually a thing, I got out the console and did this:
psql -U sjohnson -d covid_morbidity1_development
psql (11.5)
Type "help" for help.
covid_morbidity1_development=# EXPLAIN select max(date_created_at) from locations;
QUERY PLAN
----------------------------------------------------------------------
Aggregate (cost=3736.90..3736.91 rows=1 width=4)
-> Seq Scan on locations (cost=0.00..3525.32 rows=84632 width=4)
(2 rows)
Now I don't use Postgres enough to really understand this but it isn't showing me that an index is NOT being used for the query and that's always, always bad. So I added a simple index as per this example:
table.index ["date_created_at"], name: "index_locations_on_date_created_at"
And now we get:
❯ psql -U sjohnson -d covid_morbidity1_development
psql (11.5)
Type "help" for help.
covid_morbidity1_development=# EXPLAIN select max(date_created_at) from locations;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------
Result (cost=0.36..0.37 rows=1 width=4)
InitPlan 1 (returns $0)
-> Limit (cost=0.29..0.36 rows=1 width=4)
-> Index Only Scan Backward using index_locations_on_date_created_at on locations (cost=0.29..5992.46 rows=84633 width=4)
Index Cond: (date_created_at IS NOT NULL)
(5 rows)
What I can tell from this is that we're not dealing with a full table scan of 3736 row but instead a scan of 36 rows.
Recommend
-
59
In this blog post, we will walk through PMM-Query Analytics for MongoDB. We will see how to analyze MongoDB query perfo...
-
33
In MySQL 8.0.18 there is a new feature called Explain Analyze
-
9
pev2 A VueJS component to show a graphical vizualization of a PostgreSQL execution plan. See Demo . Disclaimer This project is a rewrite o...
-
6
A simple explanation of the Louvain algorithmPermalink If you haven’t read it already, knock out
-
11
Improve Performance Using SQL Functions in Postgres We’ve all hit that point on a project: features are working, and you’re starting to really expand the application’s capabilities, but the expansion is steadily leadi...
-
4
Illustrating a use case for the Louvain algorithmPermalink Explaining domain topics to someone far outside that domain i...
-
10
The PostGIS raster extension has a steep learning curve, but it opens up some unique possibilities for data analysis and accessing non-standard data from within PostgreSQL. Here's an example that shows how to access raster data from PostGIS r...
-
8
TL;DR: Evolutionary game theory, initially developed for biology, has been successfully applied to other domains such as economics, sociology, and anthropol...
-
9
Using GPT-3 to explain how code works One of my favourite uses for the GPT-3 AI language model is generating explanations of how code works. It’s shockingly effective at this: its training set clearly include a vast amount of source code...
-
6
Chris Panigel October 21, 2022 1 minute read...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK