Question

There a few questions out there on what OPTIMIZE TABLE COMMAND does. I know that it helps to clear up the overhead that can hang around in fragmented tables. For me there are sometimes that I need to run it and I wanted to create a view to run these commands from within django.

Is the best way to do this using raw SQL or is there some other django orm to help with it?

Was it helpful?

Solution

Raw SQL is the way to go here.

from django.db import connections, transaction
cursor = connections['default'].cursor()
cursor.execute('OPTIMIZE TABLE;')
transaction.commit_unless_managed(using='my_db_alias')

OTHER TIPS

The best way to do this is by executing the custom sql directly.

But it is very dangerous to this kind of system or database administration through the application; if something goes wrong with that command your django application could go offline taking your database management tools with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top