AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-247746

birgersp's questions

Martin Hope
birgersp
Asked: 2024-06-05 19:48:03 +0800 CST

时间索引应该按升序还是降序排列?

  • 6

规划一些新表,我试图决定索引应该是“升序”还是“降序”。

该表将非常大(我想象每分钟大约有 2000 次插入,最初从大约有 10 亿行的不同表迁移)。

我将为此使用 timescaledb 扩展(按时间进行分区)。

这是创建表的方式:

create table "Sample"(
    "id" bigserial,
    "deviceId" int not null,
    "timestamp" timestamptz not null,
    "value" float8 not null
);
select create_hypertable('"Sample"', 'timestamp'); -- creates a desc index on "timestamp"

create index on "Sample"("deviceId", "timestamp"); -- should this be "desc"?

这是我们将运行的两个最常见的查询(当然,deviceId 和时间戳可能会有所不同):

select "timestamp", "value"
from "Sample" where "deviceId"=123 and "timestamp"<'2024-01-01Z'
order by "timestamp" desc limit 1;

和

select "timestamp", "value"
from "Sample" where "deviceId"=123 and "timestamp" between '2024-01-01Z' and '2024-02-01Z'
order by "timestamp" asc;

所以我想了解的是“时间戳”应该按什么顺序排列?为什么?

我的(可能错误的)直觉告诉我带有“时间戳”的索引应该按升序排列,因为我需要按时间戳升序对数据进行排序。

但是,时间刻度文档中的示例总是按降序对时间列进行索引。我不太明白为什么。

这里理想的指数选择是什么?

postgresql
  • 1 个回答
  • 36 Views
Martin Hope
birgersp
Asked: 2022-02-17 23:01:27 +0800 CST

如何添加到表中,然后将 id 用于另一个查询?

  • 0

我有三个表,我使用这些查询创建:

create table parents(
    rowid serial primary key,
    display_name varchar,
    unique(display_name)
);

create table clients(
    rowid serial primary key,
    parent int,
    display_name varchar,
    foreign key (parent) references parents(rowid),
    unique(display_name, parent)
);

create table datapoints(
    rowid serial primary key,
    client int,
    val float8,
    foreign key (client) references clients(rowid)
);

我正在获取包含父名称、客户端名称和一些值的数据。每次获得这些数据时,我都想向数据点表中添加行。我还想向 clients 和 parents 表添加行,但前提是我收到的数据具有无法识别的名称。

例如,我可能会得到以下数据:

“parent1-client1-123.0”

我想要实现的是处理这些数据(在查询中),这种方式:

如果需要,在“父母”表中插入一行。

如果需要,使用适当的父 ID(在上一步中获得?)向“客户”表插入一行。

使用适当的客户端 ID(在上一步中获得?)在“数据点”表中插入一行。

我如何通过查询来管理它?我的程序员想写:

  • 如果需要,插入父母。将适当父级的 ID 保存在变量“current_parent”中
  • 如果需要,使用“current_parent”变量插入客户端。将相应客户端的 ID 保存在变量“current_client”中。
  • 使用上一页中的“current_client”变量插入数据点。步。

但显然这不是 sql 的工作方式,对吧?如果我有 5 个或 10 个而不是这三个“嵌套”表呢?(!)

请帮忙

postgresql insert
  • 1 个回答
  • 45 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve