生成11,000,000个唯一ID的最快方法

时间:2013-11-21 01:43:49

标签: php mysql scripting

我正在尝试创建php脚本,按顺序生成11,000,000,000个唯一ID。但是,我试图在20分钟内很快完成它应该产生这1100万个独特的ID。此外,一旦达到12,000,000,它应该环绕并从零开始。

这是我到目前为止所拥有的。这个脚本一次只能返回一个id。我刚刚添加了一个循环,看看生成id需要多长时间。

while(true){

    try {

        $this->getAdapter()->query('INSERT INTO generate_ids (assigned_id) SELECT (MAX(assigned_id)+1) FROM generate_ids');
        $id = $this->getAdapter()->lastInsertId();


        $sql = 'SELECT assigned_id FROM generate_ids WHERE id = $id';  
        $query = $this->getAdapter()->query($sql);
        $result = $query->fetchAll();
            //Live system would return id here
        $assigned_id = $result[0]['assigned_id'];


    } catch (Exception $e) {
        //do nothing
    }

    if($count == 11000000){
        die();
    }

    $count++;
}

}

2 个答案:

答案 0 :(得分:5)

如果您创建下表:

 CREATE TABLE sequence (
     sequence_id BIGINT NOT NULL AUTO_INCREMENT,
     PRIMARY KEY (`sequence_id`)
) 

然后一个接一个地发出这三个查询:

INSERT INTO sequence () VALUES ();
DELETE FROM sequence WHERE sequence_id < LAST_INSERT_ID();
SELECT LAST_INSERT_ID() AS sequence;

第三个查询是保证以返回唯一的序列号。即使您有数十个不同的客户端程序连接到您的数据库,此保证仍然有效。这就是AUTO_INCREMENT的美妙之处。

您可以使用这些SQL查询在需要时获取唯一的序列号,而不是仅预先生成1,100万个序列号。

如果您必须环绕1,200号序列,则可以使用这些查询。

INSERT INTO sequence () VALUES ();
DELETE FROM sequence WHERE sequence_id < LAST_INSERT_ID();
SELECT LAST_INSERT_ID() MOD 12000000 AS sequence;

这里的技巧是使用自动增量序列号来表示唯一性,但也要删除表中的行,这样就不会占用大量空间。

请注意,您还可以将LAST_INSERT_ID()的序列号用于其他目的,例如。

INSERT INTO sequence () VALUES ();
DELETE FROM sequence WHERE sequence_id < LAST_INSERT_ID();
INSERT INTO user (userid, username, phone) 
          VALUES (LAST_INSERT_ID() MOD 12000000, 'Joe', '800-555-1212');
SELECT LAST_INSERT_ID() MOD 12000000 AS sequence;

答案 1 :(得分:1)

如果您需要一次性插入所有ID(出于某种原因),最快的方法是在纯SQL中执行此操作

insert into generate_ids (assigned_id)
select N
from
(
select a.N + b.N * 10 + c.N * 100 + d.N * 1000 + e.N * 10000 + f.N * 100000 + g.N * 1000000 + h.N * 10000000 + 1 as N
from (select 0 as N union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) a
    ,(select 0 as N union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) b
    ,(select 0 as N union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) c
    ,(select 0 as N union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) d
    ,(select 0 as N union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) e
    ,(select 0 as N union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) f
    ,(select 0 as N union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) g
    ,(select 0 as N union all select 1) h
order by N
) q
 where N <= 11000000

我的笔记本电脑上需要不到一分钟的时间。