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?

有帮助吗?

解决方案

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')

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top