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

daniel9x's questions

Martin Hope
daniel9x
Asked: 2021-03-23 13:34:43 +0800 CST

Postgres 11 JSONB - 如何跨所有元素获取字段的所有不同值

  • 1

我有一张桌子叫Shipments:

CREATE TABLE shipments
(
      shipmentId numeric,
      metadata jsonb
); 

在metaData列内可以存在一个名为 JSON 对象的数组,stops其名称可能如下所示:

{
  "stops": [
    {
      "stopId": 1,
      "stopType": "Origin"
    },
    {
      "stopId": 2,
      "stopType": "Destionation"
    },
    {
      "stopId": 3,
      "stopType": "Transit"
    }
  ]
}

我正在尝试查询整个表并获取所有不同的stopType值。我可以通过这个简单的查询获得数组stopType中第一个索引的所有不同值:stops

select distinct metadata->'stops'->0->>'stopType' from shipments

这几乎给了我我需要的东西:

╔══════════╗
║ stopType ║
╠══════════╣
║ Origin   ║
╚══════════╝

但我想要的是:

╔══════════════╗
║ stopType     ║
╠══════════════╣
║ Origin       ║
║ Destination  ║
║ Transit      ║
╚══════════════╝

在此先感谢您的帮助!

postgresql json
  • 2 个回答
  • 1610 Views
Martin Hope
daniel9x
Asked: 2020-08-18 12:24:42 +0800 CST

Postgres 11 - 具有 NULL 值的日期列上的范围分区

  • 1

我们有一个表结构,在同一个表中包含父记录和子记录。它们通过引用父项的列 ( parentId ) 进行引用(在顶级父记录中为 null)。此外,我们有一个请求的日期列,我们通过它进行所有查询,但唯一具有此值的记录是父记录。

由于我们所有的查询最初都在过滤父结果并使用此日期,因此我们希望在此日期范围内对表进行分区,但要理解这是不可能的,因为子记录的记录中有空值。

除了在数据库中的每一个子记录上创建一个“虚拟”值(一些永远不会在我们的过滤器中捕获的任意日期)之外,我们还有什么其他选择?是否不可能以某种方式为我们从未查询过的所有空值创建一个单独的分区?

postgresql partitioning
  • 1 个回答
  • 1835 Views
Martin Hope
daniel9x
Asked: 2020-05-30 10:33:13 +0800 CST

Postgres 10.6:为什么没有使用特定分区的索引?

  • 0

我真的很困惑是什么导致了这个问题,并希望得到一些指导。

首先,这是在我们的 STAGE 环境中,我们根据createdTime每个月对该表进行分区。我已经三次检查了每个分区和索引,每个月的所有内容都是相同的。此外,我们的 PRODUCTION 数据库表我已经对以下查询运行了 EXPLAIN ANALYZE:

explain analyze select * from ubdrecord
where jsonb_extract_path_text("metadata",'deliveryNoteInfo','deliveryNoteNumber') =  '3535359152';

这导致以下结果:

Append  (cost=0.43..23499.50 rows=21693 width=3503) (actual time=10.682..11.823 rows=7 loops=1)
  ->  Index Scan using p_01_2019_deliverynotenumberindex on ubdrecord_partition_01_2019  (cost=0.43..53.50 rows=47 width=765) (actual time=0.743..0.744 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_02_2019_deliverynotenumberindex on ubdrecord_partition_02_2019  (cost=0.43..34.23 rows=30 width=808) (actual time=0.892..0.893 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_03_2019_deliverynotenumberindex on ubdrecord_partition_03_2019  (cost=0.43..31.81 rows=28 width=800) (actual time=0.682..0.682 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_04_2019_deliverynotenumberindex on ubdrecord_partition_04_2019  (cost=0.43..37.17 rows=32 width=822) (actual time=0.904..0.904 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_05_2019_deliverynotenumberindex on ubdrecord_partition_05_2019  (cost=0.43..37.12 rows=33 width=827) (actual time=0.821..0.821 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_06_2019_deliverynotenumberindex on ubdrecord_partition_06_2019  (cost=0.43..50.06 rows=44 width=821) (actual time=0.720..0.720 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_07_2019_deliverynotenumberindex on ubdrecord_partition_07_2019  (cost=0.43..45.29 rows=40 width=816) (actual time=0.773..0.774 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_08_2019_deliverynotenumberindex on ubdrecord_partition_08_2019  (cost=0.43..37.82 rows=33 width=799) (actual time=0.950..0.950 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_09_2019_deliverynotenumberindex on ubdrecord_partition_09_2019  (cost=0.43..42.18 rows=37 width=843) (actual time=1.111..1.111 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_10_2019_deliverynotenumberindex on ubdrecord_partition_10_2019  (cost=0.43..44.22 rows=39 width=815) (actual time=0.765..0.765 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_11_2019_deliverynotenumberindex on ubdrecord_partition_11_2019  (cost=0.43..55.72 rows=49 width=738) (actual time=1.141..1.141 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_12_2019_deliverynotenumberindex on ubdrecord_partition_12_2019  (cost=0.43..37.24 rows=32 width=833) (actual time=0.720..0.720 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_01_2020_deliverynotenumberindex on ubdrecord_partition_01_2020  (cost=0.43..41.19 rows=36 width=814) (actual time=0.027..0.027 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_02_2020_deliverynotenumberindex on ubdrecord_partition_02_2020  (cost=0.43..39.26 rows=34 width=848) (actual time=0.027..0.027 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_03_2020_deliverynotenumberindex on ubdrecord_partition_03_2020  (cost=0.43..45.85 rows=40 width=856) (actual time=0.026..0.027 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_04_2020_deliverynotenumberindex on ubdrecord_partition_04_2020  (cost=0.43..3.77 rows=2 width=811) (actual time=0.025..0.026 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Bitmap Heap Scan on ubdrecord_partition_05_2020  (cost=241.19..22846.59 rows=21130 width=3575) (actual time=0.349..1.451 rows=7 loops=1)
        Recheck Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
        Heap Blocks: exact=7
        ->  Bitmap Index Scan on p_05_2020_deliverynotenumberindex  (cost=0.00..235.91 rows=21130 width=0) (actual time=0.338..0.338 rows=7 loops=1)
              Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_06_2020_deliverynotenumberindex on ubdrecord_partition_06_2020  (cost=0.14..2.36 rows=1 width=3575) (actual time=0.006..0.006 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_07_2020_deliverynotenumberindex on ubdrecord_partition_07_2020  (cost=0.14..2.36 rows=1 width=3575) (actual time=0.005..0.005 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_08_2020_deliverynotenumberindex on ubdrecord_partition_08_2020  (cost=0.14..2.36 rows=1 width=3575) (actual time=0.004..0.005 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_09_2020_deliverynotenumberindex on ubdrecord_partition_09_2020  (cost=0.14..2.36 rows=1 width=3575) (actual time=0.004..0.004 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_10_2020_deliverynotenumberindex on ubdrecord_partition_10_2020  (cost=0.14..2.36 rows=1 width=3575) (actual time=0.004..0.004 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_11_2020_deliverynotenumberindex on ubdrecord_partition_11_2020  (cost=0.14..2.36 rows=1 width=3575) (actual time=0.006..0.006 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
  ->  Index Scan using p_12_2020_deliverynotenumberindex on ubdrecord_partition_12_2020  (cost=0.14..2.36 rows=1 width=3575) (actual time=0.004..0.005 rows=0 loops=1)
        Index Cond: (jsonb_extract_path_text(metadata, VARIADIC '{deliveryNoteInfo,deliveryNoteNumber}'::text[]) = '3535359152'::text)
Planning time: 192.498 ms
Execution time: 11.997 ms

如您所见,当前月份没有像其他月份一样被扫描。这个月有两点与众不同:

1)这是我们所在的当前月份(因此对该表有很多写入)和

2) 这是实际包含一些结果的月份。

我没有探讨这些特殊区别的原因是因为我们的生产环境在本月完美地扫描索引。我应该指出每个环境中的数据和读/写模式也非常相似。我们已经验证了两种环境中的分区和索引在其特定配置中是相同的,但很好奇其他更广泛的数据库参数设置可能是导致这种差异的原因。任何见解将不胜感激,我可以提供您需要的任何其他信息,只需询问即可。

更新:这对我们来说是个问题的原因是我们正在运行的实际查询使用其他 where 子句参数(它们也被索引,尽管对于这种特殊情况来说效率低得多)。我们不可能删除其他索引,同样,在生产环境中,使用了正确的索引并且查询很快返回。但是在 STG 中,未能使用正确的索引会导致查询在几分钟内无法返回。我想后续问题是我们如何强制查询计划使用特定索引(如果很难确定为什么不使用它)?我希望我已经成功解释了。同样,两个环境之间的 DATA 几乎相同。

postgresql index-tuning
  • 2 个回答
  • 499 Views
Martin Hope
daniel9x
Asked: 2019-09-12 06:06:56 +0800 CST

如何让 Postgres 多列多表搜索更高效

  • 2

我有一个Shipment表,其中包含一些关于货物的基本数据和一个ShipmentItem表,其中包含有关该货物的附加属性,表的主键foreignKey上有一个。toShipment表是关系。ShipmentShipmentItemOneToMany

我们需要包含一个文本搜索选项,该选项采用给定的输入文本字符串并搜索 (make)Shipment的 2 个列以及三个特定types的ShipmentItemname 列。这是我当前的查询:

select *
from Shipment shipment
where shipment.deliveryRequestedDate >= '2019-06-09T00:00:00Z'
  and shipment.deliveryRequestedDate <= '2019-12-06T23:59:59Z'
  and (
        shipment.identifierkeyvalues = '12345'
        or shipment.carrierReferenceNumber = '12345'
        or shipment.uuid in (
            select shipmentItem.resultId
            from ShipmentItem shipmentItem
            where (
                shipmentItem.type in (
                                      'poNumber', 'deliveryNoteNumber', 'salesOrderNumber'
                )
            )
            and shipmentItem.name = '12345'
            and shipmentItem.deliveryRequestedDate >= '2019-06-09T00:00:00Z'
            and shipmentItem.deliveryRequestedDate <= '2019-12-06T23:59:59Z'
       )
    )
limit 25

我发现的问题是将子查询作为or条件之一的组合导致了主要的性能问题(即使子查询本身通过利用type_name_deliveryRequestedDate该表上的索引快速返回。虽然我们在主表上有多个索引(identifierKeyValues, carrierReferenceNumber, , 甚至是查询的所有三个 Shipment 列的索引,它只会使用deliveryRequestedDate效率极低的索引,因为这个查询的范围太大了。

将其转换为 JOIN 似乎会导致相同的行为。我只是不太确定目前最好的方法是什么。我们在此查询上方有一个 Java Persistence API 层,因此希望尽可能避免对数据模型进行任何重大更改,但不确定最佳方法是什么。任何想法将不胜感激!

解释计划:

Limit  (cost=110.61..209.98 rows=25 width=1370) (actual time=119503.030..124034.809 rows=1 loops=1)
      ->  Index Scan using shipment_deliveryrequesteddate_idx on shipment shipment  (cost=110.61..890840.18 rows=224084 width=1370) (actual time=119503.027..124034.805 rows=1 loops=1)
            Index Cond: ((deliveryrequesteddate >= '2019-06-09 00:00:00'::timestamp without time zone) AND (deliveryrequesteddate <= '2019-12-06 23:59:59'::timestamp without time zone))
            Filter: ((identifierkeyvalues = '12345'::text) OR (carrierreferencenumber = '12345'::text) OR (hashed SubPlan 1))
            Rows Removed by Filter: 496784
            SubPlan 1
              ->  Index Scan using "type_name_deliveryRequestedDate" on resultitem shipmentitem  (cost=0.56..110.11 rows=24 width=16) (actual time=10.706..16.416 rows=1 loops=1)
                    Index Cond: ((type = ANY ('{poNumber,deliveryNoteNumber,salesOrderNumber}'::text[])) AND (name = '12345'::text) AND (deliveryrequesteddate >= '2019-06-09 00:00:00'::timestamp without time zone) AND (deliveryrequesteddate <= '2019-12-06 23:59:59'::timestamp without time zone))
    Planning time: 3.175 ms
    Execution time: 124035.006 ms

解释计划删除子查询——为什么它使用完全不同的索引?

Limit  (cost=9.51..273.71 rows=6 width=1370) (actual time=0.052..0.053 rows=0 loops=1)
  ->  Bitmap Heap Scan on shipment shipment  (cost=9.51..273.71 rows=6 width=1370) (actual time=0.051..0.051 rows=0 loops=1)
        Recheck Cond: (((identifierkeyvalues = '12345'::text) AND (deliveryrequesteddate >= '2019-06-09 00:00:00'::timestamp without time zone) AND (deliveryrequesteddate <= '2019-12-06 23:59:59'::timestamp without time zone)) OR (carrierreferencenumber = '12345'::text))
        Filter: ((deliveryrequesteddate >= '2019-06-09 00:00:00'::timestamp without time zone) AND (deliveryrequesteddate <= '2019-12-06 23:59:59'::timestamp without time zone))
        Rows Removed by Filter: 2
        Heap Blocks: exact=2
        ->  BitmapOr  (cost=9.51..9.51 rows=66 width=0) (actual time=0.041..0.041 rows=0 loops=1)
              ->  Bitmap Index Scan on shipment_identifierkeyvalues_idx  (cost=0.00..4.61 rows=4 width=0) (actual time=0.023..0.024 rows=0 loops=1)
                    Index Cond: ((identifierkeyvalues = '12345'::text) AND (deliveryrequesteddate >= '2019-06-09 00:00:00'::timestamp without time zone) AND (deliveryrequesteddate <= '2019-12-06 23:59:59'::timestamp without time zone))
              ->  Bitmap Index Scan on shipment_carrierreferencenumber_idx  (cost=0.00..4.90 rows=62 width=0) (actual time=0.016..0.016 rows=2 loops=1)
                    Index Cond: (carrierreferencenumber = '12345'::text)
Planning time: 1.668 ms
Execution time: 0.116 ms
postgresql performance
  • 1 个回答
  • 1487 Views
Martin Hope
daniel9x
Asked: 2019-06-25 09:26:17 +0800 CST

将 JSON 数组展平为逗号分隔列表

  • 2

我正在尝试构建一个SELECT语句,该语句将采用JSONB以下格式的列:

{
  "Cities": [
    {
      "Name": "Atlanta"
    },
    {
      "Name": "London"
    },
    {
      "Name": "New York"
    }
  ]
}

列结果集的输出需要采用以下格式:

Atlanta, London, New York

更新

@a_horse_with_no_name 下面的答案是正确的,但我的要求实际上比最初发布的要复杂一些。我实际上需要将此选择放入更大的(连接)查询中,如下所示:

select eo.citydata.cities.names <-- Flattened Comma delimited JSON Array
from orderline o 
join eventorders eo on eo.orderlineid = o.id
join events e on e.id = eo.eventid
where e.id = '123'

显然,需要修改提供的答案才能使其正常工作,我正在努力弄清楚如何去做。

postgresql json
  • 1 个回答
  • 4549 Views
Martin Hope
daniel9x
Asked: 2018-04-18 07:35:46 +0800 CST

如何在两个位置之间提取子字符串

  • 2

我在表格(通知)中有一个文本列( emailbody ),它记录了针对特定电子邮件发出的 HTML。在该 HTML 文件的某处,有一行文本包含以下行:

Delayed ${shipmentId} for Carrier

其中${shipmentId}表示为该特定记录生成的特定 ID。示例:12345

基本上我想在表中的列中查询所有 shippingId 值,不幸的是,这将涉及获取 和 之间的字符串Delayed位置 for Carrier。我知道这可能会涉及使用 Postgres 的正则表达式函数之一,但我不太确定该怎么做,或者是否有一些更简单的东西。

postgresql regular-expression
  • 1 个回答
  • 8039 Views
Martin Hope
daniel9x
Asked: 2018-04-07 11:02:35 +0800 CST

Postgres - 如何使用连接表的 Where 子句获取多计数查询

  • 1

我有以下查询,可获取连接的多个表的计数:

select
    count(distinct e.planningitemdata->>'receiverDuns') as supplierOrCustomer,
    count(distinct e.planningitemdata->>'shipTo') as shipTo,
    count(distinct e.planningitemdata->>'productId') as product,
    count(distinct eo.orderlineid) as orderline,
    count(distinct e.planningitemid) as planningitem 
from
    eventplanningitem e 
join
    eventorderline eo 
        on e.eventid = eo.eventid
join
    orderline o 
        on eo.orderlineid = o.id
        and o.deliveryrequesteddate between '2018-03-06' AND '2018-05-06'
where
    e.eventId = '9f6d3d50-05ca-4441-a4e4-24de2b52de5b'

我想根据日期过滤器过滤“orderlineid”计数,但我的问题是,当我尝试将它应用于如上所示的连接时,它会显示所有其他计数的所有结果为 0。如果我尝试在末尾添加 where 子句,也会发生同样的事情。如果我将 orderline 表设为左外连接,我会得到与日期条件不匹配的 orderlines 的计数。

结果:

0;0;0;0;0

类似的版本会产生相同的结果:

select
        count(distinct e.planningitemdata->>'receiverDuns') as supplierOrCustomer,
        count(distinct e.planningitemdata->>'shipTo') as shipTo,
        count(distinct e.planningitemdata->>'productId') as product,
        count(distinct eo.orderlineid) as orderline,
        count(distinct e.planningitemid) as planningitem 
    from
        eventplanningitem e 
    join
        eventorderline eo 
            on e.eventid = eo.eventid
    join
        orderline o 
            on eo.orderlineid = o.id
            and o.deliveryrequesteddate between '2018-03-06' AND '2018-05-06'
    where
        e.eventId = '9f6d3d50-05ca-4441-a4e4-24de2b52de5b' 
        and (o.deliveryrequesteddate >='2018-03-06' and o.deliveryrequesteddate <= '2018-05-06')

如果我让 orderline 加入 a ,我会得到计数left outer join,但它包括实际超出日期标准的 orderLine 记录,因此它也不正确。

我想避免将其分解为多个查询,但我想在走那条路线之前检查我是否遗漏了一些东西。

postgresql join
  • 2 个回答
  • 1986 Views
Martin Hope
daniel9x
Asked: 2017-08-26 06:33:37 +0800 CST

Postgres Order By 但只有特定类型的最新

  • 0

假设我有一个包含三列的 PET 表:名称、类型和日期。我想按日期对三列进行排序,但只显示特定类型的最新一列(本例中为“狗”),其余的全部显示。例如,我的原始数据列表:

Name        Type        Date
Nemo        Fish        June 1, 2016
Fido        Dog         January 1, 2016
Felix       Dog         February 1, 2016
Whiskers    Cat         April 1, 2016
Marlin      Fish        August 1, 2016
Shifu       Cat         March 3, 2016

应用查询时看起来像这样。

Felix       Dog         February 1, 2016
Shifu       Cat         March 3, 2016
Whiskers    Cat         April 1, 2016
Nemo        Fish        June 1, 2016
Marlin      Fish        August 1, 2016

我敢肯定有多种方法可以做到这一点,但从效率的角度来看,只有几种方法是最佳的。

postgresql performance
  • 2 个回答
  • 1069 Views
Martin Hope
daniel9x
Asked: 2016-03-17 11:30:22 +0800 CST

使用左外连接和 Where 条件创建视图

  • 1

假设我有一个用户表和一个用户记录表,其中列出了与该用户关联的数据源。

我想创建一个(Sql Server)视图,列出用户的所有内容,以及一个可选列,仅在满足特定条件时列出用户记录表中的特定行。

这是我到目前为止所拥有的:

create view
select user.*,user_record.external_identifier
from [User] user
left outer join [User Record] user_record on user_record.USER_ID = user.ID

添加 "where user_record.source = 'Peoplesoft' 将过滤掉没有关联 Peoplesoft 用户记录的用户行。

如果我在 User 表中有 100 个用户,其中 95 个用户有 Peoplesoft 用户记录,我希望返回 100 个记录,最后一列中的 5 个没有关联的记录为“null”。

我知道这相对简单,但我很挣扎。提前致谢。

sql-server join
  • 1 个回答
  • 7993 Views
Martin Hope
daniel9x
Asked: 2016-03-11 06:59:55 +0800 CST

按开始日期排序 Desc 如果结束日期为空,如果不是 按结束日期排序

  • 0

我有一个带有活动列表的 SQL Server (RDBMS) 表。每个活动都有一个开始和结束日期。

要求是对它们进行排序,以便首先显示具有 NULL 结束日期的那些(这表明“当前” - 开始日期降序),但是具有 start 和 end 值的那些将在“当前”行并按 end_date 降序排序。

最后,所有开始日期和结束日期为 null 的值都必须位于结果集的末尾。

具有结束日期但开始日期为空的值应在 END date desc 片段中排序的第二部分中进行排序。

sql-server sorting
  • 2 个回答
  • 3113 Views
Martin Hope
daniel9x
Asked: 2016-02-03 12:49:03 +0800 CST

Select 子句中的函数是否评估 where 子句之前传入的列?

  • 1

我有一个简单的 select 语句,它使用一个(不是那么)简单的函数。为了这个问题的目的,我将尽力简化和展示我的能力。

            select submission_identifier,
                   submitted_by_person_id,
                   submission_date,
                   form_version_date_time,
                   form_id,
                   func_get_open_days_2(
                     submitted_by_person_id, 
                     submission_date, 
                     form_version_date_time, 
                     form_id)` open_days
            from form_submission
            where submission_identifier = 2070;

submission_identifier 是 form_submission 表的主键,因此自然地,此查询返回一个结果。

如您所见,该函数有 4 个参数。我假设这些参数中的值是计算 where 子句后的查询结果。这是该函数的一个片段。

create or replace function        func_get_Open_Days_2
   ( SUBMITTED_BY_PERSON_ID in number,
  SUBMISSION_DATE in date,
  FORM_VERSION_DATE_TIME in timestamp,
  FORM_ID in number
   )
RETURN number

IS
   n1 number:=0;
   n2 number:=0;

   cursor records is
      SELECT 
        SUBMITTED_BY_PERSON_ID,
        SUBMISSION_DATE,
        FORM_ID,
        FORM_VERSION_DATE_TIME,
        AUDIT_DATE_TIME,
        STATUS_DATE,
        SUBMISSION_STATUS_CODE,
        CREATE_USER_ID
      from form_state_audit_trail t
      where
        t.submitted_by_person_id = SUBMITTED_BY_PERSON_ID and
        t.form_id = FORM_ID and
        t.submission_date = SUBMISSION_DATE and
        t.form_version_date_time = FORM_VERSION_DATE_TIME and 
        t.submission_status_code NOT IN (3469, 3462);
        order by t.status_date asc;

   rec records%ROWTYPE;

BEGIN

dbms_output.put_line('submitted by person id ='||submitted_by_person_id);
dbms_output.put_line('form id ='||form_id);
dbms_output.put_line('submission date ='||submission_date);
dbms_output.put_line('form_version_date_time ='||form_version_date_time);

  -- go through all the durations in audit_trail table
  FOR rec in records
  LOOP

我已经尝试对返回值进行硬编码并将它们放入此游标选择查询中。我收到 1 个结果。

这向我建议“记录”数组或列表应该只有 1 个结果。但是,我现在得到了很多结果。正在返回“记录”,这表明没有使用我期望的 4 个值/结果评估 where 子句我不确定我做错了什么。“dbms_output.put_line”是 BEGIN,但在 LOOP 确认预期值到达函数之前。

oracle functions
  • 1 个回答
  • 118 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