About PostgreSQL query optimisation. It is essential to include sufficient information in your question as outlined in the tag info. For complex, advanced questions or if you're looking to tune without modifying queries, ask on http://dba.stackexchange.com/ instead.

learn more… | top users | synonyms

1
vote
1answer
17 views

Why am I getting different query plans for the same query (on master / replicated servers)

I am running the same query on a master and replicated server. The settings on both servers in the postgresql.conf are the same however the master / write server has 32GB of RAM and the replicated / ...
11
votes
1answer
7k views

Finding similar strings with PostgreSQL quickly

I need to create a ranking of similar strings in a table. I have the following table create table names ( name character varying(255) ); Currently, I'm using pg_trgm module which offers the ...
5
votes
1answer
617 views

Add datetime constraint to a PostgreSQL multi-column partial index

I've got a PostgreSQL table called queries_query, which has many columns. Two of these columns, created and user_sid, are frequently used together in SQL queries by my application to determine how ...
1
vote
0answers
23 views

Degraded SQL Query Speed By Nesting a Single Query inline vs temp table

I have a query of the following, basic form. SELECT DISTINCT a.field1, b.field2, c.agg_values FROM a INNER JOIN b ON a.something = b.something LEFT JOIN ( SELECT ...
0
votes
1answer
23 views

Postgres jsonb query missing index?

We have the following json documents stored in our PG table (identities) in a jsonb column 'data': { "email": { "main": "[email protected]", "prefix": "aliasPrefix", ...
0
votes
0answers
27 views

Postgres using slower index with small limit

I have an interesting conundrum. I have a few different queries that slow down significantly in certain situations. This one is fast: SELECT "posts".* FROM "posts" WHERE "posts"."source_id" IN ...
6
votes
2answers
81 views

How do I increase the speed of my Postgres select statement?

I have the following tables: CREATE TABLE views ( view_id bigint NOT NULL, usr_id bigint, ip inet, referer_id bigint, country_id integer, validated smallint, completed ...
1
vote
1answer
43 views

Slow performance with small table after extreme reduction of size

I have table with approximately 10 million rows, with the id column being primary key. Then I delete all rows where id > 10. Only 10 rows remain in the table. Now, when I run the query SELECT id ...
8
votes
2answers
5k views

Optimize GROUP BY query to retrieve latest record per user

I have the following table (simplified form) in Postgres 9.2 CREATE TABLE user_msg_log ( aggr_date DATE, user_id INTEGER, running_total INTEGER ); It contains up to one record per user ...
2
votes
2answers
73 views

Performance of DELETE with NOT IN (SELECT …)

I have these two tables and want to delete all authors from ms_author, who are not present in author. author (1.6M rows) +-------+-------------+------+-----+-------+ | Field | Type | Null | ...
2
votes
0answers
67 views

Postgres query on multi column unique index takes longer than expected

I have a large table with 2 columns: name::text url::text I have indices on both columns and I have another unique index on (name, url). When I execute select count(*) from t where name = 'foo' ...
1
vote
1answer
60 views

PostgreSQL aggregate query is very slow

I have a table, which contains a timestamp column and a source column varchar(20). I insert a couple thousand entries into this table every hour and I would like to show an aggregate on this data. My ...
9
votes
5answers
202 views

Optimize large IN condition for Redshift query

I have a ~2TB fully vacuumed Redshift table with a distkey phash (high cardinality, hundreds of millions of values) and compound sortkeys (phash, last_seen). When I do a query like: SELECT ...
1
vote
0answers
23 views

Why no index on timestamp column

I've read a lot of articles about date indexing but I can't use index on timestamp properly. My table: CREATE TABLE registration ( id_reg serial NOT NULL, pass_uid character ...
7
votes
1answer
83 views

Postgresql 9.4 query gets progressively slower when joining TSTZRANGE with &&

I am running a query that gets progressively slower as records are added. Records are added continuously via an automated process (bash calling psql). I would like to correct this bottle neck; ...
0
votes
0answers
27 views

Postgres query performance monitoring on production servers

My goal here is to identify slow queries, which can be done using slow query logs i.e increase the timeout in postgresql.conf to be some 100ms (acceptable query time) and then identify the slow ...
0
votes
0answers
49 views

Long execution time for indexed query in Postgresql

I am using Postgresql 9.2 and I am facing the problem with the index usage. Consider a quite big table (124M records) with 2 columns: device_id, date. Device_id is the id of the device that is ...
1
vote
3answers
58 views

Postgres multi-column index (integer, boolean, and array)

I have a Postgres 9.4 database with a table like this: | id | other_id | current | dn_ids | rank | ...
3
votes
1answer
33 views

Fewer rows versus fewer columns

I am currently modeling a table schema for PostgreSQL that has a lot of columns and is intended to hold a lot of rows. I don't know if it is faster to have more columns or to split the data into more ...
0
votes
3answers
48 views

Slow query on a large table when using order by

I have a table with ~30M tuples. The table looks like: id | first_name | last_name | email ----------------------------------------- 1 | foo | bar | [email protected] Also there are an ...
1
vote
2answers
46 views

Does updating a row with the same value actually update the row?

I have a performance-related question. Let's say I have a user with the first name Michael. Take the following query: UPDATE users SET first_name = 'Michael' WHERE users.id = 123 Will the query ...
20
votes
4answers
18k views

Running PostgreSQL in memory only

I want to run a small PostgreSQL database which runs in memory only, for each unit test I write. For instance: @Before void setUp() { String port = runPostgresOnRandomPort(); ...
0
votes
1answer
47 views

Any way to speed up this sql query?

I have the following Postgres query, the query takes 10 to 50 seconds to execute. SELECT m.match_id FROM match m WHERE m.match_id NOT IN(SELECT ml.match_id FROM message_log ml) AND m.account_id = ? ...
0
votes
2answers
70 views

Hibernate SQL working slow

I am working on Struts2 with Hibernate. For accessing data from database I am using hibernate. I have used following hibernate configuration <hibernate-configuration> <session-factory> ...
1
vote
1answer
48 views

PostgreSQL: FULL OUTER JOIN on RANGE datatypes

I have several tables with partially overlapping TSTZRANGE values and I need to JOIN them such that "breakpoints" are created for each UPPER and LOWER boundary for each range. Some sample data: ...
1
vote
1answer
45 views

postgresql - are my indexes or column types slowing down my query?

I have a table I created locally to use some of PG's window functions on a dataset that has about 4 million rows (originally a text file). Each row corresponds to a customer order. CREATE TABLE ...
1
vote
5answers
90 views

Better way than multiple SELECT statements?

I'm creating a web app that displays a pie chart. In order to get all the data for the chart from a PostgreSQL 9.3 database in a single HTTP request, I'm combining multiple SELECT statements with ...
0
votes
1answer
51 views

Performance improvement for fetching records from a Table of 10 million records in Postgres DB

I have a analytic table that contains 10 million records and for producing charts i have to fetch records from analytic table. several other tables are also joined to this table and data is fetched ...
3
votes
1answer
100 views

First call of query on big table is surprisingly slow

I have a query that feels like it is taking more time then it should be. This only applies on the first query for a given set of parameters, so when cached there is no issue. I am not sure what to ...
13
votes
2answers
9k views

Configuration parameter work_mem in PostgreSQL on Linux

I have to optimize queries by tuning basic PostgreSQL server configuration parameters. In documentation I've came across the work_mem parameter. Then I checked how changing this parameter would ...
1
vote
0answers
29 views

Why is my SQL statement so slow executed on Postgre?

I have a table with 1.5 million rows, now I need to do a data migration on the table: First add a column on the table, then update the column to be the same as another column in the same table: ...
6
votes
3answers
914 views

Any downsides of using data type “text” for storing strings?

As per Postgres documentation, they support 3 data-types for character data: character varying(n), varchar(n) variable-length with limit character(n), char(n) fixed-length, blank padded ...
1
vote
2answers
656 views

Multicolumn index on 3 fields with heterogenous data types

I have a postgres table with 3 fields: a : postgis geometry b : array varchar[] c : integer and I have a query that involves all of them. I would like to add a multicolumn index to speed it up ...
1
vote
0answers
46 views

Optimizing query and join in PostGIS View

I am developing a webmapping application in which I used PosgreSQL with PostGIS plugin as my DB. I have this two tables namely; evidensapp_polystructures and evidensapp_seniangcbr. Both tables ...
1
vote
1answer
51 views

Optimising Hash And Hash Joins in SQL Query

I have a bunch of tables in PostgreSQL and I run a query as follows: SELECT DISTINCT ON ...some stuff... FROM "rent_flats" INNER JOIN "rent_flats_linked_users" ON ...
0
votes
1answer
65 views

ORDER BY column from right table of LEFT OUTER JOIN

I'm having serious performance issues when using a LEFT OUTER JOIN and trying to use a column in the right table in Postgres. I have a table of users and a table with online_users that lists user ids ...
0
votes
0answers
53 views

what's the cause of this very slow but simple query?

I'm baffled by this, EXPLAIN ANALYZE SELECT DISTINCT "spree_variants"."product_id" FROM "spree_variants" INNER JOIN "spree_prices" ON "spree_prices"."variant_id" = "spree_variants"."id" I ...
1
vote
2answers
75 views

How does Postgres perfom ORDER BY if a b-tree index is built on that field?

I have a table bsort: CREATE TABLE bsort(a int, data text); Here data may be incomplete. In other words, some tuples may not have data value. And then I build a b-tree index on the table: CREATE ...
1
vote
1answer
51 views

Tuning a query in PostgreSQL

What is the best way to tune the below SQL for PostgreSQL which seems to be very costly? Will creating a temporary table gives optimal cost? UPDATE table1 SET id = qry.crmId FROM ( SELECT ...
1
vote
2answers
50 views

How to store and query version of same document in PostgreSQL?

I am storing versions of a document in PostgreSQL 9.4. Every time the user creates a new version, it inserts a row so that I can track all changes over time. Each row shares a reference_id column with ...
3
votes
2answers
108 views

How to increase query efficiency for a large volume of data in a PostgreSQL database?

I have a PostgreSQL database with 1.2 billions rows, attempted to make an application that queries rows a million at a time, with an option to query larger intervals. At first I was just querying a ...
3
votes
4answers
133 views

Spatial query on large table with multiple self joins performing slow

I am working on queries on a large table in Postgres 9.3.9. It is a spatial dataset and it is spatially indexed. Say, I have need to find 3 types of objects: A, B and C. The criteria is that B and C ...
5
votes
1answer
60 views

Storing 'Rank' for Contests in Postgres

I'm trying to determine if there a "low cost" optimization for the following query. We've implemented a system whereby 'tickets' earn 'points' and thus can be ranked. In order to support analytical ...
2
votes
0answers
558 views

JSONB PostgreSQL data type with JAVA - inserts and joins

Trying my hand at JSONB datatype first time(discussion continued from (Join tables using a value inside a JSONB column) on advise from @Erwin , starting new thread) Two tables (obfuscated data and ...
0
votes
1answer
65 views

Table Locking in PostgreSQL

I have a PL/pgSQL function which takes data from a staging table to our target table. The process executes every night. Sometimes due to server restart or some maintenance issues we get the process ...
5
votes
1answer
7k views

Optimize Postgres timestamp query range

I have the following table and indices defined: CREATE TABLE ticket ( wid bigint NOT NULL DEFAULT nextval('tickets_id_seq'::regclass), eid bigint, created timestamp with time zone NOT NULL ...
17
votes
6answers
8k views

Best way to delete millions of rows by ID

I need to delete about 2 million rows from my PG database. I have a list of IDs that I need to delete. However, any way I try to do this is taking days. I tried putting them in a table and doing it ...
0
votes
2answers
80 views

How to speed up the query in PostgreSQL

I have DB in PostgreSQL with a big data (now it is somewhere around 46 GB and the db will keep growing). I created indexes on often used columns and adapted the config file: shared_buffers = 1GB ...
3
votes
2answers
85 views

PostgreSQL not using index on a filtered multiple sort query

I have a pretty simple table CREATE TABLE approved_posts ( project_id INTEGER, feed_id INTEGER, post_id INTEGER, approved_time TIMESTAMP NOT NULL, post_time TIMESTAMP NOT NULL, PRIMARY ...
0
votes
0answers
38 views

PostgreSQL Performance Worsening

I'm running a PostgreSQL DB on a windows VM. Every morning I run some large customer matching algorithms which run in 10-15 minutes. However, recently, the machine has begun to take longer and longer ...