互联网技术 · 2024年3月3日

将数据加载到PostgreSQL的缓冲区缓存中的步骤

我们都知道数据在缓存中访问远比在磁盘中访问速度要快,那么我们怎么在pg中将指定的数据加载到缓存中呢,这有点类似于Oracle的in-memory。

当然要注意并不是把数据加载到内存中就一定是好的,因为相较于磁盘,内存总是有限的,所以一帮我们只是在特殊场合下将需要的数据加载到内存中来加快访问的速度。

我们可以使用pg_prewarm插件来将指定的表加载到OS Buffer或者pg shared buffer中。

安装:

bill=# create extension pg_prewarm ;
CREATE EXTENSION

 

性能测试:

构建测试表t1,t2,分别插入1000W条测试数据

bill=# create table t1(id int,info text);
CREATE TABLE
bill=# create table t2(id int,info text);
CREATE TABLE
bill=# insert into t1 select generate_series(1,10000000),md5(random()::text);
INSERT 0 10000000
bill=# insert into t2 select generate_series(1,10000000),md5(random()::text);
INSERT 0 10000000

 

测试前先清空shared_buffer,可以使用下面sql查看shared_buffer使用情况:

安装pg_buffercache插件:

bill=# create extension pg_buffercache;
CREATE EXTENSION

 

查询shared_buffer使用情况:

SELECT
    c.relname,
    count(*) AS buffers
FROM pg_buffercache b
INNER JOIN pg_class c
   ON b.relfilenode = pg_relation_filenode(c.oid)
    AND b.reldatabase IN (0, (SELECT oid FROM pg_database
WHERE datname = current_database()))
GROUP BY c.relname
ORDER BY 2 DESC;
                 relname                 | buffers
—————————————–+———
 pg_attribute                            |      36
 pg_proc                              |      27
 pg_class              &

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册