質問

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