I'm curious how to create a temporary table in django? (the database is mysql, a client requirement)
CREATE TEMPORARY TABLE somewhat_like_a_cache AS
(SELECT * FROM expensive_query_with_multiple_joins);
SELECT * FROM somewhat_like_a_cache LIMIT 1000 OFFSET X;
The reasoning behind this: The resultset is fairly large and I have to iterate it. The expensive query takes around 30 seconds. Without a temporary table I stress the database server for several hours. With the temporary table the expensive query is executed only once and iterating the temporary table in slices is cheap afterwards.
This is no duplicate of How do I create a temporary table to sort the same column by two criteria using Django's ORM?. The author just wanted to sort by two columns.
You cannot associate a trigger with a TEMPORARY table or a view. Trigger names exist in the schema namespace, meaning that all triggers must have unique names within a schema. Triggers in different schemas can have the same name.
The Django web framework includes a default object-relational mapping layer (ORM) that can be used to interact with application data from various relational databases such as SQLite, PostgreSQL and MySQL. The Django ORM is an implementation of the object-relational mapping (ORM) concept.
You can create a temporary table that has the same name as an existing table in the same schema, effectively hiding the existing table. You can create a table that has the same name as an existing temporary table in the same schema; however, the newly-created table is hidden by the temporary table.
At first glance, this may sound like a view, but views and temporary tables are rather different: A view exists only for a single query. Each time you use the name of a view, its table is recreated from existing data. A temporary table exists for the entire database session in which it was created.
I have came to this problem and I built a function to sync models to the database (adapted from the management script syncdb
.
you can write your temporary models any where in your code or even generate the models in the run-time then call sync_models
. and enjoy the ORM
its database independent by the way and can be used with any django
supported database backend
from django.db import connection
from django.test import TestCase
from django.core.management.color import no_style
from importlib import import_module
def sync_models(model_list):
'''
Create the database tables for given models.
'''
tables = connection.introspection.table_names()
seen_models = connection.introspection.installed_models(tables)
created_models = set()
pending_references = {}
cursor = connection.cursor()
for model in model_list:
# Create the model's database table, if it doesn't already exist.
sql, references = connection.creation.sql_create_model(model, no_style(), seen_models)
seen_models.add(model)
created_models.add(model)
for refto, refs in references.items():
pending_references.setdefault(refto, []).extend(refs)
if refto in seen_models:
sql.extend(connection.creation.sql_for_pending_references(refto, no_style(), pending_references))
sql.extend(connection.creation.sql_for_pending_references(model, no_style(), pending_references))
for statement in sql:
cursor.execute(statement)
tables.append(connection.introspection.table_name_converter(model._meta.db_table))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With