How to Bulk Generate UUIDs
โ Back to Blog
How to Bulk Generate UUIDs
ยท 5 min read
Common Scenarios for Bulk UUID Generation
Typical scenarios requiring bulk UUID generation: database seed data initialization (pre-preparing unique IDs for large numbers of records rather than having the database generate them at insert time); test data generation (preparing a fixed UUID set for automated tests to make tests deterministic); data migration (batch-assigning new UUIDs to records migrated from old systems); API key or token candidate pools (bulk pre-generating backup unique identifiers); batch naming of documents or assets (assigning unique names to a batch of uploaded files).
Command-Line Bulk Generation
# Linux/macOS๏ผ็ๆ 100 ไธช UUID
for i in $(seq 1 100); do uuidgen; done
# macOS ้ป่ฎคๅคงๅ๏ผ่ฝฌๅฐๅ
for i in $(seq 1 100); do uuidgen | tr '[:upper:]' '[:lower:]'; done
# ไฟๅญๅฐๆไปถ
for i in $(seq 1 1000); do uuidgen; done > uuids.txt
# Python ไธ่กๅฝไปคๆน้็ๆ
python3 -c "import uuid; [print(uuid.uuid4()) for _ in range(100)]"
# ็ๆ CSV ๆ ผๅผ๏ผๅธฆ็ดขๅผ๏ผ
python3 -c "
import uuid
print('index,uuid')
for i in range(1, 101):
print(f'{i},{uuid.uuid4()}')
" > uuids.csv
# Node.js ๆน้็ๆ
node -e "
const {randomUUID} = require('crypto');
for(let i=0;i randomUUID());
}
// ็ๆ JSON ๆไปถ๏ผ้ๅไฝไธบๆต่ฏๆฐๆฎ๏ผ
function generateTestData(count) {
const data = Array.from({ length: count }, (_, i) => ({
id: randomUUID(),
name: `Test Item ${i + 1}`,
createdAt: new Date().toISOString(),
}));
return JSON.stringify(data, null, 2);
}
// ไฟๅญๅฐๆไปถ
fs.writeFileSync('test-data.json', generateTestData(100));
console.log('Generated 100 test records with UUIDs');
// ็ๆ CSV ๆ ผๅผ
function generateCSV(count) {
const header = 'id,name,status';
const rows = Array.from({ length: count }, (_, i) =>
`${randomUUID()},Item ${i + 1},active`
);
return [header, ...rows].join('\n');
}
fs.writeFileSync('test-data.csv', generateCSV(500));
Database-Level Bulk Generation
-- PostgreSQL๏ผๆน้็ๆ UUID ่ฎฐๅฝ
INSERT INTO products (id, name, category)
SELECT gen_random_uuid(), 'Product ' || gs, 'default'
FROM generate_series(1, 1000) AS gs;
-- MySQL 8.0+๏ผๆน้ๆๅ
ฅ
DELIMITER //
CREATE PROCEDURE bulk_insert_uuids(IN count INT)
BEGIN
DECLARE i INT DEFAULT 0;
WHILE i < count DO
INSERT INTO items (id, name) VALUES (UUID(), CONCAT('Item ', i));
SET i = i + 1;
END WHILE;
END //
DELIMITER ;
CALL bulk_insert_uuids(1000);
-- SQL Server๏ผๆน้็ๆ
INSERT INTO orders (id, status)
SELECT NEWID(), 'pending'
FROM (
SELECT TOP 1000 ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n
FROM sys.objects CROSS JOIN sys.objects
) AS nums;
Considerations for Bulk Generation
Things to note when bulk generating UUIDs: each UUID is independently generated with no correlation between them โ bulk generation does not increase collision risk; do not use loops calling non-cryptographically-secure random number generators like Math.random(); if bulk-generated UUIDs will be used in databases, consider batch inserts (rather than row-by-row inserts) for better performance; for extremely large batches (over one million), consider processing in chunks and writing to files to avoid memory overflow; if you need reproducible test data (same UUIDs generated each run), use UUID v5 derived from indexes or names rather than random v4.
Verifying Uniqueness of Bulk Generation Results
import uuid
# ็ๆๅนถ้ช่ฏๅฏไธๆง๏ผ็จไบๆผ็คบ๏ผ็ไบงไธญๆ ้่ฟๆ ทๅ๏ผ
def bulk_generate_verified(count):
uuids = set()
while len(uuids) < count:
new_uuid = str(uuid.uuid4())
uuids.add(new_uuid)
return list(uuids)
# ็ป่ฎก set ๅคงๅฐ็ญไบ list ๅคงๅฐ่ฏดๆๆ ้ๅค
uuids = [str(uuid.uuid4()) for _ in range(100000)]
unique_count = len(set(uuids))
print(f"Generated: {len(uuids)}, Unique: {unique_count}")
# ๅ ไนๅฟ
็ถ่พๅบ๏ผGenerated: 100000, Unique: 100000
Try the online tool now โ no installation, completely free.
Open Tool โ
Try the free tool now
Use Free Tool โ