Should I put an effort into avoiding updates when the data on a submitted form is unchanged from the data already stored? Is it worth it to query the database before updating?

For a specific example of what I'm talking about, say I use php to generate a form as follows, using existing data to populate the values:
<form ... >
<input type="hidden" name="id[1]" value="1"><input name="name[1]" value="Arnie Alpha"/>
<input type="hidden" name="id[2]" value="2"><input name="name[2]" value="Bernie Bravo"/>
<input type="hidden" name="id[3]" value="3"><input name="name[3]" value="Charlie Charlie"/>
<input type="submit">

Now if the user changes "Charlie Charlie" to "Donald Delta", and submits the whole form, should the database be queried to see that the first two records were unchanged?


Well, usually an update query takes longer than a select query because an update query locks the database where a select query does not. Therefore, if you can avoid an update query by running a select query, it might do good to your app's performance. You will need to have use case statistics, however, to see how many times values actually remain unchanged, what the difference in impact is for both scenarios, etc., to be able to give a true answer to your question.

Ok, that helps. At least I know it isn't "always do this" or "always do that". I'll have to see my database in action for a bit to see how it pans out. Thank you.

In addition to my answer, you can measure how long actions take by using - for example - the microtime function. Example:

// ----- ACTION 1: -----

$start = microtime(true);

for ($i = 0; $i < 9999; $i++) {
    // Perform action 1.

$end = microtime(true) - $start;
$avg_time_per_action_1 = $end / 9999;

// ----- ACTION 2: -----

$start = microtime(true);

for ($i = 0; $i < 9999; $i++) {
    // Perform action 2.

$end = microtime(true) - $start;
$avg_time_per_action_2 = $end / 9999;

// ----- RESULTS: -----
echo 'Avg time 1: ' . $avg_time_per_action_1 . ', 2: ' . $avg_time_per_action_2 . '<br>';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.