postgresql-performance

Postgres NOT IN performance

删除回忆录丶 提交于 2019-11-29 03:49:53
问题 Any ideas how to speed up this query? Input EXPLAIN SELECT entityid FROM entity e LEFT JOIN level1entity l1 ON l1.level1id = e.level1_level1id LEFT JOIN level2entity l2 ON l2.level2id = l1.level2_level2id WHERE l2.userid = 'a987c246-65e5-48f6-9d2d-a7bcb6284c8f' AND (entityid NOT IN (1377776,1377792,1377793,1377794,1377795,1377796... 50000 ids) ) Output Nested Loop (cost=0.00..1452373.79 rows=3865 width=8) -> Nested Loop (cost=0.00..8.58 rows=1 width=8) Join Filter: (l1.level2_level2id = l2

Improving query speed: simple SELECT in big postgres table

孤街浪徒 提交于 2019-11-28 16:46:39
问题 I'm having trouble regarding speed in a SELECT query on a Postgres database. I have a table with two integer columns as key: (int1,int2) This table has around 70 million rows. I need to make two kind of simple SELECT queries in this environment: SELECT * FROM table WHERE int1=X; SELECT * FROM table WHERE int2=X; These two selects returns around 10.000 rows each out of these 70 million. For this to work as fast as possible I thought on using two HASH indexes, one for each column. Unfortunately

Why is PostgreSQL not using my indexes on a small table?

白昼怎懂夜的黑 提交于 2019-11-28 14:10:41
I have the following table in PostgreSQL: CREATE TABLE index_test ( id int PRIMARY KEY NOT NULL, text varchar(2048) NOT NULL, last_modified timestamp NOT NULL, value int, item_type varchar(2046) ); CREATE INDEX idx_index_type ON index_test ( item_type ); CREATE INDEX idx_index_value ON index_test ( value ) I make the following selects: explain select * from index_test r where r.item_type='B'; explain select r.value from index_test r where r.value=56; The explanation of execution plan looks like this: Seq Scan on index_test r (cost=0.00..1.04 rows=1 width=1576) Filter: ((item_type)::text = 'B':

Optimize BETWEEN date statement

这一生的挚爱 提交于 2019-11-28 11:40:41
I need help in optimize a PostgreSQL query which uses the BETWEEN clause with a timestamp field. I have 2 tables: ONE(int id_one(PK), datetime cut_time, int f1 . . .) containing about 3394 rows TWO(int id_two(PK), int id_one(FK), int f2 . . .) containing about 4000000 rows There are btree indexes on both PKs id_one and id_two , on the FK id_one and cut_time . I want to perform a query like: select o.id_one, Date(o.cut_time), o.f1, t.f2 from one o inner join two t ON (o.id_one = t.id_one) where o.cut_time between '2013-01-01' and '2013-01-31'; This query retrieves about 1.700.000 rows in about

Postgres query optimization (forcing an index scan)

谁说胖子不能爱 提交于 2019-11-28 08:56:53
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 '2012-02-05 00:00:00'::TIMESTAMP AND '2012-05-05 00:00:00'::TIMESTAMP; EXPLAIN: Hash Join (cost=271.30.

PostgreSQL window function: partition by comparison

故事扮演 提交于 2019-11-28 08:49:26
I'm trying to find the way of doing a comparison with the current row in the PARTITION BY clause in a WINDOW function in PostgreSQL query. Imagine I have the short list in the following query of this 5 elements (in the real case, I have thousands or even millions of rows). I am trying to get for each row, the id of the next different element (event column), and the id of the previous different element. WITH events AS( SELECT 1 as id, 12 as event, '2014-03-19 08:00:00'::timestamp as date UNION SELECT 2 as id, 12 as event, '2014-03-19 08:30:00'::timestamp as date UNION SELECT 3 as id, 13 as

Postgres not using index when index scan is much better option

微笑、不失礼 提交于 2019-11-28 08:30:21
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_activities while setting the limit in subquery to even 44k will cause query plan to use seq scan on email

Indexed ORDER BY with LIMIT 1

余生颓废 提交于 2019-11-28 07:58:03
问题 I'm trying to fetch most recent row in a table. I have a simple timestamp created_at which is indexed. When I query ORDER BY created_at DESC LIMIT 1 , it takes far more than I think it should (about 50ms on my machine on 36k rows). EXPLAIN -ing claims that it uses backwards index scan , but I confirmed that changing the index to be (created_at DESC) does not change the cost in query planner for a simple index scan . How can I optimize this use case? Running postgresql 9.2.4 . Edit: # EXPLAIN

SQL function very slow compared to query without function wrapper

跟風遠走 提交于 2019-11-28 07:24:51
问题 I have this PostgreSQL 9.4 query that runs very fast (~12ms): SELECT auth_web_events.id, auth_web_events.time_stamp, auth_web_events.description, auth_web_events.origin, auth_user.email, customers.name, auth_web_events.client_ip FROM public.auth_web_events, public.auth_user, public.customers WHERE auth_web_events.user_id_fk = auth_user.id AND auth_user.customer_id_fk = customers.id AND auth_web_events.user_id_fk = 2 ORDER BY auth_web_events.id DESC; But if I embed it into a function, the

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

微笑、不失礼 提交于 2019-11-28 05:02:33
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=72) (actual time=28.726..65.056 rows=3398 loops=1) -> Index Scan using replays_playeringame_player_id on