postgresql-performance

How to get unique values from each column based on a condition?

落花浮王杯 提交于 2019-12-05 21:43:19
I have been trying to find an optimal solution to select unique values from each column . My problem is I don't know column names in advance since different table has different number of columns. So first, I have to find column names and I could use below query to do it: select column_name from information_schema.columns where table_name='m0301010000_ds' and column_name like 'c%' Sample output for column names: c1, c2a, c2b, c2c, c2d, c2e, c2f, c2g, c2h, c2i, c2j, c2k, ... Then I would use returned column names to get unique/distinct value in each column and not just distinct row . I know a

Add datetime constraint to a PostgreSQL multi-column partial index

跟風遠走 提交于 2019-12-05 07:20:48
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 many queries a given user has done over the past 30 days. It is very, very rare that I query these stats for any time older than the most recent 30 days. Here is my question: I've currently created my multi-column index on these two columns by running: CREATE INDEX CONCURRENTLY some_index_name ON queries_query (user_sid, created) But I'd like to further restrict the index to only care about those

Partition pruning based on check constraint not working as expected

。_饼干妹妹 提交于 2019-12-04 16:39:14
Why is the table "events_201504" included in the query plan below? Based on my query and the check constraint on that table I would expect the query planner to be able to prune it entirely: database=# \d events_201504 Table "public.events_201504" Column | Type | Modifiers ---------------+-----------------------------+--------------------------------------------------------------- id | bigint | not null default nextval('events_id_seq'::regclass) created_at | timestamp without time zone | Indexes: "events_201504_pkey" PRIMARY KEY, btree (id) "events_201504_created_at" btree (created_at) Check

Faster search for records where 1st character of field doesn't match [A-Za-z]?

放肆的年华 提交于 2019-12-04 08:03:30
I currently have the following: User (id, fname, lname, deleted_at, guest) I can query for a list of user's by their fname initial like so: User Load (9.6ms) SELECT "users".* FROM "users" WHERE (users.deleted_at IS NULL) AND (lower(left(fname, 1)) = 's') ORDER BY fname ASC LIMIT 25 OFFSET 0 This is fast thanks to the following index: CREATE INDEX users_multi_idx ON users (lower(left(fname, 1)), fname) WHERE deleted_at IS NULL; What I want to do now is be able to query for all Users that do not start with the letter's A-Z. I got this to work like so: SELECT "users".* FROM "users" WHERE (users

Bitmap Heap Scan performance

孤街醉人 提交于 2019-12-03 23:11:40
问题 I have a big report table. Bitmap Heap Scan step take more than 5 sec. Is there something that I can do? I add columns to the table, does reindex the index that it use will help? I do union and sum on the data, so I don't return 500K records to the client. I use postgres 9.1. Here the explain: Bitmap Heap Scan on foo_table (cost=24747.45..1339408.81 rows=473986 width=116) (actual time=422.210..5918.037 rows=495747 loops=1) Recheck Cond: ((foo_id = 72) AND (date >= '2013-04-04 00:00:00':

How to optimize query postgres

允我心安 提交于 2019-12-02 18:18:51
问题 I am running the following query: SELECT fat.* FROM Table1 fat LEFT JOIN modo_captura mc ON mc.id = fat.modo_captura_id INNER JOIN loja lj ON lj.id = fat.loja_id INNER JOIN rede rd ON rd.id = fat.rede_id INNER JOIN bandeira bd ON bd.id = fat.bandeira_id INNER JOIN produto pd ON pd.id = fat.produto_id INNER JOIN loja_extensao le ON le.id = fat.loja_extensao_id INNER JOIN conta ct ON ct.id = fat.conta_id INNER JOIN banco bc ON bc.id = ct.banco_id LEFT JOIN conciliacao_vendas cv ON fat.empresa

Can PostgreSQL array be optimized for join?

旧街凉风 提交于 2019-12-02 17:02:31
问题 I see that Postgres array is good for performance if the array's element is the data itself, e.g., tag http://shon.github.io/2015/12/21/postgres_array_performance.html How about if I use array as a way to store foreign keys of integer? Barring foreign key constraint problem, is it advisable to store foreign keys with integer array? Apps should optimize for report or analytics. So if the app will end up joining the array to table most of the time, say the app need to show the label/title/name

Indexing strategy for different combinations of WHERE clauses incl. text patterns

為{幸葍}努か 提交于 2019-12-02 14:13:51
问题 Continuation of other question here: How to get date_part query to hit index? When executing the following query, it hits a compound index I created on the datelocal, views, impressions, gender, agegroup fields: SELECT date_part('hour', datelocal) AS hour , SUM(views) FILTER (WHERE gender = 'male') AS male , SUM(views) FILTER (WHERE gender = 'female') AS female FROM reportimpression WHERE datelocal >= '2019-02-01' AND datelocal < '2019-03-01' GROUP BY 1 ORDER BY 1; However, I'd like to be

How to get date_part query to hit index?

假装没事ソ 提交于 2019-12-02 10:46:31
问题 I have yet to be able to get this query to hit an index instead of performing a full scan - I have another query that uses date_part('day', datelocal) against an almost identical table (that table just has a bit less data but same structure) and that one will hit the index I created on the datelocal column (which is a timestamp without timezone). Query (this one performs a parallel seq scan on the table and does a memory quicksort): SELECT date_part('hour', datelocal) AS hour, SUM(CASE WHEN

How to optimize query postgres

℡╲_俬逩灬. 提交于 2019-12-02 10:34:53
I am running the following query: SELECT fat.* FROM Table1 fat LEFT JOIN modo_captura mc ON mc.id = fat.modo_captura_id INNER JOIN loja lj ON lj.id = fat.loja_id INNER JOIN rede rd ON rd.id = fat.rede_id INNER JOIN bandeira bd ON bd.id = fat.bandeira_id INNER JOIN produto pd ON pd.id = fat.produto_id INNER JOIN loja_extensao le ON le.id = fat.loja_extensao_id INNER JOIN conta ct ON ct.id = fat.conta_id INNER JOIN banco bc ON bc.id = ct.banco_id LEFT JOIN conciliacao_vendas cv ON fat.empresa_id = cv.empresa_id AND cv.chavefato = fat.chavefato AND fat.rede_id = cv.rede_id WHERE 1 = 1 AND cv