How to speed up insertion performance in PostgreSQL

后端 未结 7 768
走了就别回头了
走了就别回头了 2020-11-22 06:25

I am testing Postgres insertion performance. I have a table with one column with number as its data type. There is an index on it as well. I filled the database up using thi

7条回答
  •  别那么骄傲
    2020-11-22 07:04

    If you happend to insert colums with UUIDs (which is not exactly your case) and to add to @Dennis answer (I can't comment yet), be advise than using gen_random_uuid() (requires PG 9.4 and pgcrypto module) is (a lot) faster than uuid_generate_v4()

    =# explain analyze select uuid_generate_v4(),* from generate_series(1,10000);
                                                            QUERY PLAN
    ---------------------------------------------------------------------------------------------------------------------------
     Function Scan on generate_series  (cost=0.00..12.50 rows=1000 width=4) (actual time=11.674..10304.959 rows=10000 loops=1)
     Planning time: 0.157 ms
     Execution time: 13353.098 ms
    (3 filas)
    

    vs

    
    =# explain analyze select gen_random_uuid(),* from generate_series(1,10000);
                                                            QUERY PLAN
    --------------------------------------------------------------------------------------------------------------------------
     Function Scan on generate_series  (cost=0.00..12.50 rows=1000 width=4) (actual time=252.274..418.137 rows=10000 loops=1)
     Planning time: 0.064 ms
     Execution time: 503.818 ms
    (3 filas)
    

    Also, it's the suggested official way to do it

    Note

    If you only need randomly-generated (version 4) UUIDs, consider using the gen_random_uuid() function from the pgcrypto module instead.

    This droped insert time from ~2 hours to ~10 minutes for 3.7M of rows.

提交回复
热议问题