Jump to content

Optimize table after every delete?


Jaguar

Recommended Posts

Is it a bad idea to do this?

 

DELETE FROM table WHERE id = $id;

OPTIMIZE TABLE table;

 

I'm guessing it could get nasty if the database were huge, but this database is small and only contains about 10,000 records. Also deletes shouldn't happen that often. But I'd like to know for future reference if I work with much larger and more active databases. Should I make a seperate button to optimize the tables so it doesn't optimize with every delete?

Link to comment
Share on other sites

OPTIMIZE TABLE should be used if you have deleted a large part of a table or if you have made many changes to a table with variable-length rows (tables that have VARCHAR, VARBINARY, BLOB, or TEXT columns). Deleted rows are maintained in a linked list and subsequent INSERT operations reuse old row positions. You can use OPTIMIZE TABLE to reclaim the unused space and to defragment the data file.

 

This statement requires SELECT and INSERT privileges for the table.

 

In most setups, you need not run OPTIMIZE TABLE at all. Even if you do a lot of updates to variable-length rows, it is not likely that you need to do this more than once a week or month and only on certain tables.

 

If you do have a db that needs to be OPTIMIZEd sometimes, one way to do it is to include a line in your deletion code along the lines of:

 

<?php

mysql_query("DELETE blah blah blah...");
if (mt_rand(0,100000) == 1) mysql_query("OPTIMIZE TABLE table");
?>

 

That way it'd only optimize, on average, every 100,000 deletes.

Link to comment
Share on other sites

Is it a bad idea to do this?

 

DELETE FROM table WHERE id = $id;

OPTIMIZE TABLE table;

 

I'm guessing it could get nasty if the database were huge, but this database is small and only contains about 10,000 records. Also deletes shouldn't happen that often. But I'd like to know for future reference if I work with much larger and more active databases. Should I make a seperate button to optimize the tables so it doesn't optimize with every delete?

 

Why is this a problem for you?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.