# Are concurrent database queries in ASGI a thing?

**URL:** <https://forum.djangoproject.com/t/are-concurrent-database-queries-in-asgi-a-thing/24136>\
**Category:** Async/Channels\
**Created:** [September 26, 2023, 4:49pm UTC](https://forum.djangoproject.com/t/are-concurrent-database-queries-in-asgi-a-thing/24136 "2023-09-26T16:49:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![salomvary](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salomvary/32/9746_2.png) [@salomvary](https://forum.djangoproject.com/u/salomvary)\
**Post date:** [September 26, 2023, 4:49pm UTC](https://forum.djangoproject.com/t/are-concurrent-database-queries-in-asgi-a-thing/24136/1 "2023-09-26T16:49:30Z")

</div>

Hi folks,

I recently started experimenting with asynchronous views on an existing codebase to improve response times where the view rendering depends on the results of dozens of database queries.

I’m past optimizing the database queries themselves and the logical next step was to try executing (at least some of) them concurrently.

So I’ve switched to Daphne ASGI server and started adding async views and making database queries concurrently using `await asyncio.gather(....)`.

However when I look at the query execution timeline in Sentry, the database queries appear to happen strictly sequentially.

I understand that asynchronous does not necessary mean concurrent but I was hoping that Django + ASGI will bring both. I also re-read the relevant documentation here but I am left clueless: [Asynchronous support | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/async/)

I’m using `psycopg2-binary = "^2.9.6"` with `django = "^4.2"`.

Any insights would be welcome.

Cheers,  
Márton

---

<div class="post-metadata">

**Author:** ![salomvary](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/salomvary/32/9746_2.png) [@salomvary](https://forum.djangoproject.com/u/salomvary)\
**Post date:** [October 5, 2023, 1:08pm UTC](https://forum.djangoproject.com/t/are-concurrent-database-queries-in-asgi-a-thing/24136/2 "2023-10-05T13:08:47Z")

</div>

I got the bottom of this myself, and the answer is, **no, as of 5.0, Django can not execute database queries concurrently**.

- Psycopg 2 has no async support at all.
- Psycopg 3 does, but that is not integrated with Django.
- Most if not all `async def a...` ORM queries are just wrappers around `sync_to_async`: [https://github.com/django/django/blob/main/django/db/models/query.py#L658-L659](https://github.com/django/django/blob/main/django/db/models/query.py#L658-L659)
- What `sync_to_async` does is running functions on a separate thread, _one after another_.

Now I understand that implementing a truly asynchronous database backend is a huge undertaking. The only thing I found is this work in progress pull request, but it seems to be in a very early stage: [Support async database backends by domingues · Pull Request #17275 · django/django · GitHub](https://github.com/django/django/pull/17275)
