Re: Too many DELETE statements

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Lynch wrote:
Plus you are unlink-ing 10 files.  That's probably the real problem.

You'd have to write some timing code to be sure, though, as a slow
database server and a very fast hard drive could be involved.

Here are some things you could do to speed it up, assuming you don't want
the ON DELETE CASCADE option, or if your database doesn't provide that
option.

You should do this (even if you don't want to) because it is good design. If when a member is deleted you want all associated tables to also delete this member information then you should set up your foreign keys properly.


1. Send only one query for each table:
You should be able to collect all the $ID values in one list like this:

$ids_sql = "2, 4, 5, 42, 17, 68, 1, 9, 10";
$query = "select from table1 where userid in ($ids_sql)";

Of course, you'll need to write that to handle your incoming FORM data
rather than hard-coding the IDs.

The other thing is unlink-ing the image.  That is probably the bigger
time-sink than just a few (dozen) queries.

One way to beat this is to *NOT* unlink the file in your script.

And the ON DELETE CASCADE won't fix this at all.

Instead, write a cron job to walk through the images and throw away
anything not being used.  This will be "slower" and "less efficient" than
doing it in the script, but it can be a background process, not making the
user wait for what is essentially a house-cleaning project.

Something else you could do is to build one big rm statement and run it in the background. This would save you building a cron job which sometimes isn't possible depending on the hosting arrangement.


<?php

$Remove = "rm $ImageLocation1; rm $ImagLocation2; rm $ImageLocation3";
exec("nohop $Remove > /dev/null 2>&1 &");

?>

Where the $ImageLocationx is the full path to the image you want to delete. The & at the end of the exec tells the command to run in the background, which means your script won't wait for the command to finish before continuing. The output redirection ( > /dev/null 2>&1) is also necessary to allow the script to continue. See the PHP documentation on exec for more details.

-- David Dickson

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux