我在 Web API 中有一个 JDBC 连接池,它经常向 RDS 实例发出请求。每个请求都是针对长时间运行的 SQL 查询,其中 PG 决定不使用索引,因为要返回的数据量非常大。
我注意到其中一些长时间运行的查询只是停留在 state idle in transaction。当在表中查找pid这些进程时pg_locks,它们有 152 行。带模式AccessShareLock。
该表的目的是永远不会更新——它充当时间表中的一个点。所以我认为不需要idle transactionor accesssharelock。我是否可以关闭这些功能,以便我的查询运行得更快并且不会因为AccessShareLock?
我有一张超过 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) 我有一个包含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