Таблица разделов в PostgreSQL (Simulate Millions Data) - часть 2

Таблица разделов в PostgreSQL (Simulate Millions Data) - часть 2

Если вы не знаете, как разбить таблицу на разделы в PostgreSQL, изучитеPartition Table In PostgreSQL (Create Partition) – Part 1

Здесь я предоставлю простую функцию, чтобы продемонстрировать, как вставить миллион данных в таблицу разделов.

--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;

Я создал простую функцию hashvalue_PT_InsertRandomRecords для имитации данных для тестирования производительности на таблице разделов.

select * from hashvalue_PT_InsertRandomRecords(20000000);

Ха-ха .. один выстрел вставить 200 миллионов данных. Хорошо, теперь у меня 200 миллионов данных за 10 месяцев, в следующем сеансе я начну проводить тестирование производительности между разделом и таблицей без разделов, посетитеPerformance Testing on Partition Table In PostgreSQL – Part 3