php mysql 游标

发布于 2024-10-11 09:21:29 字数 366 浏览 6 评论 0原文

我需要创建一个 MySQL 游标来跟踪我在遍历“巨大”(数百万个整体)表时当前所处的行号。

示例数据库表:

CREATE TABLE  test (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
someText TEenter code hereXT NOT NULL
) ;

如果该表有 1,000,000 个条目;我执行以下查询:

select * from test where id >= 50;

然后我根据需要在 php 脚本中处理数据(限制为 1 分钟)。我如何跟踪我已经遍历“测试”表的哪一行?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2024-10-18 09:21:29
// 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');

我只能按照你告诉我的去做,尽管我觉得这应该本地受到限制,而不仅仅是等待 PHP 超时。

编辑另一种扩展批处理思想的方法是更改​​表并添加可以更新的“已处理”列。

EDIT2 另外,要小心。我确实设置/取消设置会话值。因此,如果您刷新,即使页面加载成功而不是超时错误,也可能会陷入无限循环($lastID 不会看到会话变量,它将再次从 1 开始,然后继续执行再次)。

// 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文