Here is the random selection query
SELECT NAME, SEX,
CURRENT_DATE - CAST(RANDOM()*365.25*100 AS INTEGER) AS DOB,
(SELECT SURNAME FROM SURNAMES ORDER BY RANDOM() LIMIT 1) AS SURNAME
FROM NAMES ORDER BY RANDOM() LIMIT 1;
Here is the procedure:
CREATE OR REPLACE FUNCTION FILL(IN NUM INT) RETURNS VARCHAR(1) AS $$
DECLARE
N INTEGER;
BEGIN
N:=NUM;
WHILE N > 0 LOOP
INSERT INTO PEOPLE(NAME,SEX,DOB,SURNAME)
SELECT NAME, SEX,
CURRENT_DATE - CAST(RANDOM()*365.25*100 AS INTEGER) AS DOB,
(SELECT SURNAME FROM SURNAMES ORDER BY RANDOM() LIMIT 1) AS SURNAME
FROM NAMES ORDER BY RANDOM() LIMIT 1;
N := N - 1;
END LOOP;
RETURN 'T';
END; $$
LANGUAGE PLPGSQL;
(Update)
Forgot to say it takes about seven seconds to insert ten thousands rows ... quite faster than MySQL.
test=# SELECT NOW();SELECT FILL(10000);SELECT NOW(); |
now |
——————————- |
2008-11-09 17:32:53.985751+01 (1 row) |
fill |
—— |
T |
(1 row) |
now |
——————————- |
2008-11-09 17:32:59.359207+01 |
(1 row) |
test=# |
No comments :
Post a Comment