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.
5
votes
1answer
37 views
How to delete many rows from frequently accessed table
I need to delete the majority (say, 90%) of a very large table (say, 5m rows). The other 10% of this table is frequently read, but not written to.
From "Best way to delete millions of rows by ID", I ...
-1
votes
0answers
24 views
PostgreSql - Speed up the query while joining large table
I've a query where I'm joining a master table(few 100 of records) with large table(1,000,000,000 records). Its taking few minutes to come out. is there any way to improve such query in PostgreSQL?
...
1
vote
1answer
44 views
Postgresql not using multi-column indexes (btree_gin)
I am having issues making postgres use my multi-column indexes for full search using the btree_gin extension. This is for a search page for articles. The idea behind the use of btree_gin is to be able ...
4
votes
5answers
179 views
Way to try multiple SELECTs till a result is available?
What if I want to search for a single row in a table with a decrementing precision, e.g. like this:
SELECT * FROM image WHERE name LIKE 'text' AND group_id = 10 LIMIT 1
When this gives me no ...
-1
votes
1answer
19 views
Query performance issue with ST_Intersects()
Following query is taking 3.06min to execute. How to speedup this query? How to use existing index column while executing query?
SELECT samples_collected.talukname,sample_count,TotalSamples from
(
...
6
votes
3answers
994 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
1answer
40 views
Prevent usage of index for a particular query in Postgres
I have a slow query in a Postgres DB. Using explain analyze, I can see that Postgres makes bitmap index scan on two different indexes followed by bitmap AND on the two resulting sets.
Deleting one of ...
1
vote
2answers
296 views
Optimizing SQL query with multiple joins and grouping (Postgres 9.3)
I've browsed around some other posts and managed to make my queries run a bit faster. However, I've come to a loss as to how to further optimize this query. I'm going to be using it on a website where ...
1
vote
2answers
207 views
Table Partitioning vs non-Partitioned Table with many indexes
I have a DB "DB_One" with a Master Table called t_d_gate_out with 8 indexes on it. I created another DB with partitioned t_d_gate_out (let's call it "DB_Two"). It was partitioned by month and year ...
3
votes
0answers
38 views
PostgreSQL Index Use On Joins
There are numerous posts on the use of indexes, and I'm trying to understand, but I'm hopeful someone can help with my specific issue. I have a table:
CREATE TABLE f4111
(
costcenter_ilmcu ...
1
vote
3answers
2k views
Simple Postgres queries running slow on Heroku
I have a Movie model with around 15,000 items and a Dvd model with 3500.
The following queries are simple Rails associations using a Crane Postgres db running on Heroku. I'm wondering why the ...
0
votes
1answer
35 views
Query plan & perf. variations on another machine
I'm not a PostgreSQL expert, and I've been struggling with this.
I have a rather simple query:
SELECT exists(SELECT 1 FROM res_users_log WHERE create_uid=u.id), count(1)
FROM res_users u
WHERE ...
0
votes
0answers
34 views
PostgreSQL 9.4 Index speed very slow on Select
The box running PostgreSQL 9.4 is a 32 core system at 3.5ghz per core with 128GB or ram with mirrored Samsung pro 850 SSD drives on FreeBSD with ZFS. This is no reason for this poor of performance ...
4
votes
2answers
47 views
Postgres not using index when index scan is much better option
I have a simple query to join two tables that's being really slow. I found out that the query plan does a seq scan on the large table email_activities (~10m rows) while I think using indexes doing ...
2
votes
1answer
114 views
Efficient PostgreSQL query on timestamp using index or bitmap index scan?
In PostgreSQL, I have an index on a date field on my tickets table.
When I compare the field against now(), the query is pretty efficient:
# explain analyze select count(1) as count from tickets ...
2
votes
1answer
46 views
Optimize Postgres Query with Indexes for large amounts of data
I've got a database, posts that has about 20 million rows in it. I'm trying to narrow down the posts for a paginated list using the following query:
SELECT "posts".* FROM "posts"
WHERE ...
1
vote
1answer
20 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
8k 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
637 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
29 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
30 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
29 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
92 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
52 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
6k 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
86 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
68 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
69 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
219 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
25 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 ...
8
votes
1answer
101 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
40 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
51 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
77 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
35 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
49 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
50 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 ...
22
votes
4answers
19k 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
48 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
71 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
51 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
48 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
91 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
56 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
121 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 ...
14
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
30 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:
...
1
vote
2answers
728 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
51 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
53 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 ...