php mysql cursor
I need to create a MySQL cursor to keep track of what row number I am currently up to while traversing a "huge"(millions of entires) table.
sample database table:
CREATE TABLE test (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
开发者_运维知识库someText TEenter code hereXT NOT NULL
) ;
if this table as 1,000,000 entries; I execute the following query:
select * from test where id >= 50;
And then I process the data as needed in my php script (with 1 min limit). How do i keep track of up to what row I have traversed "test" table?
// use a PHP session to store the id (could also use cookies...)
session_start();
// your 1 minute timeout
set_time_limit(60);
// query your results (may even put a known-out-of-reach limit on the
// query just to make sure you're not always pulling all the entries every
// reload (that would each up your timeout alone, depending)
$lastID = 0; // lowest possible ID value (e.g. MIN(id) )
if (session_is_registered('last_select_id'))
{
$lastID =(int)$_SESSION['last_select_id'];
}
else
{
session_register('last_select_id');
$_SESSION['last_select_id'] = $lastID;
}
$dbResult = mysql_query("SELECT * FROM test WHERE id>{$lastID} ORDER BY id"/* LIMIT 10000 */);
if ($dbResult)
{
while (($row = mysql_fetch_row($dbResult)) !== false)
{
// perform processing
// mark as last processed (depending how many steps,
// you may decide to move this from end to somewhere
// else, just sh*t luck where your timeout occurs)
$_SESSION['last_select_id'] = $row['id'];
}
}
// it reached the end, save to assume we can remove the session variable
session_unregister('last_select_id');
I can only go by what you're telling me, though I feel this should be natively throttled, not just awaiting PHP to spit out a timeout.
EDIT Another method, extending the batch idea, is to ALTER the table and add a "processed" column you can update.
EDIT2 Also, be careful. I do set/unset the session value. For that reason, this could go infinite loop on you if you refresh even though you got a successful page load back instead of a timeout error (the $lastID won't see a session variable, it will start again at 1, and proceed through again).
精彩评论