相关疑难解决方法(0)

提高查询速度:大postgres表中的简单SELECT

我在Postgres数据库的SELECT查询中遇到速度问题.

我有一个表有两个整数列作为键:(int1,int2)这个表有大约7000万行.

我需要在这种环境中进行两种简单的SELECT查询:

SELECT * FROM table WHERE int1=X;
SELECT * FROM table WHERE int2=X;
Run Code Online (Sandbox Code Playgroud)

这两个选择在这7000万个中返回大约10,000行.为了尽可能快地工作,我考虑使用两个HASH索引,每列一个.不幸的是结果并不那么好:

                                                               QUERY PLAN                                                               
----------------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on lec_sim  (cost=232.21..25054.38 rows=6565 width=36) (actual time=14.759..23339.545 rows=7871 loops=1)
   Recheck Cond: (lec2_id = 11782)
   ->  Bitmap Index Scan on lec_sim_lec2_hash_ind  (cost=0.00..230.56 rows=6565 width=0) (actual time=13.495..13.495 rows=7871 loops=1)
         Index Cond: (lec2_id = 11782)
 Total runtime: 23342.534 ms
(5 rows)
Run Code Online (Sandbox Code Playgroud)

这是其中一个查询的EXPLAIN ANALYZE示例.这需要大约23秒.我的期望是在不到一秒的时间内获得这些信息.

这些是postgres db config的一些参数:

work_mem = 128MB
shared_buffers = 2GB
maintenance_work_mem = 512MB
fsync = off
synchronous_commit = off …
Run Code Online (Sandbox Code Playgroud)

sql postgresql performance postgresql-performance

28
推荐指数
2
解决办法
5万
查看次数

PostgreSQL:索引时间戳的日期部分

请考虑下表:

       Column       |           Type           |
--------------------+--------------------------+
 id                 | bigint                   |
 creation_time      | timestamp with time zone |
...
Run Code Online (Sandbox Code Playgroud)

像下面这样的查询(更不用说更复杂的JOIN)需要相当长的时间,因为它们需要为每个项计算creation_time :: DATE:

SELECT creation_time::DATE, COUNT(*) FROM items GROUP BY 1;
Run Code Online (Sandbox Code Playgroud)

如何在时间戳的那一天创建索引 - creation_time::DATE?

我试过了:

  • CREATE INDEX items_day_of_creation_idx ON items (creation_time)::date;
  • CREATE INDEX items_day_of_creation_idx ON items (creation_time::date);

但都失败了:

ERROR:  syntax error at or near "::"
Run Code Online (Sandbox Code Playgroud)

postgresql indexing date

10
推荐指数
1
解决办法
3332
查看次数