Questions tagged [postgresql-performance]

About PostgreSQL query optimization. Include sufficient information in questions. For complex, advanced questions or if you're looking to tune without modifying queries, ask on https://dba.stackexchange.com/ instead.

Filter by
Sorted by
Tagged with
0 votes
2 answers
76 views

Optimize a query using multicolumn indices

Say you want to optimize a query in a postgres database like: SELECT DISTINCT ON (first) first, second, third FROM my_table WHERE second > 100 AND fourth = 3 ORDER BY first, ...
Rob's user avatar
  • 3,392
3 votes
2 answers
57 views

Compound index with date function doesn't allow index-only scans?

I'm trying to use a compound (multicolumn) index on a table to assist in creating daily report counts. I am using Postgres 13, and my table looks like this: CREATE TABLE inquiries ( id bigint NOT ...
Brad Pardee's user avatar
0 votes
0 answers
31 views

Outliers produce a bad query plan

I have table document with ~2.5 millions rows and more than 100 columns. I provide only used in query columns: CREATE TABLE document ( id serial PRIMARY KEY, organizationid INTEGER, status_1 ...
Bunny Boss's user avatar
0 votes
1 answer
73 views

How to improve bulk inserts to Postgres DB

I currently have a service in C# that uses dapper to call a stored procedure that does 2 things: if the customer exists, it grabs the customer GUID and adds it to the CustomerInformations table; if ...
Master's user avatar
  • 2,047
-1 votes
2 answers
51 views

PostgreSQL better query performance

I have customers and visits table, and I would like to know which of these two queries has the better performance: (I have indexes defined for those columns) Query 1 SELECT customers.id as id, ...
Pablo Alejandro's user avatar
1 vote
2 answers
59 views

Test given IDs for existence in any row of a JSON column

In my Postgres 13.10 database, I have a table menus with a JSON column named dishes. This column contains values in the following format: { "desserts": [ {"id": 1, &...
ocratravis's user avatar
0 votes
1 answer
30 views

How to select efficiently by list of dateranges?

I have two tables: create table foo( id serial primary key, ts timestamp ); create table bar( id serial primary key, ts_start timestamp, ts_end timestamp ); I need to select all rows from ...
peter ignatiev's user avatar
1 vote
1 answer
78 views

Using PostgreSQL table as tumbling window persistence layer

Currently, I'm storing a large amount of time-series data into the following PostgreSQL table: create table view ( user_id uuid, product_id integer, banner_id integer, campaign_id integer, ...
vinnylinux's user avatar
  • 7,108
0 votes
2 answers
50 views

Postgres - Select Query Optimization

I want to optimize the following DB SELECT query: SELECT ls.caller_id, ls.caller_path_id, ls.caller_path_name, ls.caller_data->>'worker' AS worker,...
Johnny's user avatar
  • 57
1 vote
1 answer
99 views

Postgres using two indexes for WHERE clause in parallel

I have table which has a few billion rows. There are two columns in the table: match_id uuid, group_id integer And there are indexes created on both of the above columns: create index if not exists ...
Sagar's user avatar
  • 5,417
1 vote
1 answer
80 views

Efficient approach to remove the majority of rows from a PostgreSQL table

I have a table in my Postgres database with approximately 4 million rows, and I need to reduce it to around 30,000 rows while preserving the original IDs due to foreign key references. The condition ...
bahri's user avatar
  • 31
1 vote
1 answer
75 views

In Postgres partial index is taking more time and cost to execute than normal index

I was comparing the performance of a normal index and partial index in Postgres and to my surprise, the partial index performance seems sub-optimal compared to the normal index. The explain statement ...
kishore's user avatar
  • 33
0 votes
1 answer
43 views

Confusingly slow SQL when querying pg_stat_database system view

I am using PostgreSQL 14.0. When I execute this SQL statement: explain analyse select xact_commit from pg_stat_database; I get a confusing query plan like this: ...
Benzecat's user avatar
-2 votes
1 answer
44 views

Combine similar EXISTS conditions in PostgreSQL

I would like to know whether multiple similar exists conditions can be combined in a meaningful and performant way. Let us assume the following example: Different activities can be assigned to a ...
Philipp Li's user avatar
0 votes
1 answer
128 views

Why Postgres does not use GIN index if I specify a LIMIT clause?

I have a table containing about 5 million records and a GIN index on Barcodes column. I have this query: SELECT T1.* FROM public."Skus" AS T1 WHERE (T1."Barcodes" && '{ &...
MatteoSp's user avatar
  • 2,960
0 votes
2 answers
61 views

Looking for a more performant query

I have 2 tables with flex one-to-many relationship, meaning the parent side may not have any child: Parent_table id dim1 price 1 "abc" 1.00 2 "abc" 2.00 3 "def&...
user3758745's user avatar
0 votes
1 answer
53 views

Unable to use index in specific situations

I am facing a strange and infuriating problem of my PostgreSQL 10 database not using indexes in specific situations. I have created an index of an md5 hash of one of the columns, and this index seems ...
Thomas Li's user avatar
1 vote
1 answer
78 views

Query stops parallelizing when adding LIMIT or ROW_NUMBER

I have 20 tables with identical structure, and I need to aggregate the results. I'm using UNION ALL to collect the results, in the following structure: SELECT something, SUM(total) AS overall_total ...
WhatMeWorry's user avatar
1 vote
1 answer
106 views

Subquery with GROUP BY and MAX won't use index

I have this table in PostgreSQL 15.3 (corresponding to a Django model): Table "public.myapp1_task" Column | Type ...
Luke's user avatar
  • 1,794
0 votes
1 answer
178 views

Is it possible to reduce the number of rows in a left join using postgre recursion?

I have a database query that takes over two minutes to complete when I add the last channel condition (channel.code) the main conceptual problem is that I only need to get 10 orders per page, while I ...
farmaper1561's user avatar
1 vote
0 answers
31 views

will CTE run again if we join in other CTE or with a main query? [duplicate]

I have a question regarding whether CTE in a query runs only once or every time it is getting joined. Causing we are facing a CPU consumption issue in postgres metrics. if it runs only once then fine. ...
tomsheldon's user avatar
0 votes
0 answers
56 views

Postgresql slow query performance

One of the Postgres table that I am working on has more 300 million rows. I am trying to select rows from that table based on a column (status_code) having possible values as 'Complete', 'In_Progress',...
Siddharth's user avatar
0 votes
2 answers
49 views

why Postgresql Explain Cost is Low in Limit and Result Phase but high in Index Scan phase

In the shared plan index scan cost is very high (cost=0.56..696715.50 But Limit (cost=0.56..224.59 is very low and so on result cost Result (cost=224.59..224.60 rows=1 width=8) why it is so? Right ...
Monika Yadav's user avatar
0 votes
0 answers
69 views

Postgres improve performance of a select with only latest rows related to item properties

I'm looking forward to optimize a query i have. The tablese are: static_data: historical configuration data of items id(serial pk) timestamp(default now() indexed) tag(varchar 255 fk from tag indexed)...
Fiodar Shurankou's user avatar
0 votes
0 answers
63 views

Single Column Table query much faster than with Multi Column Table

I have a bunch of strings for which I like to find the best match in a table column. This column contains about 400,000 rows and the strings are no longer than 100 characters. When I run the query on ...
chhenning's user avatar
  • 2,057
0 votes
0 answers
153 views

Memory continuously increases at a constant rate in Azure Database for PostgreSQL single server

Our memory usage increases at a constant rate.This is observed in Azure Metrics. Two questions here: Is there a way to know which process is generating the higher and higher memory consumption? Any ...
Ziyue Li's user avatar
6 votes
2 answers
1k views

How to improve or speed up Postgres query with pg_trgm?

Are there any additional steps I can take to speed up query execution? I have a table with more than 100m rows and I need to do search for matching strings. For that I checked two options: Compare ...
Dmiich's user avatar
  • 325
0 votes
0 answers
34 views

Combine Explicit and Implicit Join Spring jpa

Is combining explicit and implicit join in spring data jpa causes any performance issues?
I.vignesh David's user avatar
0 votes
1 answer
67 views

Different execution time for the same query with different time ranges

I have this query running on a Postgres 12 server with WHERE datetime BETWEEN datetimeA and datetimeB. Time difference between datetimeA and datetimeB is always 30 minutes, number of data involved is ...
Jihada Amalia's user avatar
1 vote
2 answers
99 views

Covering index to improve performance of a SELECT query?

I have following Postgres query that runs over a large table and whenever it does, I observe a high CPU load that reaches 100% : SELECT id, user_id, type FROM table ss WHERE end_timestamp > ...
ah_ben's user avatar
  • 95
0 votes
2 answers
594 views

How to analyze the performance of a loop with postgres?

I have a script.sh which executes 1 script.sql. It will look for the data in a data table. I have a loop and in this one I have other loops which will look for information in the data table and insert ...
Camel4488's user avatar
  • 343
1 vote
2 answers
889 views

How to make big postgres db faster?

I have big Postgres database(around 75 GB) and queries are very slow. Is there any way to make them faster? About database: List of relations Schema | ...
unbrokendub's user avatar
0 votes
1 answer
66 views

Index not used for query

I'm running the following query, and the index I have created is not used; PostgreSQL uses a sequential scan instead of an index scan. Query: EXPLAIN (analyze, buffers) SELECT date, company, ...
wiper's user avatar
  • 1
2 votes
2 answers
123 views

DISTINCT ON slow for 300000 rows

I have a table named assets. Here is the ddl: create table assets ( id bigint primary key, name varchar(255) not ...
MehdiB's user avatar
  • 870
3 votes
0 answers
162 views

PostgreSQL Select * faster than selecting columns

I'm writing a query in Postgres and noticed a significant drop in performance when specifying which columns to select. The two queries I'm comparing are basically: A: select * from rate r left ...
rayy56's user avatar
  • 31
-1 votes
2 answers
442 views

Sum over a given time period

The following codes gives the total duration that a light has been switched on. CREATE TABLE switch_times ( id SERIAL PRIMARY KEY, is1 BOOLEAN, id_dec INTEGER, label TEXT, ts TIMESTAMP ...
rm.'s user avatar
  • 509
1 vote
1 answer
1k views

Create a JSON object from parent -> child relationship without duplication

I want to query a database, get ALL of the user's data, and send it to my front end in a JSON object (with many layers of nesting). e.g. { user_id: 1, username: james, messages: [ { message_id:...
Jash1395's user avatar
  • 248
1 vote
0 answers
53 views

SQL Query to retrieve items based on start time but include the current one

I'm currently working on a performance issue in a spring boot microservice. We are trying to reduce the amount of queries sent to our PostgreSQL DB in order to improve execution speed of HTTP requests....
Ian Fako's user avatar
  • 1,158
1 vote
2 answers
473 views

Queries using LIKE are exceptionally slow

I have a database with over 30,000,000 entries. When performing queries (including an ORDER BY clause) on a text field, the = operator results in relatively fast results. However we have noticed that ...
robbieperry22's user avatar
0 votes
1 answer
60 views

pgsql query is causing performance issue

My query on PostgreSQL instance is taking a long time to retrieve the result. This query ran for every 30 mins in our application code. select fgh_dk, (select dmb from fgh_tonee ...
sportyrdy's user avatar
2 votes
3 answers
230 views

Small result query with LIMIT 1000x slower than queries with >100 rows

I am trying to debug a query that runs faster the more records it returns but performance severely degrades (>10x slower) with smaller returns (i.e. <10 rows) using small LIMIT (ie 10). Example: ...
robcxyz's user avatar
  • 324
1 vote
1 answer
127 views

Calculate ratio of counts in PostgreSQL

I have a table mydata in a Postgres 14 database, with the following relevant columns: ftype: an enum having foo, bar and baz status: another enum having pending, failed, success I want the success ...
Abhijit's user avatar
  • 467
0 votes
0 answers
34 views

Postgres, best query performance between group by text attribute VS CTE and group by integer

With these tables What is the most efficient way to display videos with at least 2 actors? Using GROUP BY (the type of the attribute is text) select v.title from play join videos v on play....
Nymau's user avatar
  • 144
0 votes
1 answer
318 views

Postgresql: Prepare statement - Function

Edited to add more context QUESTION: Does PostgreSQL "prepare" the statements stored inside functions and procedures? In other words, are the statements analysed planned when the function/...
Rafael Scudelari de Macedo's user avatar
0 votes
5 answers
1k views

Optimize aggregate query on massive table to refresh materialized view

Let's say I have the following PostgreSQL database schema: Group id: int Task: id: int created_at: datetime group: FK Group I have the following Materialized View to calculate the number of ...
Johnny Metz's user avatar
  • 5,261
1 vote
1 answer
864 views

Very slow search for NULL values with index

I have a Postgres table with ~50 columns and ~75 million records. It has the following index among others: "index_shipments_on_buyer_supplier_id" btree (buyer_supplier_id) EXPLAIN shows it ...
sscirrus's user avatar
  • 55.7k
1 vote
2 answers
95 views

Optimize query to get rows with highest, filtered count in another table

I'm trying to create the most optimal query where the database would return the names of readers who often borrow sci-fi books. That's what I'm trying to optimize: SELECT reader.name, COUNT (CASE ...
sonia 's user avatar
  • 93
0 votes
1 answer
287 views

Query with small LIMIT gets slow with many hits

I have a slow Postgres query and I may need better indexes. But I lack experience with indexing, so I just indexed foreign keys. I use Postgres 14.3 and Django 3.0. This is the slow query. It's only ...
hvar90's user avatar
  • 213
0 votes
0 answers
226 views

PostgreSQL slow query when querying large table by PK (int)

We have a large table (265M rows), which has about 50 columns in total, mostly either integers, dates, or varchars. The table has a primary key defined on an autoincremented column. A query loads temp ...
Alex V's user avatar
  • 315
2 votes
2 answers
37 views

Adding OR clause to where condition is much slower than two individual queries

There are 2 tables, main_table and other_table. A user is allowed access to main_table items if his user_id matches the main_table.user_id column or if there is an entry in other_table where his ...
sihaya's user avatar
  • 1,367

1
2 3 4 5
12