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-55628

Dolphin's questions

Martin Hope
Dolphin
Asked: 2025-04-08 18:14:48 +0800 CST

PostgreSQL 中如何获取外层查询的行结果

  • 6

我想在 PostgreSQL 选择块中选择多行。然后我尝试使用 row 函数在 PostgreSQL 15 中执行查询,如下所示:

select c.a from ( select row(1 as a, 2 as b) as c)

显示错误ERROR: syntax error at or near "as,我也尝试过:

select (c).a from ( select row(1 , 2 ) as c(a,b))

我是不是漏掉了什么?或者如何获取行值?我发现这个方法很好用:

select * from ( select row(1 , 2 ) ) a

如何得到 1 或 2?我试过:

select a[0] from ( select row(1 , 2 ) ) a

似乎没有起作用。

postgresql
  • 1 个回答
  • 13 Views
Martin Hope
Dolphin
Asked: 2023-04-29 22:29:09 +0800 CST

为什么我的 GiST 索引不会触发冲突?[复制]

  • 5
这个问题在这里已经有了答案:
具有日期范围的唯一性约束 (1 个答案)
2 天前关闭。

我的 Postgres 13 数据库中有一个包含用户订阅的表。我添加了一个 GiST 索引来禁止同一用户的重叠时间范围:

CREATE INDEX user_sub_exclusion_index ON public.user_sub USING gist (user_id, tstzrange(sub_start, sub_end))

但是今天我发现它不起作用。
这是表 DDL:

-- DROP TABLE public.user_sub;
CREATE TABLE public.user_sub (
    id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
    app_id varchar NOT NULL,
    product_id int4 NOT NULL,
    iap_product_id int8 NOT NULL,
    created_time int8 NOT NULL,
    updated_time int8 NOT NULL,
    user_id int8 NOT NULL,
    sub_start_time int8 NOT NULL DEFAULT 0,
    sub_end_time int8 NOT NULL DEFAULT 0,
    enabled int2 NOT NULL DEFAULT 1,
    order_id varchar NOT NULL,
    sub_start timestamptz NOT NULL,
    sub_end timestamptz NOT NULL,
    CONSTRAINT user_sub_new_pk PRIMARY KEY (id),
    CONSTRAINT user_sub_new_un UNIQUE (order_id)
);

CREATE INDEX user_sub_exclusion_index ON public.user_sub USING gist (user_id, tstzrange(sub_start, sub_end));
CREATE INDEX user_sub_tstzrange_user_id_product_id_excl ON public.user_sub USING gist (tstzrange(sub_start, sub_end, '[]'::text), user_id, product_id);

现在是表数据的示例:它包含同一用户的重叠时间范围:

INSERT INTO public.user_sub (app_id, product_id, iap_product_id, created_time, updated_time, user_id, sub_start_time, sub_end_time, enabled, order_id, sub_start, sub_end)
VALUES
  ('vOghoo10L9',  9,  6, 1680251663942, 1680251663942, 77, 1680501039421, 1680587439421, 1, '627599858277646336', '2023-04-03 13:50:39.421', '2023-04-04 13:50:39.421')
, ('vOghoo10L9',  9,  6, 1680263287925, 1680263287925, 77, 1680587439422, 1680673839422, 1, '627697298195189760', '2023-04-04 13:50:39.422', '2023-04-05 13:50:39.422')
, ('vOghoo10L9',  9,  6, 1680263497256, 1680263497256, 77, 1680673839423, 1680760239423, 1, '627679779371601920', '2023-04-05 13:50:39.423', '2023-04-06 13:50:39.423')
, ('vOghoo10L9',  9,  6, 1680263539890, 1680263539890, 77, 1680760239424, 1680846639424, 1, '627680143827259392', '2023-04-06 13:50:39.424', '2023-04-07 13:50:39.424')
, ('vOghoo10L9',  9,  6, 1680273609032, 1680273609032, 77, 1680846639425, 1680933039425, 1, '627601223242579968', '2023-04-07 13:50:39.425', '2023-04-08 13:50:39.425')
, ('vOghoo10L9',  9,  6, 1680275903068, 1680275903068, 77, 1680933039426, 1681019439426, 1, '627610724383956992', '2023-04-08 13:50:39.426', '2023-04-09 13:50:39.426')
, ('vOghoo10L9',  9,  6, 1680276434561, 1680276434561, 77, 1681019439427, 1681105839427, 1, '627612898614681600', '2023-04-09 13:50:39.427', '2023-04-10 13:50:39.427')
, ('vOghoo10L9',  9,  6, 1680277832740, 1680277832740, 77, 1681105839428, 1681192239428, 1, '627618880539664384', '2023-04-10 13:50:39.428', '2023-04-11 13:50:39.428')
, ('vOghoo10L9',  9,  6, 1680282545888, 1680282545888, 77, 1681192239429, 1681278639429, 1, '627411682153152512', '2023-04-11 13:50:39.429', '2023-04-12 13:50:39.429')
, ('vOghoo10L9',  9,  6, 1680327772145, 1680327772145, 77, 1681278639430, 1681365039430, 1, '627601513341616128', '2023-04-12 13:50:39.430', '2023-04-13 13:50:39.430')
, ('vOghoo10L9',  9,  5, 1680761228691, 1680761228691, 79, 1680761228687, 1680847628687, 1, '629789245600776192', '2023-04-06 14:07:08.687', '2023-04-07 14:07:08.687')
, ('vOghoo10L9',  9,  5, 1680763488691, 1680763488691, 77, 1680763488689, 1680849888689, 1, '629798689575354368', '2023-04-06 14:44:48.689', '2023-04-07 14:44:48.689')
, ('vOghoo10L9',  9,  5, 1680763634694, 1680763634694, 77, 1680849888690, 1688712288690, 1, '629799331018653696', '2023-04-07 14:44:48.690', '2023-07-07 14:44:48.690')
, ('vOghoo10L9',  9,  6, 1680850885924, 1680850885924, 80, 1680850881709, 1680937281709, 1, '630165239553671168', '2023-04-07 15:01:21.709', '2023-04-08 15:01:21.709')
, ('vOghoo10L9',  9,  6, 1681461059268, 1681461059268, 81, 1681461059263, 1681547459263, 1, '632723434021126144', '2023-04-14 16:30:59.263', '2023-04-15 16:30:59.263')
, ('vOghoo10L9',  9,  6, 1681483885008, 1681483885008, 82, 1681483885006, 1681570285006, 1, '632820205569245184', '2023-04-14 22:51:25.006', '2023-04-15 22:51:25.006')
, ('iYDFo0PQQX', 11, 14, 1682762462986, 1682762462986, 86, 1682762462971, 1682848862971, 1, '638182956129267712', '2023-04-29 18:01:02.971', '2023-04-30 18:01:02.971')
, ('iYDFo0PQQX', 11, 14, 1682764832099, 1682764832099, 86, 1682764832090, 1682851232090, 1, '638192942803423232', '2023-04-29 18:40:32.090', '2023-04-30 18:40:32.090')
, ('iYDFo0PQQX', 11, 14, 1682765939081, 1682765939081, 86, 1682765939052, 1682852339052, 1, '638197106476421120', '2023-04-29 18:58:59.052', '2023-04-30 18:58:59.052')
, ('iYDFo0PQQX', 11, 14, 1682766222551, 1682766222551, 86, 1682766193447, 1682852593447, 1, '638198676681232384', '2023-04-29 19:03:13.447', '2023-04-30 19:03:13.447')
;

我错过了什么吗?如何使这个约束起作用?即,防止同一用户订阅重叠的时间范围。

postgresql
  • 1 个回答
  • 24 Views
Martin Hope
Dolphin
Asked: 2023-03-31 14:57:32 +0800 CST

PostgreSQL 13 中是否有 unix 时间戳范围函数

  • 6

我有一个存储用户订阅信息的表,现在我想添加一个约束,使用户在同一时间段内没有多次订阅该产品。我试图这样做:

CREATE INDEX user_sub_exclusion_index ON user_sub USING GIST (
  user_id,
  tsrange(sub_start_time, sub_end_time)
);

并创建约束:

ALTER TABLE user_sub ADD CONSTRAINT user_sub_exclusion_constraint EXCLUDE USING GIST (
  user_id WITH =,
  tsrange(sub_start_time, sub_end_time) WITH &&
);

似乎 tsrange 需要时间戳数据类型。我试过范围函数但没有用。我应该怎么做才能使要点索引工作并且不更改数据类型?这是表 DDL:

CREATE TABLE public.user_sub (
    id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
    app_id varchar NOT NULL,
    product_id int4 NOT NULL,
    iap_product_id varchar NOT NULL,
    created_time int8 NOT NULL,
    updated_time int8 NOT NULL,
    user_id int8 NOT NULL,
    sub_start_time int8 NOT NULL DEFAULT 0,
    sub_end_time int8 NOT NULL DEFAULT 0,
    enabled int2 NOT NULL DEFAULT 1,
    order_id varchar NOT NULL,
    CONSTRAINT user_sub_new_pk PRIMARY KEY (id),
    CONSTRAINT user_sub_new_un UNIQUE (order_id)
);

我已经尝试将字段更改为 timestamptz 并像这样调整 sql:

CREATE INDEX user_sub_exclusion_index ON user_sub USING GIST (
  user_id,
  tstzrange(sub_start, sub_end)
);

显示错误:

SQL Error [42704]: ERROR: data type bigint has no default operator class for access method "gist"
  Hint: You must specify an operator class for the index or define a default operator class for the data type.

我应该怎么做才能添加约束?

postgresql
  • 1 个回答
  • 27 Views
Martin Hope
Dolphin
Asked: 2022-10-31 06:27:25 +0800 CST

为什么 postgresql 没有使用多列索引

  • 7

当我想使用这个 sql 来检查是否使用索引时:

explain SELECT id AS id, sub_source_id AS sub_source_id 
FROM article 
WHERE sub_source_id IN (
select sub_source_id 
    from sub_relation sr 
    where user_id =14
    and sub_status = 1

) ORDER BY created_time DESC LIMIT 50

显示这样的信息:

    Workers Planned: 2
    ->  Sort  (cost=74344.09..74697.64 rows=141422 width=24)
          Sort Key: article.created_time DESC
          ->  Hash Join  (cost=77.11..69646.15 rows=141422 width=24)
                Hash Cond: (article.sub_source_id = sr.sub_source_id)
                ->  Parallel Seq Scan on article  (cost=0.00..67792.05 rows=675805 width=24)
                ->  Hash  (cost=70.56..70.56 rows=524 width=8)
                      ->  Seq Scan on sub_relation sr  (cost=0.00..70.56 rows=524 width=8)
                            Filter: ((user_id = 14) AND (sub_status = 1))

我已经用 columnsub_source_id和建立了多列索引created_time,为什么 sql 没有使用这个article_sub_source_id_create_time_idx 索引?这是表结构:

-- public.article definition

-- Drop table

-- DROP TABLE public.article;

CREATE TABLE public.article (
    id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
    user_id int8 NOT NULL,
    title varchar(512) NOT NULL,
    author varchar(256) NOT NULL,
    guid varchar(512) NOT NULL,
    created_time int8 NOT NULL,
    updated_time int8 NOT NULL,
    link varchar(512) NULL,
    pub_time timestamptz NULL,
    sub_source_id int8 NOT NULL,
    cover_image varchar(1024) NULL,
    channel_reputation int4 NOT NULL DEFAULT 0,
    editor_pick int4 NULL DEFAULT 0,
    permanent_store int2 NOT NULL DEFAULT 0,
    CONSTRAINT article_id_seq_pk PRIMARY KEY (id),
    CONSTRAINT article_title_sub_source_id_key UNIQUE (title, sub_source_id)
);
CREATE INDEX article_sub_source_id_create_time_idx ON public.article USING btree (sub_source_id, created_time);
CREATE INDEX article_title_gin ON public.article USING gin (to_tsvector('english'::regconfig, (title)::text));
CREATE INDEX article_title_zh_gin ON public.article USING gin (to_tsvector('dolphinzhcfg'::regconfig, (title)::text));

当我删除子查询并用静态子源 ID 替换时,外部查询可以使用article_sub_source_id_create_time_idx 索引。

postgresql
  • 1 个回答
  • 43 Views
Martin Hope
Dolphin
Asked: 2022-08-06 02:43:35 +0800 CST

在 postgreSQL 13 中我应该怎么做 q jsonb 查询

  • 0

我在 PostgreSQL 13 中存储了 jsonb 数据,如下所示:

[{"code": "OFFICIAL"}, {"code": "FULLTEXT"}]

如何查询jsonb数据类型?我试过这样:

select count(*) from test where tags::jsonb ? 'code';
select count(*) from test where tags::jsonb ->> 'code' = 'OFFICIAL';
select count(*) from test where tags::jsonb @> '{"code": "OFFICIAL"}';

两者都无法工作。这是表 DDL:

CREATE TABLE public.test (
    id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
    tags jsonb NULL,
    CONSTRAINT test_pkey PRIMARY KEY (id)
);

我应该怎么做才能让它工作?也试过这样:

select * from (
    select jsonb_array_elements(tags) as tt from test
) a
where  tt -> 'code' = 'OFFICIAL'
postgresql json
  • 1 个回答
  • 20 Views
Martin Hope
Dolphin
Asked: 2022-07-30 03:03:48 +0800 CST

postgresql 13 是否支持日志轮换热重载

  • 0

我更改了服务器postgres.conf文件中的日志配置,如下所示:

log_filename = 'postgresql-%i.log'

然后我在服务器端执行这个命令:

pg_ctl reload;

新的日志文件名似乎没有生成,是否可以热重载此配置并生效?我也运行了这个命令:

SELECT pg_reload_conf();

日志仍然如下所示:

[root@k8smasterone log]# ls -alh
total 368M
drwx------  2 1000 1000 4.0K Jul 29 08:00 .
drwx------ 20 1000 1000 4.0K Jul 29 18:59 ..
-rw-------  1 1000 1000  81M Jul 28 08:00 postgresql-2022-07-27_143753.log
-rw-------  1 1000 1000  53M Jul 28 14:10 postgresql-2022-07-28_000000.log
-rw-------  1 1000 root 140M Jul 29 08:00 postgresql-2022-07-28_061040.log
-rw-------  1 1000 root  95M Jul 29 19:03 postgresql-2022-07-29_000000.log

我必须重新启动 postgresql 服务器吗?

postgresql
  • 1 个回答
  • 15 Views
Martin Hope
Dolphin
Asked: 2022-07-28 20:25:37 +0800 CST

我应该为 PostgreSQL 设置多少连接

  • 0

我应该为 PostgreSQL(版本 13.x)设置多少连接?通过 CPU 内核?内存大小?我应该选择什么值?如何估计 PostgreSQL 的连接值?我应该这样估计吗?

(single_thread_memory) = thread_stack(256KB) + binlog_cache_size(32KB) + join_buffer_size(256KB) + sort_buffer_size(256KB) + read_buffer_size(128KB) + read_rnd_buffer_size(256KB)= 1MB
postgresql
  • 1 个回答
  • 27 Views
Martin Hope
Dolphin
Asked: 2022-07-27 22:10:25 +0800 CST

我应该怎么做才能使 not in 更有效率

  • 1

我在PostgreSQL中有两个表article和article_content,表article_content中的article_id是文章表id。现在sql看起来像这样:

select * 
from article_content ac 
where article_id not in(
   select id from article a 
)
limit 10

在 article_content 中查找文章中不存在的记录。这是简单的查询:

Limit  (cost=1000.00..44996.68 rows=1 width=415)
  ->  Gather  (cost=1000.00..38955542254.16 rows=885420 width=415)
        Workers Planned: 2
        ->  Parallel Seq Scan on article_content ac  (cost=0.00..38955452712.16 rows=368925 width=415)
              Filter: (NOT (SubPlan 1))
              SubPlan 1
                ->  Materialize  (cost=0.00..101153.51 rows=1775167 width=8)
                      ->  Seq Scan on article a  (cost=0.00..85342.67 rows=1775167 width=8)

现在表 article 和 article_content 有这么多行。似乎这个 sql 无法永远完成。我应该怎么做才能删除文章中不存在的文章内容行?

postgresql
  • 1 个回答
  • 28 Views
Martin Hope
Dolphin
Asked: 2022-07-27 21:18:38 +0800 CST

是否有可能永远不会在 PostgreSQL 中运行vacuum full

  • 0

由于VACUUM FULL锁表,在我们的生产环境中对我们来说是不可接受的。

是否可以只在生产系统上运行VACUUM而从不运行?VACUUM FULL

是否VACUUM使新空间可重复使用INSERT?

postgresql locking
  • 2 个回答
  • 81 Views
Martin Hope
Dolphin
Asked: 2022-04-24 00:29:35 +0800 CST

如何在 PostgreSQL 中更新表的树路径

  • 0

我在 PostgreSQL 13 中有一个这样的菜单表:

-- Drop table

-- DROP TABLE public.menu_resource;

CREATE TABLE public.menu_resource (
    id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
    "name" varchar NOT NULL,
    res_type int4 NOT NULL,
    created_time int8 NOT NULL,
    updated_time int8 NOT NULL,
    remark varchar NULL,
    "path" varchar NOT NULL,
    parent_id int4 NOT NULL,
    component varchar NULL,
    sort int4 NOT NULL,
    name_zh varchar NOT NULL,
    tree_id_path varchar NULL,
    CONSTRAINT auth_resource_id_seq PRIMARY KEY (id)
);

parent_id菜单数据看起来像这样,带有一个用和标记的树结构id:

INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(13, 'job', 1, 1632389739938, 1632389739938, NULL, '/app/job', 1, NULL, 2, '求职管理', '5-1-13');
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(14, 'interview', 1, 1632389739938, 1632389739938, NULL, '/app/job/interview', 13, NULL, 2, '面试列表', '5-1-13-14');
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(15, 'menu', 1, 1632389739938, 1632389739938, NULL, '/privilege/menu', 2, './permission/menu', 0, '菜单列表', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(3, 'cruise', 1, 1632389739938, 1632389739938, NULL, '/app/cruise', 1, NULL, 2, 'Cruise', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(5, '系统菜单', 1, 1632389739938, 1632389739938, NULL, '/demo', 0, NULL, 0, '系统菜单', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(4, 'channel', 1, 1632389739938, 1632389739938, NULL, '/app/cruise/channel', 3, './apps/cruise/channel', 3, '频道', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(1, 'app', 1, 1632389739938, 1632389739938, NULL, '/app', 5, NULL, 1, '应用', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(2, 'privilege', 1, 1632389739938, 1632389739938, NULL, '/privilege', 5, NULL, 4, '权限管理', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(6, 'role', 1, 1632389739938, 1632389739938, NULL, '/privilege/role', 2, './permission/role', 5, '角色列表', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(7, 'article', 1, 1632389739938, 1632389739938, NULL, '/app/cruise/article', 3, './apps/cruise/article', 3, '文章', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(8, 'user', 1, 1632389739938, 1632389739938, NULL, '/privilege/user', 2, './permission/user', 5, '用户列表', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(9, 'welcome', 1, 1632389739938, 1632389739938, NULL, '/welcome', 5, './Welcome', 0, '欢迎', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(10, 'overview', 1, 1632389739938, 1632389739938, NULL, '/app/overview', 1, NULL, 2, '应用概览', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(11, 'product', 1, 1632389739938, 1632389739938, NULL, '/app/overview/product', 10, NULL, 2, '产品列表', NULL);
INSERT INTO public.menu_resource
(id, "name", res_type, created_time, updated_time, remark, "path", parent_id, component, sort, name_zh, tree_id_path)
VALUES(12, 'list', 1, 1632389739938, 1632389739938, NULL, '/app/overview/list', 10, NULL, 2, '应用列表', NULL);

现在我想生成父子id的全路径,比如tree_id_pathfor job可能是这样的:5-1-13,interview可能是这样的5-1-13-14。我应该怎么做才能让它像这样工作?

postgresql tree
  • 1 个回答
  • 131 Views
Martin Hope
Dolphin
Asked: 2022-01-22 01:41:44 +0800 CST

如何在 MySQL 5.7 中将字符串转换为日期

  • 0

现在我有一个这样的数据字符串2002年05月11日,我想将此字符串转换为unix日期时间戳,我该怎么做才能转换这个时间?我试过这样:

select STR_TO_DATE("2002年05月11日", "YYYY年MM月dd日")

不工作。然后我尝试了这个:

select STR_TO_DATE("2002年05月11日", "%Y年%M月%d日")

仍然没有工作。我应该怎么做才能将其转换为数据库可以理解的日期格式?

mysql date
  • 1 个回答
  • 49 Views
Martin Hope
Dolphin
Asked: 2022-01-21 18:36:21 +0800 CST

如何在 MySQL 5.7 中将 id 更新为行号

  • 0

我有一个表不包含 id,现在我添加了 id 列并想将 id 更新为行号,我该怎么办?这是我的表 DDL:

CREATE TABLE `spark_car_brand` (
  `alpha` varchar(255) DEFAULT NULL,
  `id` int(11) DEFAULT NULL,
  `brand_id` varchar(255) DEFAULT NULL,
  `brand_name` varchar(255) DEFAULT NULL,
  `brand_logo` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我正在使用这个 SQL 似乎不起作用:

update spark_car_brand
set id = ROW_NUMBER();
mysql
  • 1 个回答
  • 245 Views
Martin Hope
Dolphin
Asked: 2021-12-30 23:31:49 +0800 CST

如何在 MySQL 5.7 中按周分组

  • 1

当我想按月分组时,我正在使用这个 sql 在 MySQL 5.7 中进行分组:

select
    max(statistic_time) as statistic_time
    from home_overview_daily
    group by date_format(FROM_UNIXTIME(statistic_time/1000), '%Y-%m')
    order by statistic_time desc

但是当我按周分组时:

select
        max(statistic_time) as statistic_time
        from home_overview_daily
        group by date_format(FROM_UNIXTIME(statistic_time/1000), '%Y-%m-%w')
        order by statistic_time desc

结果数据如下所示:

1640789999999   2021-12-3
1640703599999   2021-12-2
1640617199999   2021-12-1
1640530799999   2021-12-0
1640444399999   2021-12-6
1640357999999   2021-12-5
1640271599999   2021-12-4
1638284399999   2021-11-2
1638197999999   2021-11-1
1638111599999   2021-11-0
1638025199999   2021-11-6
1637938799999   2021-11-5
1637852399999   2021-11-4
1637765999999   2021-11-3

为什么第 12 个月有 7 周?我在哪里做错了?我正在使用这样的格式%Y-%m-%u:

select
        max(statistic_time) as statistic_time,
        date_format(FROM_UNIXTIME(statistic_time/1000), '%Y-%m-%u')
        from home_overview_daily
        group by date_format(FROM_UNIXTIME(statistic_time/1000), '%Y-%m-%u')
        order by statistic_time desc

但结果是这样的:

1640789999999   2021-12-52
1640530799999   2021-12-51
1639925999999   2021-12-50
1639321199999   2021-12-49
1638716399999   2021-12-48
1638284399999   2021-11-48
1638111599999   2021-11-47
1637506799999   2021-11-46
1636901999999   2021-11-45
1636297199999   2021-11-44

每个月有5周?

mysql
  • 2 个回答
  • 253 Views
Martin Hope
Dolphin
Asked: 2021-12-21 01:19:20 +0800 CST

如何在mysql中使用非标准日期时间格式计算差异年份

  • 0

我的数据库(MySQL 5.7)像这样存储用户生日格式1990年1月1日,现在我想从这种格式日期时间计算用户年龄。我的 sql 看起来像这样,但为所有数据返回 null:

select period_diff(date_format(now(), '%Y'), date_format(birthday, '%Y年')) as months 
from spark_user;

表 DDL 如下所示:

CREATE TABLE `spark_user` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `birthday` varchar(64) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `index_user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8mb4;

我也试过这样:

select period_diff(date_format(now(), '%Y'), date_format(STR_TO_DATE(birthday, '%Y'), '%Y')) as months 
from spark_user;

这有效:

    select period_diff( date_format(STR_TO_DATE(birthday, '%Y年%m月%d日'), '%Y'),date_format(now(), '%Y')) as months,
birthday as birthday 
from spark_user;

但结果很奇怪:

57  1990年1月1日
57  1990年1月1日
57  1990年1月1日
57  1990年10月10日
47  1980年1月1日
-20 2001年1月1日
63  1996年10月10日
mysql
  • 3 个回答
  • 30 Views
Martin Hope
Dolphin
Asked: 2021-12-08 23:58:49 +0800 CST

如何在mysql数据库中标记用户对

  • 0

在我的表中存储两列用户 ID,我想做的是用对标记用户 ID。例如,用户 id 是这样的:

user_1_id  user_2_id

  2          3
  3          2

我们认为这两个记录是一对(只有两个用户,但顺序不同)。我添加了一个计算的新生成列user_1_id + user_2_id,如果结果相同,则为一对。但是现在我面临一个新问题,它工作不完美,因为用户 0 和 5 与记录冲突,实际上 0 和 5 是一对新用户。我应该怎么做才能完美地标记用户对?我已经思考这个问题好几个小时了。这是我项目中的真实用户信息:

在此处输入图像描述

这是我的表 DDL:

CREATE TABLE `spark_game_user_contact` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `game_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) NOT NULL,
  `target_user_id` bigint(20) NOT NULL,
  `is_exchange` int(11) NOT NULL DEFAULT '0',
  `is_whisper` int(11) NOT NULL DEFAULT '0',
  `pair_flag` bigint(20) GENERATED ALWAYS AS ((`user_id` + `target_user_id`)) STORED,
  `updated_time` bigint(20) NOT NULL,
  `created_time` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_gut` (`game_id`,`user_id`,`target_user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=102 DEFAULT CHARSET=utf8mb4;
mysql-5.7
  • 1 个回答
  • 38 Views
Martin Hope
Dolphin
Asked: 2021-12-04 22:39:10 +0800 CST

pg_stat_statements 的性能影响是什么?

  • 4

我pg_stat_statements用来在我的生产 PostgreSQL 13 中查找慢查询。两件事:

  • 我不确定此扩展对性能的影响。

  • 我能做些什么来提高它的性能吗?下面的查询需要> 1s - 有什么建议吗?


    -- add the plugin 
    create extension pg_stat_statements;
    select pg_stat_reset();
    select pg_stat_statements_reset();

接着:

-- find slow queries using the extension:

select * from pg_stat_statements order by total_exec_time desc limit 50;
postgresql pg-stat-statements
  • 1 个回答
  • 1031 Views
Martin Hope
Dolphin
Asked: 2021-12-04 01:09:08 +0800 CST

如何替换 PostgreSQL 13 中字符串的第一个开头

  • 0

我正在使用此命令替换 PostgreSQL 列中的字符串:

update sentence 
set sentence_en = replace(sentence_en,"1.  ","")

但我只想从表列的左开始替换第一个发生。我应该怎么做才能使其按预期工作?我想要的结果sentence_en是这样的:1. xxxxxx1. xxxx1. ,结果可能是这样的:xxxxxx1. xxxx1.

postgresql
  • 2 个回答
  • 256 Views
Martin Hope
Dolphin
Asked: 2021-10-07 22:52:51 +0800 CST

是否可以在不重新启动的情况下重新加载 postgresql idle_in_transaction_session_timeout 配置

  • 0

我想更改idle_in_transaction_session_timeoutPostgreSQL 13 中的参数,然后执行以下 sql:

alter system set idle_in_transaction_session_timeout='30min';

当我显示新的设置值时,它仍然是旧值,现在似乎没有生效,因为PostgreSQL在线,所以是否可以在不重新启动PostgreSQL的情况下使alter生效?我的 PostgreSQL 部署在 kubernetes 集群中。

postgresql
  • 1 个回答
  • 229 Views
Martin Hope
Dolphin
Asked: 2021-10-05 18:41:39 +0800 CST

如何正确结束 PostgreSQL 创建索引

  • 1

我正在使用此命令在 PostgreSQL 13 中创建索引:

CREATE INDEX dict_word_idx ON dict (word); 

但现在 sql 运行超过 12 小时,我不确定 sql 是否仍在工作。我使用这个命令来显示当前正在运行的 sql:

SELECT 
  procpid, 
  start, 
  now() - start AS lap, 
  current_query 
FROM 
  (SELECT 
    backendid, 
    pg_stat_get_backend_pid(S.backendid) AS procpid, 
    pg_stat_get_backend_activity_start(S.backendid) AS start, 
    pg_stat_get_backend_activity(S.backendid) AS current_query 
  FROM 
    (SELECT pg_stat_get_backend_idset() AS backendid) AS S 
  ) AS S 
WHERE 
  current_query <> '<IDLE>' 
ORDER BY 
  lap DESC;

显示创建索引的结果,如下所示:

1303    2021-10-04 09:24:27 13:12:49.940239 CREATE INDEX dict_word_idx ON dict (word);

但pg_stat_progress_create_index什么也没显示:

select *
from pg_stat_progress_create_index;

然后我尝试使用这个 sql 来终止创建索引任务:

SELECT pg_cancel_backend(1303);

好像不行,create index命令还站在那里。我应该怎么做才能弄清楚现在发生了什么。SQL 仍然忙于运行,我应该等待吗?不应该像这样杀死 craeted index 命令吗?我应该怎么做才能正确处理这种情况,并且以后不要使数据库失败。

postgresql
  • 1 个回答
  • 616 Views
Martin Hope
Dolphin
Asked: 2021-09-30 18:55:38 +0800 CST

如何将序列号添加到表中?

  • 0

我有一个表有超过 1000000 行是从 CSV 文件导入的。该表没有主键,我想在表中添加一个具有递增 bigint 的主键。这是我的表 DDL:

CREATE TABLE public.ecdict (
    word varchar(256) NULL,
    phonetic varchar(512) NULL,
    definition varchar(80000) NULL,
    "translation" varchar(80000) NULL,
    pos varchar(100) NULL,
    collins int4 NULL,
    oxford int4 NULL,
    tag varchar(128) NULL,
    bnc int4 NULL,
    frq int4 NULL,
    exchange varchar(128) NULL,
    detail varchar(2000) NULL,
    audio varchar(8) NULL,
    id int8 NULL
);

该id列是新添加的,所有数据都为 NULL,我尝试使用此命令:

update ecdict c
set id = c2.seqnum
from (
   select c2.*, row_number() over () as seqnum
   from ecdict c2
) c2
where c2.pkid = c.pkid;

但是我找不到用于连接的表的唯一列,我应该怎么做才能添加id具有递增bigint值的列?

postgresql primary-key
  • 1 个回答
  • 787 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