zatae

zatae

Ecto high database time with Cloud SQL

I have a Phoenix framework project running on Cloud Run. The database is hosted on Cloud SQL.

For some reason, I have really high db response time for even really simple requests and nearly no data:

2022-04-05 20:24:31.035 CEST
18:24:31.035 [debug] QUERY OK source="items" db=298.8ms queue=0.1ms idle=900.7ms
2022-04-05 20:24:31.035 CEST
SELECT t0."id", t0."name", t0."identifier", t0."enabled", t0."user_id", t0."inserted_at", t0."updated_at" FROM "items" AS t0 WHERE (t0."identifier" = $1) ["f2356731-fbd4-4a10-8aa2-c89adf48a98d"]

2022-04-05 20:27:15.077 CEST
18:27:15.076 [debug] QUERY OK source="items" db=692.3ms idle=807.2ms
2022-04-05 20:27:15.077 CEST
SELECT t0."id", t0."name", t0."identifier", t0."enabled", t0."user_id", t0."inserted_at", t0."updated_at" FROM "items" AS t0 WHERE (t0."identifier" = $1) ["f2356731-fbd4-4a10-8aa2-c89adf48a98d"]


SELECT COUNT(*) FROM items;
 count
-------
    16
(1 row)

Any idea ? I tried various type of instance size (both SQL & Run container) but this seems that this is not the problem.

Marked As Solved

zatae

zatae

Finally found what’s was wrong : I have a process running in background, pulling messages from a GCP PubSub and processing it with some db queries (the ones that were really slow). And it seems that without any incoming traffic from the outside (GCP PubSub pull requests obviously aren’t), the container goes in some kind of sleep mode after a few seconds.

To overcome this, you just need to update your container’s configuration CPU allocation and pricing to CPU is always allocated so the container never goes in “sleep mode” and is always awake to query database without any delay. This was more like a GCP issue than a Elixir one but still, it might be useful for some other ppl.

Thanks for your help.

Also Liked

mylan

mylan

Just a shot in the dark, but are they in the same region? And are you using the internal database address or are you using the public address?

joey_the_snake

joey_the_snake

If you show us the explain plan it would help seeing what’s happening at the db level. You can do so using this function:

https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.html#explain/4

Using the anlayze: true opt would provide the best info.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement