我想针对我的 PostgreSQL 数据库对一些 SQL 查询进行基准测试。有什么方法可以使用 SQL 查询时间psql吗?
我想根据 ID 获取最多 100 行。id 是表的主键。
我编写的查询如下所示:
select * from table where id = any ($1);
Run Code Online (Sandbox Code Playgroud)
其中$1被插值为 ids 数组。
使用时EXPLAIN ANALYZE我得到以下计划(解释链接):
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.43..44.98 rows=17 width=553) (actual time=100.048..834.209 rows=17 loops=1)
-> Index Scan using instagram_id_index_1000 on profiles_1000 (cost=0.43..44.98 rows=17 width=553) (actual time=100.046..834.163 rows=17 loops=1)
Index Cond: (id = ANY ('{34491540,28977916,33241270,33609141,31043380,29364420,30247037,33311491,36267571,32886281,32366574,32569254,33038689,31089076,29416100,30455309,31570597}'::integer[]))
Planning time: 424.512 ms
Execution time: 834.280 ms
(5 rows)
Run Code Online (Sandbox Code Playgroud)
当我实际执行它时(使用\timing),我得到的结果在 2-5 秒范围内!我真的无法接受如此糟糕的表现。EXPLAIN ANALYZE首先提供的执行时间就已经很长了。
一些上下文:
1)数据库是本地的,所以没有网络延迟
2)我查询的表是物化视图
3)我也尝试了 …