簡體   English   中英

如何在sql Postgresql中獲取字符串變量

[英]How to get a string variable in sql Postgresql

有人可以幫我設置一個 SQL 命令,該命令僅檢索字符串中第一次出現“成本”的值,如下例所示。

請參閱下面從我存儲 sql 查詢執行計划的表返回的兩個結果。 我使用 Postgres。 cost 這個詞總是出現在開頭,但它並不總是出現在相同的位置,所以你不能簡單地做 substring (...)。 我認為您應該每次都找到初始和最終位置,以便能夠僅提取成本值。 如果有人可以幫助我,下面示例的預期結果是:

select (.... )
-----------------
cost=399301  (only this)

----------------------示例字符串源 ------------------

Sort  (cost=399301.55..399301.57 rows=6 width=36)
  Sort Key: l_returnflag, l_linestatus
  ->  HashAggregate  (cost=399301.21..399301.48 rows=6 width=36)
        ->  Seq Scan on h_lineitem  (cost=0.00..250095.98 rows=5968209 width=36)
              Filter: (l_shipdate <= (to_date('1998/12/01'::text, 'YYYY/MM/DD'::text) - '10 days'::interval day))

-------------------- 第二個樣品 ---------------------------- ----

Aggregate  (cost=7922058.70..7922058.71 rows=1 width=16)"
  ->  Hash Join  (cost=1899763.92..7922058.69 rows=1 width=16)"
        Hash Cond: (h_lineitem.l_partkey = h_part.p_partkey)"
        Join Filter: (((h_part.p_brand = 'Brand#13'::bpchar) AND (h_part.p_container = ANY ('{"SM CASE","SM BOX","SM PACK","SM PKG"}'::bpchar[])) AND (h_lineitem.l_quantity >= 4::double precision) AND (h_lineitem.l_quantity <= 14::double precision) AND (h_ (...)"
        ->  Seq Scan on h_lineitem  (cost=0.00..235156.84 rows=211094 width=32)"
              Filter: ((l_shipmode = ANY ('{AIR,"AIR REG"}'::bpchar[])) AND (l_shipinstruct = 'DELIVER IN PERSON'::bpchar))"
        ->  Hash  (cost=1183158.46..1183158.46 rows=35278997 width=33)
->  Seq Scan on h_part  (cost=0.00..1183158.46 rows=35278997 width=33) Filter: (p_size >= 1)"

此致

假設計划存儲在一個名為表plan_table在名為execution_plan您可以使用以下命令:

select replace(substring(execution_plan from '\(cost=[0-9]+'), '(cost=', '')
from plan_table;

substring(...)返回(cost=的第一次出現,然后replace用於刪除該前綴。

我成功了,解決方案是,目標是獲得值 27726324.41,最終開發的 Expression Regular 是:

select substring('Aggregate (cost=27726324.40..27726324.41 Rows' from '[.]Y*([0-9]+.?[0-9]{2})') 

結果 27726324.41

感謝 a_horse_with_no_name 和其他幫助我的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM