以下是我的查询.我试图让它使用索引扫描,但它只会seq扫描.
顺便说一下,这个metric_data表有1.3亿行.该metrics表有大约2000行.
metric_data 表格列:
metric_id integer
, t timestamp
, d double precision
, PRIMARY KEY (metric_id, t)
Run Code Online (Sandbox Code Playgroud)
如何让此查询使用我的PRIMARY KEY索引?
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;
Run Code Online (Sandbox Code Playgroud)
说明:
Hash Join (cost=271.30..3866384.25 rows=294973 width=25)
Hash Cond: (d.metric_id = s.id)
-> Seq Scan on metric_data d (cost=0.00..3753150.28 rows=29336784 width=20)
Filter: ((t >= '2012-02-05 00:00:00'::timestamp without …Run Code Online (Sandbox Code Playgroud) postgresql indexing query-optimization postgresql-9.1 postgresql-performance
我正在运行Postgres 9.6.1和PostGIS 2.3.0 r15146并且有两个表.
geographies可能有150,000,000行,paths可能有10,000,000行:
CREATE TABLE paths (id uuid NOT NULL, path path NOT NULL, PRIMARY KEY (id))
CREATE TABLE geographies (id uuid NOT NULL, geography geography NOT NULL, PRIMARY KEY (id))
Run Code Online (Sandbox Code Playgroud)
给定一个数组/套ids的表geographies,什么是查找所有交叉路径与几何的"最佳"的方式?
换句话说,如果一个初始geography有一个相应的交叉,path我们还需要找到这个相交的所有其他 .从那里,我们需要找到这些新发现的所有其他相交,依此类推,直到我们找到所有可能的交叉点.geographiespathpathsgeographies
初始地理标识(我们的输入)可以是0到700之间的任何位置.平均值大约为40.
最小交叉点将为0,最大值将为大约1000.平均值可能大约为20,通常小于100连接.
我创建了一个这样做的功能,但我是PostGIS中的GIS和Postgres的新手.我发布了我的解决方案作为这个问题的答案.
我觉得应该有比我想出的更有说服力和更快速的方式.
我有一个简单的查询来连接两个非常慢的表.我发现查询计划在大表email_activities(~10m行)上执行seq扫描,而我认为使用嵌套循环的索引实际上会更快.
我使用子查询重写了查询,试图强制使用索引,然后注意到一些有趣的东西.如果您查看下面的两个查询计划,您将看到当我将子查询的结果集限制为43k时,查询计划确实使用了email_activities上的索引,而将子查询中的限制设置为甚至44k将导致查询计划使用seq扫描email_activities.一个显然比另一个更有效,但Postgres似乎并不关心.
什么可能导致这个?如果其中一个集合大于特定大小,它是否在某处强制使用散列连接?
explain analyze SELECT COUNT(DISTINCT "email_activities"."email_recipient_id") FROM "email_activities" where email_recipient_id in (select "email_recipients"."id" from email_recipients WHERE "email_recipients"."email_campaign_id" = 1607 limit 43000);
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=118261.50..118261.50 rows=1 width=4) (actual time=224.556..224.556 rows=1 loops=1)
-> Nested Loop (cost=3699.03..118147.99 rows=227007 width=4) (actual time=32.586..209.076 rows=40789 loops=1)
-> HashAggregate (cost=3698.94..3827.94 rows=43000 width=4) (actual time=32.572..47.276 rows=43000 loops=1)
-> Limit (cost=0.09..3548.44 rows=43000 width=4) (actual time=0.017..22.547 rows=43000 loops=1)
-> Index Scan using index_email_recipients_on_email_campaign_id on email_recipients (cost=0.09..5422.47 rows=65710 width=4) (actual time=0.017..19.168 rows=43000 loops=1) …Run Code Online (Sandbox Code Playgroud) 我有这个过程,必须使用pl/pgsql进行一系列查询:
--process:
SELECT function1();
SELECT function2();
SELECT function3();
SELECT function4();
Run Code Online (Sandbox Code Playgroud)
为了能够在一次调用中执行所有操作,我创建了一个过程函数:
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
Run Code Online (Sandbox Code Playgroud)
问题是,当我总结每个函数自身所用的时间时,总计为200秒,而函数所process()用的时间超过一个小时!
也许这是一个内存问题,但我不知道postgresql.conf应该改变哪种配置.
DB在Debian 8中的PostgreSQL 9.4上运行.
postgresql plpgsql database-performance query-performance postgresql-performance
我创建了一个 36M 行的表,列上有一个索引type:
CREATE TABLE items AS
SELECT
(random()*36000000)::integer AS id,
(random()*10000)::integer AS type,
md5(random()::text) AS s
FROM
generate_series(1,36000000);
CREATE INDEX items_type_idx ON items USING btree ("type");
Run Code Online (Sandbox Code Playgroud)
我运行这个简单的查询并期望 postgresql 使用我的索引:
explain select count(*) from "items" group by "type";
Run Code Online (Sandbox Code Playgroud)
但是查询计划器决定使用 Seq Scan 代替:
HashAggregate (cost=734592.00..734627.90 rows=3590 width=12) (actual time=6477.913..6478.344 rows=3601 loops=1)
Group Key: type
-> Seq Scan on items (cost=0.00..554593.00 rows=35999800 width=4) (actual time=0.044..1820.522 rows=36000000 loops=1)
Planning time: 0.107 ms
Execution time: 6478.525 ms
Run Code Online (Sandbox Code Playgroud)
无解释时间: 5s 979ms
我从这里 …
采取以下两个表:
Table "public.contacts"
Column | Type | Modifiers | Storage | Stats target | Description
--------------------+-----------------------------+-------------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('contacts_id_seq'::regclass) | plain | |
created_at | timestamp without time zone | not null | plain | |
updated_at | timestamp without time zone | not null | plain | |
external_id | integer | | plain | |
email_address | character varying | | extended | |
first_name | character varying | | extended | | …Run Code Online (Sandbox Code Playgroud) 我有一个问题:
EXPLAIN ANALYZE
SELECT CAST(DATE(associationtime) AS text) AS date ,
cast(SUM(extract(epoch
FROM disassociationtime) - extract(epoch
FROM associationtime)) AS bigint) AS sessionduration,
cast(SUM(tx) AS bigint)AS tx,
cast(SUM(rx) AS bigint) AS rx,
cast(SUM(dataRetries) AS bigint) AS DATA,
cast(SUM(rtsRetries) AS bigint) AS rts,
count(*)
FROM SESSION
WHERE ssid_id=42
AND ap_id=1731
AND DATE(associationtime)>=DATE('Tue Nov 04 00:00:00 MSK 2014')
AND DATE(associationtime)<=DATE('Thu Nov 20 00:00:00 MSK 2014')
GROUP BY(DATE(associationtime))
ORDER BY DATE(associationtime);
Run Code Online (Sandbox Code Playgroud)
输出是:
GroupAggregate (cost=0.44..17710.66 rows=1 width=32) (actual time=4.501..78.880 rows=17 loops=1)
-> Index Scan using session_lim_values_idx …Run Code Online (Sandbox Code Playgroud) postgresql indexing sql-execution-plan postgresql-performance
我在 Postgres 数据库中有一个缓慢的查询。使用explain analyze,我可以看到 Postgres 对两个不同的索引进行位图索引扫描,然后对两个结果集进行位图 AND 扫描。
删除其中一个索引会使评估速度加快十倍(第一个索引仍使用位图索引扫描)。但是,删除的索引在其他查询中很有用。
询问:
select
booking_id
from
booking
where
substitute_confirmation_token is null
and date_trunc('day', from_time) >= cast('01/25/2016 14:23:00.004' as date)
and from_time >= '01/25/2016 14:23:00.004'
and type = 'LESSON_SUBSTITUTE'
and valid
order by
booking_id;
Run Code Online (Sandbox Code Playgroud)
索引:
"idx_booking_lesson_substitute_day" btree (date_trunc('day'::text, from_time)) WHERE valid AND type::text = 'LESSON_SUBSTITUTE'::text
"booking_substitute_confirmation_token_key" UNIQUE CONSTRAINT, btree (substitute_confirmation_token)
Run Code Online (Sandbox Code Playgroud)
查询计划:
Sort (cost=287.26..287.26 rows=1 width=8) (actual time=711.371..711.377 rows=44 loops=1)
Sort Key: booking_id
Sort Method: quicksort Memory: 27kB
Buffers: shared hit=8 read=7437 written=1 …Run Code Online (Sandbox Code Playgroud) sql postgresql indexing query-optimization postgresql-performance
我注意到我的一个 SQL 查询比我预期的要慢得多,结果查询计划程序提出了一个对我来说似乎很糟糕的计划。我的查询如下所示:
select A.style, count(B.x is null) as missing, count(*) as total
from A left join B using (id, type)
where A.country_code in ('US', 'DE', 'ES')
group by A.country_code, A.style
order by A.country_code, total
Run Code Online (Sandbox Code Playgroud)
B 有一个 (type, id) 索引,A 有一个 (country_code, style) 索引。A 比 B 小得多:A 中有 250K 行,B 中有 100M。
所以,我希望查询计划看起来像:
country_code(type, id)索引查找匹配行(如果有)country_code和分组事物style但是查询规划器决定执行此操作的最佳方法是对 B 进行顺序扫描,然后对 A 进行右连接。我无法理解为什么会这样;有没有人有想法?这是它生成的实际查询计划:
Sort (cost=14283513.27..14283513.70 rows=171 width=595)
Sort Key: a.country_code, …Run Code Online (Sandbox Code Playgroud) 位图扫描的作者描述了位图堆扫描和索引扫描之间的区别:
一个普通的indexscan一次从索引中获取一个元组指针,然后立即访问表中的该元组。位图扫描一次性从索引中获取所有元组指针,使用内存中的“位图”数据结构对其进行排序,然后以物理元组位置顺序访问表元组。位图扫描提高了表的引用局部性,但要花更多的簿记开销来管理“位图”数据结构---并且不再按索引顺序检索数据,这对您来说无关紧要查询,但是如果您说ORDER BY会很重要。
问题:
当索引已排序时,为什么又对获取的元组指针进行排序?
它如何与位图排序?我知道位图是什么,但是我不知道如何将其用于排序。
为何要获取中等比例的表,为什么它比索引扫描更快?相反,它似乎为该过程增加了很多计算。
postgresql ×10
indexing ×6
sql ×5
gis ×1
group-by ×1
performance ×1
plpgsql ×1
postgis ×1
sqlgeography ×1