# Ecto high database time with Cloud SQL

**URL:** <https://forum.elixirforum.com/t/ecto-high-database-time-with-cloud-sql/47052>\
**Category:** Questions\
**Tags:** ecto, google-cloud\
**Created:** [April 5, 2022, 6:40pm UTC](https://forum.elixirforum.com/t/ecto-high-database-time-with-cloud-sql/47052 "2022-04-05T18:40:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zatae](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zatae/32/23640_2.png) [@zatae](https://forum.elixirforum.com/u/zatae)\
**Post date:** [April 5, 2022, 6:40pm UTC](https://forum.elixirforum.com/t/ecto-high-database-time-with-cloud-sql/47052/1 "2022-04-05T18:40:21Z")

</div>

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:

```elixir
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.

---

<div class="post-metadata">

**Author:** ![mylan](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@mylan](https://forum.elixirforum.com/u/mylan)\
**Post date:** [April 5, 2022, 10:45pm UTC](https://forum.elixirforum.com/t/ecto-high-database-time-with-cloud-sql/47052/2 "2022-04-05T22:45:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zatae](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zatae/32/23640_2.png) [@zatae](https://forum.elixirforum.com/u/zatae)\
**Post date:** [April 6, 2022, 7:02am UTC](https://forum.elixirforum.com/t/ecto-high-database-time-with-cloud-sql/47052/3 "2022-04-06T07:02:37Z")

</div>

Sorry for the lack of data related to the configuration, I was in a hurry!

So, both instances are on same region : **us-central1** for Cloud Run and **us-central1-f** for Cloud SQL. (I assume these are the same). I use `:postgrex, "0.15.13"` & `:ecto, "3.7.1"` and connect to database through socket `socket_dir: System.get_env("POSTGRESQL_SOCKET")`. _Instance IP assignment_ is set to **Public IP**. Tell me if you need more information about this.

---

<div class="post-metadata">

**Author:** ![joey\_the\_snake](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joey\_the\_snake](https://forum.elixirforum.com/u/joey_the_snake)\
**Post date:** [April 6, 2022, 10:13am UTC](https://forum.elixirforum.com/t/ecto-high-database-time-with-cloud-sql/47052/4 "2022-04-06T10:13:42Z")

</div>

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:

> **[Ecto.Adapters.SQL — Ecto SQL v3.14.0](https://ecto-sql.hexdocs.pm/Ecto.Adapters.SQL.html)**

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

---

<div class="post-metadata">

**Author:** ![zatae](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zatae/32/23640_2.png) [@zatae](https://forum.elixirforum.com/u/zatae)\
**Post date:** [April 6, 2022, 1:03pm UTC](https://forum.elixirforum.com/t/ecto-high-database-time-with-cloud-sql/47052/5 "2022-04-06T13:03:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rathoud96](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rathoud96/32/26154_2.png) [@rathoud96](https://forum.elixirforum.com/u/rathoud96)\
**Post date:** [March 7, 2023, 3:08pm UTC](https://forum.elixirforum.com/t/ecto-high-database-time-with-cloud-sql/47052/6 "2023-03-07T15:08:13Z")

</div>

Can you please tell me if you did some additional steps to connect to cloud sql through `unix socket`  
I am having trouble connecting to the db from my `cloud_run` instance.

Here’s the discussion thread

> [@Ecto connection error with gcloud postgres instance](https://forum.elixirforum.com/t/ecto-connection-error-with-gcloud-postgres-instance/54310/10):
>
> Another person on the forum also pasted a CloudSQL config here: [https://forum.elixirforum.com/t/preparing-the-production-environment-for-deployment-in-gcp-with-cloud-sql-database/45476](https://forum.elixirforum.com/t/preparing-the-production-environment-for-deployment-in-gcp-with-cloud-sql-database/45476)

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [March 7, 2023, 3:29pm UTC](https://forum.elixirforum.com/t/ecto-high-database-time-with-cloud-sql/47052/7 "2023-03-07T15:29:01Z")

</div>

Nah man, I worked with a lot of stuff but not with CloudSQL. I distinctly remember having used `socket_dir` once to connect to a local Postgres and it worked. Can’t help you further I am afraid. Must be something in the configuration of CloudSQL or something else.
