TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-05-2008, 06:06 PM   #1 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default mysql -- most efficient way to batch process UPDATES

Hey all. I currently have a program that can be launched by the user of which does processing on their records specific. Part of the process is to select out specific records and make an update to them with the recently pulled ID.

Currently I am running the program to batch update in groups of 25. I will select 25 records that need updated, and once they are all pulled in, update each specific row with its new data.

The problem this poses is that I do not want to try and do this as efficiently as possible (since it is an unknown as to how many users may launch this at anyone time).

Doing a UPDATE query call for each iteration is unacceptable (I think anyways).

and for some reason, mysqli_multi_query took almost twice the amount of time as an acutal individual call to each update.

Does anyone know the basics behind how transactions work? If i loop over the updates within a transaction, then call a commit at the end does this force it to truely be done within 1 call? or does will it still make 25 seperate calls to the database for the update?

Psuedo Code:
PHP Code:
{start transaction}
  {foreach 
$record}
    {
UPDATE table set foo bar WHERE id $record[id]}
  {/foreach}
{/
end transactioncommit
Is this the best possible way to tackle this?
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Old 05-05-2008, 06:20 PM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Can you give us the specifics of the query itself? In other words, is it something like your example where you're setting the same value for every record that match on a set of record ID values?

If that's the case, then use a single query referencing a list of ID values using the IN operator, e.g.
Code:
UPDATE yourTable
SET column1 = 'new value'
WHERE id IN (3,5,7,9,11,13);
A transaction is an interesting idea; what engine type are you working with? MyISAM or InnoDB?
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Old 05-05-2008, 06:25 PM   #3 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

InnoDB -- I need the huge maximum record count as well as row level locking for speed.

Right ~ These updates are unique values to each row pulled from another source, so the above will not work.

Each row will be updated based on the WHERE clause to single it down... however the values being updated are completely irrelevant from one row to another.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Old 05-06-2008, 12:09 PM   #4 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

Anyone have any insight or tips into this? I tried searching around, but am not getting a very definative answer on how transactions are processed with multiple queries. I know there will be 25 seperate queries fired off... however I am curious as to whether or not it sends the query with each request, or will wait for the final commit to be made. Obviously before the commit happens, the changes wont happen yet... however I think they go to some type of staging area in the table with each request? Am I way off on this one?
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:41 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design