Re: [Django] #34874: Add table_exists() to optimize checks for django_migrations table. (was: the construction "table_name" in introspection.table_names() is suboptimal)

3 views
Skip to first unread message

Django

unread,
Sep 27, 2023, 10:11:27 AM9/27/23
to django-...@googlegroups.com
#34874: Add table_exists() to optimize checks for django_migrations table.
-------------------------------------+-------------------------------------
Reporter: pinaraf | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => wontfix


Comment:

> For instance on a PG database with 8800 relations, the query takes 21ms.

As far as I'm aware, tables introspection is extremely fast on PostgreSQL.
This is proven by 21ms for 8800 tables.

> Fixing the migrations recorder to keep the information cached would be a
quick work around this, but it would still execute the long query at least
once, but especially with django-tenant-schema it would end up taking tens
of seconds (at least one call per tenant).

This is cached in Django 5.0+ (ea8cbca579cc6742e119747fc1eb6ecf90638bce.)

> I suggest adding an `table_exists(table_name)` function in
`db.introspection`. Its base implementation could be simply a return
table_name in self.table_names(), but specific DB engines like PostgreSQL
could have an optimized variant.

I don't think it's worth adding for a single (already cached) call.

--
Ticket URL: <https://code.djangoproject.com/ticket/34874#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages