postgresql-performance

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

↘锁芯ラ 提交于 2020-02-02 15:22:35
问题 I have a Postgres 9.4 database with a table like this: | id | other_id | current | dn_ids | rank | |----|----------|---------|---------------------------------------|------| | 1 | 5 | F | {123,234,345,456,111,222,333,444,555} | 1 | | 2 | 7 | F | {123,100,200,900,800,700,600,400,323} | 2 | (update) I already have a couple indexes defined. Here is the CREATE TABLE syntax: CREATE TABLE mytable ( id integer NOT NULL, other_id integer, rank integer, current boolean DEFAULT false, dn_ids integer[]

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

感情迁移 提交于 2020-02-02 15:19:26
问题 I have a Postgres 9.4 database with a table like this: | id | other_id | current | dn_ids | rank | |----|----------|---------|---------------------------------------|------| | 1 | 5 | F | {123,234,345,456,111,222,333,444,555} | 1 | | 2 | 7 | F | {123,100,200,900,800,700,600,400,323} | 2 | (update) I already have a couple indexes defined. Here is the CREATE TABLE syntax: CREATE TABLE mytable ( id integer NOT NULL, other_id integer, rank integer, current boolean DEFAULT false, dn_ids integer[]

Proper way to access latest row for each individual identifier?

可紊 提交于 2020-01-11 10:45:12
问题 I have a table core_message in Postgres, with millions of rows that looks like this (simplified): ┌────────────────┬──────────────────────────┬─────────────────┬───────────┬──────────────────────────────────────────┐ │ Colonne │ Type │ Collationnement │ NULL-able │ Par défaut │ ├────────────────┼──────────────────────────┼─────────────────┼───────────┼──────────────────────────────────────────┤ │ id │ integer │ │ not null │ nextval('core_message_id_seq'::regclass) │ │ mmsi │ integer │ │ not

Better way than multiple SELECT statements?

自作多情 提交于 2020-01-06 01:30:08
问题 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 UNION ALL — here's a portion: SELECT 'spf' as type, COUNT(*) FROM (SELECT cai.id FROM common_activityinstance cai JOIN common_activityinstance_settings cais ON cai.id = cais.activityinstance_id JOIN common_activitysetting cas ON cas.id = cais.id JOIN quizzes_quiz q ON q.id = cai.activity_id WHERE cai

Why PostgreSQL queries are slower in the first request after server start than during the subsequent requests?

£可爱£侵袭症+ 提交于 2020-01-05 11:07:11
问题 I'm using PostgreSQL 9.1.1 and Rails 3.2.8. Using the development mode of NewRelic I've noticed that several SQL queries takes much longer during the first request following my server start or restart than during the subsequent requests. Is there any reason for that, is that due to prepared statements? 回答1: Just after start none of the indexes is loaded in memory, so the server will have to do a lot of very slow disk reads. As activity progresses more and more index pages get loaded in memory

Postgresql becomes unresponsible when new index value is added

只愿长相守 提交于 2020-01-05 05:57:16
问题 In my app I have a concept of "seasons" which change discretely over time. All the entities are related to some season. All entities have season based indices as well as some indices on other fields. When season change occurs, postgresql decides to use filtered scan plan based on season index rather than more specific field indices. At the beginning of the season the planning cost of such decision is very little, so it's ok, but the problem is - season change brings MANY users to come at the

Efficient PostgreSQL query on timestamp using index or bitmap index scan?

自作多情 提交于 2020-01-02 09:13:33
问题 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 where updated_at > now(); QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=90.64..90.66 rows=1 width=0) (actual time=33.238..33.238 rows=1 loops=1) -> Index Scan using tickets

Execute multiple functions together without losing performance

不羁的心 提交于 2019-12-29 07:31:15
问题 I have this process that has to make a series of queries, using pl/pgsql: --process: SELECT function1(); SELECT function2(); SELECT function3(); SELECT function4(); To be able to execute everything in one call, I created a process function as such: CREATE OR REPLACE FUNCTION process() RETURNS text AS $BODY$ BEGIN PERFORM function1(); PERFORM function2(); PERFORM function3(); PERFORM function4(); RETURN 'process ended'; END; $BODY$ LANGUAGE plpgsql The problem is, when I sum the time that each

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

守給你的承諾、 提交于 2019-12-28 04:29:12
问题 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 INDEX ON bsort USING BTREE(a); Now if I perform this query: SELECT * FROM bsort ORDER BY a; Does PostgreSQL sort tuples with nlogn complexity, or does it get the order directly from the b-tree index? 回答1: For a simple query like this Postgres will use an index scan and retrieve readily sorted tuples

postgres not using index

本小妞迷上赌 提交于 2019-12-25 08:38:50
问题 There are lots of questions on this topic, but all of them seem to be more complex cases than what I'm looking at at the moment and the answers don't seem applicable. OHDSI=> \d record_counts Table "results2.record_counts" Column | Type | Modifiers ------------------------+-----------------------+----------- concept_id | integer | schema | text | table_name | text | column_name | text | column_type | text | descendant_concept_ids | bigint | rc | numeric | drc | numeric | domain_id | character