#34874: the construction "table_name" in introspection.table_names() is suboptimal -------------------------------------+------------------------------------- Reporter: pinaraf | Owner: nobody Type: | Status: new Cleanup/optimization | Component: Database | Version: 4.2 layer (models, ORM) | Severity: Normal | Keywords: Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- On databases with a lot of tables, even in several schemas (with django- tenant-schema for instance), the `introspection.table_names()` call can take tens of ms to run. For instance on a PG database with 8800 relations, the query takes 21ms. table_names() is called at least by the migration recorder in order to verify the presence of the django_migrations table. It thus slows down every migration execution uselessly. 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).
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. The SQL query from table_names function modified to add a `relname = 'XXX'` criteria uses the index on relname and is thus over 20 times faster. -- Ticket URL: <https://code.djangoproject.com/ticket/34874> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018ad6add31f-298f08da-291c-4dd8-b45f-a09e6aa3a539-000000%40eu-central-1.amazonses.com.