相关疑难解决方法(0)

在 ORDER BY 和 LIMIT 子句中使用列值

我在 Postgres 9.3 数据库中有这个表:

在此处输入图片说明

我需要使用列的内容order,并limit进行排序和筛选此表。该表将按列排序first_name

最终结果将如下图所示:

在此处输入图片说明

注意:对不起,如果这对你来说很简单,但我无法解决这个问题。所有邮件地址均由 www.mockaroo.com 生成。如果您的地址在此列表中,请不要怪我。

orderlimit列将始终具有相同的数据。order可能是ascdesclimit可能是任何整数值(但所有行都将是相同的值。它来自分组查询。

postgresql order-by

4
推荐指数
1
解决办法
3116
查看次数

简单查询的执行速度非常慢

我尝试为我的数据创建报告,但在大表上真的很慢。

表结构为:

CREATE TABLE posts
(
  id serial NOT NULL,
  project_id integer,
  moderation character varying(255),
  keyword_id integer,
  author_id integer,
  created_at timestamp without time zone,
  updated_at timestamp without time zone,
  server_id character varying(255),
  social_creation_time integer,
  social_id character varying(255),
  network character varying(255),
  mood character varying(255) DEFAULT NULL::character varying,
  url text,
  source_id integer,
  location character varying(255),
  subject_id integer,
  conversation_id integer,
  CONSTRAINT posts_pkey PRIMARY KEY (id)
);

CREATE INDEX index_posts_on_author_id ON posts (author_id);
CREATE INDEX index_posts_on_keyword_id ON posts (keyword_id);
CREATE INDEX index_posts_on_project_id_and_network_and_social_id 
    ON posts …
Run Code Online (Sandbox Code Playgroud)

postgresql explain

3
推荐指数
2
解决办法
2万
查看次数

时间序列数据中罕见 SELECT 与频繁 INSERT 的性能

我有一个简单的时间序列表

movement_history (
    data_id serial,
    item_id character varying (8),
    event_time timestamp without timezone,
    location_id character varying (7),
    area_id character varying (2)
);
Run Code Online (Sandbox Code Playgroud)

我的前端开发人员告诉我,如果他想知道某个项目在给定时间戳的位置,那么成本太高了,因为他必须对表格进行排序。他希望我为下一个事件添加另一个时间戳字段,这样他就不必进行排序。然而,这将使我插入新动作的代码成本增加一倍以上,因为我需要查询该项目的前一个条目,更新该条目,然后插入新数据。

我的插入当然远远超过他的查询频率。而且我从未见过包含下一个事件时间条目的时间序列表。他告诉我我的表坏了,因为他不频繁的查询需要排序。有什么建议?

我不知道他在使用什么查询,但我会这样做:

select * from movement_history 
where event_time <= '1-15-2015'::timestamp  
and item_id = 'H665AYG3' 
order by event_time desc limit 1;
Run Code Online (Sandbox Code Playgroud)

我们目前有大约 15,000 个项目,它们最多每天输入一次。然而,我们很快就会有 50K 的项目,其传感器数据每 1 到 5 分钟更新一次。

我没有看到他的查询经常执行,但是另一个获取托盘当前状态的查询将会执行。

select distinct on (item_id) * 
from movement_history 
order by item_id, event_time desc;
Run Code Online (Sandbox Code Playgroud)

该服务器当前运行的是 9.3,但如果需要,它也可以运行在 9.4 上。

postgresql performance partitioning index-tuning postgresql-performance

3
推荐指数
2
解决办法
694
查看次数

低基数字段:整数或字符串

工单具有以下状态:

new
in_progress
on_hold
closed
Run Code Online (Sandbox Code Playgroud)

我可以创建ticket.status一个字符串 ( on_hold) 或一个唯一的 int( 2)。它被索引。

整数优点:索引中的最小尺寸

Int con:BI 和不断发展的模式的清晰度低(3与 相比closed

String pro:清晰的数据导航

String con:占用更多空间来索引,对于相同的 RAM 性能较低

我想如果字符串索引的基数较低,它不会比 int 索引占用太多空间。如果该字段的基数较低并且不是复合索引的一部分,则选择整数是否过早优化?

我将 Postgres 与 SQLAlchemy、Python ORM 一起使用。

postgresql index database-design datatypes

3
推荐指数
1
解决办法
1304
查看次数

没有表锁的 CLUSTER 的替代方案

由于频繁的新记录和更新记录导致索引和存储碎片,我面临性能下降和存储使用量增加的问题。

VACUUM 没有多大帮助。

不幸的是,CLUSTER 不是一个选项,因为它会导致停机并且 pg_repack 不适用于 AWS RDS。

我正在寻找 CLUSTER 的 hacky 替代品。在我的本地测试中似乎可以正常工作的一个是:

begin;

create temp table tmp_target as select * from target;

delete from target;
insert into target select * from tmp_target order by field1 asc, field2 desc;

drop table tmp_target;

commit;
Run Code Online (Sandbox Code Playgroud)

ctid看起来的顺序是正确的:

select ctid, field1, field2 from target order by ctid;
Run Code Online (Sandbox Code Playgroud)

问题是:这看起来好吗?是否会锁定target表以SELECT查找导致应用程序停机的查询?有没有办法列出事务中涉及的锁?

集群命令锁定只读查询相关的副本

postgresql clustering locking

3
推荐指数
1
解决办法
1063
查看次数

B 树索引中支持最近行查询的最佳排序顺序?

假设我有一个表,其描述如下:

create table my_table (
  id serial, 
  create_date timestamp with time zone default now(),
  data text
);
Run Code Online (Sandbox Code Playgroud)

和这样的查询:

select * from my_table
where create_date >= timestamp with time zone 'yesterday'
Run Code Online (Sandbox Code Playgroud)

理论上哪个索引会更快,为什么?

create index index_a on my_table (create_date);

create index index_b on my_table (create_date DESC);
Run Code Online (Sandbox Code Playgroud)

postgresql index index-tuning postgresql-performance

3
推荐指数
1
解决办法
2899
查看次数

对于 READ 繁重的应用程序,PostgreSQL 是否受益于更多内核或更快的内核?

我一直在试图弄清楚如何配置一个具有 64GB 内存(远大于数据库)的 PostgreSQL 实例,以及我是否应该选择更多内核或更快的内核。(假设通行证总数在 15- 20% 的差异)

一些额外的细节:最密集的 READ 将是通过大量 UNION 的 JSONB。本例中的操作系统是 CentOS 6。

postgresql performance union blob

0
推荐指数
1
解决办法
7082
查看次数