Table de partition dans PostgreSQL (Simuler des millions de données) - Partie 2
Si vous ne savez pas comment faire la partition de table dans PostgreSQL, veuillez étudierPartition Table In PostgreSQL (Create Partition) – Part 1
Ici, je vais fournir une fonction simple pour montrer comment insérer des millions de données dans la table de partition.
--create sequence for testing CREATE SEQUENCE hashvalue_PT_serial START 1; --Generate Dynamic data for testing CREATE OR REPLACE FUNCTION hashvalue_PT_InsertRandomRecords(in a_no_of_records integer) RETURNS integer AS $$ DECLARE v_counter integer; vhash varchar(255); v_date varchar(15); BEGIN v_counter := 1; RAISE NOTICE 'No of records insert : %', a_no_of_records; WHILE (v_counter <= a_no_of_records) LOOP IF( v_counter % 10000 =0) THEN RAISE NOTICE 'Counter here is %', v_counter; END IF; v_date := trunc(random() * 27) +1; vhash := '00' || nextval('hashvalue_PT_serial'); --insert into partiton table INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea , date ('2008-01-' || v_date)); INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea, date ('2008-02-' || v_date)); INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea, date ('2008-03-' || v_date)); INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea, date ('2008-04-' || v_date)); INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea, date ('2008-05-' || v_date)); INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea , date ('2008-06-' || v_date)); INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea, date ('2008-07-' || v_date)); INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea, date ('2008-08-' || v_date)); INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea, date ('2008-09-' || v_date)); INSERT INTO hashvalue_pt( hash, hashtime) VALUES (E'\\003\\002\\001\\0151'::bytea || vhash::bytea, date ('2008-10-' || v_date)); v_counter := v_counter + 1; END LOOP; RETURN 0; END; $$ LANGUAGE plpgsql;
J'ai créé une simple fonction «hashvalue_PT_InsertRandomRecords» pour simuler les données pour les tests de performances sur la table de partition.
select * from hashvalue_PT_InsertRandomRecords(20000000);
Haha .. one shot insérer 200 millions de données. Ok, maintenant j'ai 200 millions de données en 10 mois, la prochaine session je vais commencer à faire des tests de performances entre la partition et la table non partitionnée, veuillez visiterPerformance Testing on Partition Table In PostgreSQL – Part 3