postgresql-performance

PostgreSQL query taking too long

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 16:46:53
问题 I have database with few hundred millions of rows. I'm running the following query: select * from "Payments" as p inner join "PaymentOrders" as po on po."Id" = p."PaymentOrderId" inner join "Users" as u On u."Id" = po."UserId" INNER JOIN "Roles" as r on u."RoleId" = r."Id" Where r."Name" = 'Moses' LIMIT 1000 When the where clause finds a match in database, I get the result in several milliseconds, but if I modify the query and specify a non-existent r."Name" in where clause, it takes too much

Postgres using an index for one table but not another

会有一股神秘感。 提交于 2019-12-18 09:48:47
问题 I have three tables in my app, call them tableA , tableB , and tableC . tableA has fields for tableB_id and tableC_id , with indexes on both. tableB has a field foo with an index, and tableC has a field bar with an index. When I do the following query: select * from tableA left outer join tableB on tableB.id = tableA.tableB_id where lower(tableB.foo) = lower(my_input) it is really slow (~1 second). When I do the following query: select * from tableA left outer join tableC on tableC.id =

PostgreSQL window function: partition by comparison

有些话、适合烂在心里 提交于 2019-12-17 18:44:24
问题 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

Why does the following join increase the query time significantly?

落花浮王杯 提交于 2019-12-17 17:05:35
问题 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

Running PostgreSQL in memory only

本秂侑毒 提交于 2019-12-17 03:54:10
问题 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(); connectTo("postgres://localhost:"+port+"/in_memory_db"); // ... } Ideally I'll have a single postgres executable checked into the version control, which the unit test will use. Something like HSQL , but for postgres. How can I do that? Were can I get such a Postgres version? How can I instruct it not to use the disk? 回答1

Performance impact of empty LIKE in a prepared statement

落爺英雄遲暮 提交于 2019-12-14 02:37:58
问题 I have set a GiST pg_trgm index on the name column of the files table. The simplified query of the prepared statement looks like this: SELECT * FROM files WHERE name LIKE $1; The $1 param will be composed of % + user query + % . Since the input might also be an empty string this might result in %% . Does an "empty" LIKE ( %% ) result in performance degradation? Should I build a new query in that case, or does it not matter? 回答1: Postgres 9.2 or later is generally smart enough to realize that

Many to Many Table - Performance is bad

Deadly 提交于 2019-12-13 01:18:24
问题 The following tables are given: --- player -- id serial name VARCHAR(100) birthday DATE country VARCHAR(3) PRIMARY KEY id --- club --- id SERIAL name VARCHAR(100) country VARCHAR(3) PRIMARY KEY id --- playersinclubs --- id SERIAL player_id INTEGER (with INDEX) club_id INTEGER (with INDEX) joined DATE left DATE PRIMARY KEY id Every player has a row in table player (with his attributes). Equally every club has an entry in table club. For every station in his career, a player has an entry in

Order BY turns a 30ms query into a 7120ms query. Known performance issue?

China☆狼群 提交于 2019-12-12 16:27:59
问题 I have a User table with 1m records: User (id, fname, lname, deleted_at, guest) I have the following query which is being run against a postgres 9.1 db: SELECT "users".* FROM "users" WHERE (users.deleted_at IS NULL) AND (SUBSTRING(lower(fname), 1, 1) = 's') ORDER BY guest = false, fname ASC LIMIT 25 OFFSET 0 Using pgAdmin 3, this SQL is taking 7120ms to return 25 rows. If I remove the 'ORDER BY guest = false, fname ASC' the query takes just 31ms . I have the following indexes: add_index

Window functions filter through current row

不想你离开。 提交于 2019-12-12 16:08:11
问题 This is a follow-up to this question, where my query was improved to use window functions instead of aggregates inside a LATERAL join. While the query is now much faster, I've found that the results are not correct. I need to perform computations on x year trailing time frames. For example, price_to_maximum_earnings is computed per row by getting max(earnings) over ten years ago to the current row, and dividing price by the result. We'll use 1 year for simplicity here. SQL Fiddle for this

How to speed up the query in PostgreSQL

时光怂恿深爱的人放手 提交于 2019-12-11 12:29:18
问题 I have DB in PostgreSQL with a big data (now it is somewhere around 46 GB and the db will keep growing). I created indexes on often used columns and adapted the config file: shared_buffers = 1GB temp_buffers = 256MB work_mem = 512MB But this query is still slow: select distinct us_category_id as cat, count(h_user_id) as res from web_hits inner join users on h_user_id = us_id where (h_datetime)::date = ('2015-06-26')::date and us_category_id != '' group by us_category_id Explain Analyze: