小编Ant*_*ony的帖子

长时间运行的 READ 查询陷入“事务中空闲”状态

我在 Web API 中有一个 JDBC 连接池,它经常向 RDS 实例发出请求。每个请求都是针对长时间运行的 SQL 查询,其中 PG 决定不使用索引,因为要返回的数据量非常大。

我注意到其中一些长时间运行的查询只是停留在 state idle in transaction。当在表中查找pid这些进程时pg_locks,它们有 152 行。带模式AccessShareLock

该表的目的是永远不会更新——它充当时间表中的一个点。所以我认为不需要idle transactionor accesssharelock。我是否可以关闭这些功能,以便我的查询运行得更快并且不会因为AccessShareLock?

postgresql performance postgresql-performance

5
推荐指数
1
解决办法
5889
查看次数

如何优化索引列上的 IN 查询

我有一张超过 5000 万条记录的表。其中一个字段是COLOR_CODE。我在列上设置了一个索引,COLOR_CODE如下所示:

"mytable_colorcode_idx" btree (color_code)
Run Code Online (Sandbox Code Playgroud)

我注意到当我运行下面的查询时,执行时间更长

SELECT count(total_amount) FROM mytable 
WHERE color_code in ('red','green') and sale_date = '1970'
Run Code Online (Sandbox Code Playgroud)

但是,使用OR子句执行时间更快:

SELECT count(total_amount) FROM mytable 
WHERE color_code = 'red' or color_code = 'green' and sale_date = '1970'
Run Code Online (Sandbox Code Playgroud)

查询计划 IN

explain analyze SELECT count(total_amount) FROM mytable 
WHERE color_code in ('red','green') and sale_date = '1970'
                                                                            QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=2074238.07..2074238.08 rows=1 width=8) (actual time=63520.150..63520.150 rows=1 loops=1)
   ->  Bitmap Heap Scan on mytable  (cost=53504.73..2069923.27 rows=1725919 width=6) (actual time=3509.920..63080.519 …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index optimization query-performance

4
推荐指数
1
解决办法
131
查看次数

将引号放在整数列的 WHERE 子句中有什么后果?

我有一个包含some_column数据类型列的表integer
我注意到我的 PostgreSQL 10.6 实例足够智能,可以将以下两个查询解释为相同:

select * from my_table where some_column = 5; -- no quotes

select * from my_table where some_column = '5'; -- quotes added
Run Code Online (Sandbox Code Playgroud)

在列的where子句中添加引号有什么影响integer
它会对一个包含 2000 万行的大表产生性能影响吗?
(有一个索引some_column。)

postgresql performance syntax postgresql-10 query-performance

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