postgresql-performance

How to understand an EXPLAIN ANALYZE

南笙酒味 提交于 2019-11-28 04:53:50
I am not very familiar with looking at EXPLAIN ANALYZE results, I have a huge problem with my queries being too slow. I have tried to read up on how to interpret results from an explain queries, but I still don't know what I should be looking for, and what might be wrong. I have a feeling that there is some big red light flashing somewhere, I just don't see it. So the query is pretty simple, it looks like this: EXPLAIN ANALYZE SELECT "cars".* FROM "cars" WHERE "cars"."sales_state" = 'onsale' AND "cars"."brand" = 'BMW' AND "cars"."model_name" = '318i' AND "cars"."has_auto_gear" = TRUE LIMIT 25

Why does the following join increase the query time significantly?

天涯浪子 提交于 2019-11-28 02:15:41
I have a star schema here and I am querying the fact table and would like to join one very small dimension table. I can't really explain the following: EXPLAIN ANALYZE SELECT COUNT(impression_id), imp.os_id FROM bi.impressions imp GROUP BY imp.os_id; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=868719.08..868719.24 rows=16 width=10) (actual time=12559.462..12559.466 rows=26 loops=1) -> Seq Scan on impressions imp (cost=0.00..690306.72 rows=35682472 width=10) (actual time=0

Configuration parameter work_mem in PostgreSQL on Linux

送分小仙女□ 提交于 2019-11-27 13:38:44
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 influence performance of my query (using sort). I measured query execution time with various work_mem settings and was very disappointed. The table on which I perform my query contains 10,000,000 rows and there are 430 MB of data to sort. ( Sort Method: external merge Disk: 430112kB ). With work_mem = 1MB , EXPLAIN output is: Total runtime: 29950.571 ms (sort takes about 19300 ms). Sort (cost=4032588.78

How to understand an EXPLAIN ANALYZE

随声附和 提交于 2019-11-27 05:24:39
问题 I am not very familiar with looking at EXPLAIN ANALYZE results, I have a huge problem with my queries being too slow. I have tried to read up on how to interpret results from an explain queries, but I still don't know what I should be looking for, and what might be wrong. I have a feeling that there is some big red light flashing somewhere, I just don't see it. So the query is pretty simple, it looks like this: EXPLAIN ANALYZE SELECT "cars".* FROM "cars" WHERE "cars"."sales_state" = 'onsale'

Postgres query optimization (forcing an index scan)

落爺英雄遲暮 提交于 2019-11-27 02:38:04
问题 Below is my query. I am trying to get it to use an index scan, but it will only seq scan. By the way the metric_data table has 130 million rows. The metrics table has about 2000 rows. metric_data table columns: metric_id integer , t timestamp , d double precision , PRIMARY KEY (metric_id, t) How can I get this query to use my PRIMARY KEY index? SELECT S.metric, D.t, D.d FROM metric_data D INNER JOIN metrics S ON S.id = D.metric_id WHERE S.NAME = ANY (ARRAY ['cpu', 'mem']) AND D.t BETWEEN

Postgres not using index when index scan is much better option

浪尽此生 提交于 2019-11-27 01:42:35
问题 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 nested loops will actually be faster. I rewrote the query using a subquery in an attempt to force the use of index, then noticed something interesting. If you look at the two query plans below, you will see that when I limit the result set of subquery to 43k, query plan does use index on email

PostgreSQL query runs faster with index scan, but engine chooses hash join

…衆ロ難τιáo~ 提交于 2019-11-27 00:46:00
问题 The query: SELECT "replays_game".* FROM "replays_game" INNER JOIN "replays_playeringame" ON "replays_game"."id" = "replays_playeringame"."game_id" WHERE "replays_playeringame"."player_id" = 50027 If I set SET enable_seqscan = off , then it does the fast thing, which is: QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.00..27349.80 rows=3395 width

Best way to delete millions of rows by ID

时间秒杀一切 提交于 2019-11-27 00:12:35
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 in batches of 100. 4 days later, this is still running with only 297268 rows deleted. (I had to select 100 id's from an ID table, delete where IN that list, delete from ids table the 100 I selected). I tried: DELETE FROM tbl WHERE id IN (select * from ids) That's taking forever, too. Hard to gauge how long, since I can't see it's progress till done, but the query was still running after 2 days. Just

Way to try multiple SELECTs till a result is available?

别来无恙 提交于 2019-11-26 21:03:55
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 result, try this one: SELECT * FROM image WHERE name LIKE 'text' LIMIT 1 And when this gives me no result, try this one: SELECT * FROM image WHERE group_id = 10 LIMIT 1 Is it possible to do that with just one expression? Also there arises a problem when I have not two but e.g. three or more search parameters. Is there a generic solution for that? Of course it would come in handy when the search result is

Finding similar strings with PostgreSQL quickly

徘徊边缘 提交于 2019-11-26 18:42:16
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 similarity function, but I have an efficiency problem. I created an index like the Postgres manual suggests : CREATE INDEX trgm_idx ON names USING gist (name gist_trgm_ops); and I'm executing the following query: select (similarity(n1.name, n2.name)) as sim, n1.name, n2.name from names n1, names n2 where n1.name != n2.name and similarity(n1.name, n2.name) > .8 order by sim desc; The query works, but is