2

我在这里阅读了很多答案,但这是我的第一个问题。我是新手,所以对问题风格/格式的建议表示赞赏。

PostgreSQL 9.1 我有一张叫做事件的表。主键=序列号

   ==============================================
   |serialid|             time|sender|dependency|

   |       1| 2012-11-22 14:40|  John|          |
   |       2| 2012-11-22 14:41|   Sue|         1|
   |       3| 2012-11-22 14:42|  John|         1|
   |       4| 2012-11-22 14:43|   Sue|          |
   |       5| 2012-11-22 14:44|  John|         4|
   |       6| 2012-11-22 14:44|  John|         4|

关于表:第一行(serialid==1)有2个依赖事件:2nd,3rd

(1)FIRST GOAL 我想确保如果我做一个 WHERE 语句 ex: WHERE sender="John" 每个相关事件也存在于结果表中

结果不好:

   |       1| 2012-11-22 14:40|  John|          |
   |       3| 2012-11-22 14:42|  John|         1|
   |       5| 2012-11-22 14:44|  John|         4|
   |       6| 2012-11-22 14:44|  John|         4|

我还需要 Sue 的事件(serialid==2),因为它连接到约翰的“主要事件”

好结果:

   |       1| 2012-11-22 14:40|  John|          |
   |       2| 2012-11-22 14:41|   Sue|         1|
   |       3| 2012-11-22 14:42|  John|         1|
   |       5| 2012-11-22 14:44|  John|         4|
   |       6| 2012-11-22 14:44|  John|         4|

(2)第二个目标:让我们看一下“好结果”表:

   |       1| 2012-11-22 14:40|  John|          |
   |       2| 2012-11-22 14:41|   Sue|         1|
   |       3| 2012-11-22 14:42|  John|         1|
   |       5| 2012-11-22 14:44|  John|         4|
   |       6| 2012-11-22 14:44|  John|         4|

有些事件没有“主要事件”。serialid: 5,6 的事件是 event serialid: 4 的依赖项,但它不在结果中。好的结果是:

   |       1| 2012-11-22 14:40|  John|          |
   |       2| 2012-11-22 14:41|   Sue|         1|
   |       3| 2012-11-22 14:42|  John|         1|
   |       4| 2012-11-22 14:43|   Sue|          |
   |       5| 2012-11-22 14:44|  John|         4|
   |       6| 2012-11-22 14:44|  John|         4|

(摘要)所以我需要一个查询,我可以在其中指定一个自定义 where 语句,查询的其余部分收集 where 语句结果的所有依赖项。

像这样的东西:

SELECT * FROM events WHERE “我的条件”

联盟??魔术依赖收集器查询:-)

(注意)依赖只能是一级深度。

提前谢谢你,戴夫

更新

感谢 Igor 我在我的真实数据库中得到了我的真实查询:

WITH RECURSIVE dep_event AS 
(
  SELECT ev.serialid,ev.time_processed,ev.time_created,ev.sender_console,ev.sender_manager,ev.sender_map,ev.sender_device,ev.event_type,ev.event_command,ev.event_severity,ev.event_actionlist,ev.event_source,ev.event_info,ev.event_message,ev.dependency_main,ev.dependency_comment
  FROM events ev
  WHERE ev.event_severity='warning'
  UNION
  SELECT ev.serialid,ev.time_processed,ev.time_created,ev.sender_console,ev.sender_manager,ev.sender_map,ev.sender_device,ev.event_type,ev.event_command,ev.event_severity,ev.event_actionlist,ev.event_source,ev.event_info,ev.event_message,ev.dependency_main,ev.dependency_comment
  FROM  events ev
  JOIN dep_event dev ON ev.serialid = dev.dependency_main
             OR dev.serialid = ev.dependency_main
)
SELECT *
FROM dep_event

解释分析

"CTE Scan on dep_event  (cost=203955680.32..204419627.58 rows=23197363 width=1034) (actual time=11.204..4602.977 rows=234159 loops=1)"
"  CTE dep_event"
"    ->  Recursive Union  (cost=0.00..203955680.32 rows=23197363 width=176) (actual time=11.200..4468.402 rows=234159 loops=1)"
"          ->  Seq Scan on events ev  (cost=0.00..47382.98 rows=227693 width=176) (actual time=11.181..2145.798 rows=225365 loops=1)"
"                Filter: ((event_severity)::text = 'warning'::text)"
"          ->  Nested Loop  (cost=4.89..20344435.01 rows=2296967 width=176) (actual time=1.593..610.347 rows=5863 loops=3)"
"                ->  WorkTable Scan on dep_event dev  (cost=0.00..45538.60 rows=2276930 width=16) (actual time=0.050..33.360 rows=78053 loops=3)"
"                ->  Bitmap Heap Scan on events ev  (cost=4.89..8.90 rows=1 width=176) (actual time=0.005..0.005 rows=0 loops=234159)"
"                      Recheck Cond: ((serialid = dev.dependency_main) OR (dev.serialid = dependency_main))"
"                      ->  BitmapOr  (cost=4.89..4.89 rows=1 width=0) (actual time=0.003..0.003 rows=0 loops=234159)"
"                            ->  Bitmap Index Scan on serialid  (cost=0.00..2.44 rows=1 width=0) (actual time=0.000..0.000 rows=0 loops=234159)"
"                                  Index Cond: (serialid = dev.dependency_main)"
"                            ->  Bitmap Index Scan on dep_main  (cost=0.00..2.45 rows=1 width=0) (actual time=0.002..0.002 rows=0 loops=234159)"
"                                  Index Cond: (dev.serialid = dependency_main)"
"Total runtime: 4621.138 ms"
4

1 回答 1

0

一般来说-这是递归CTE(WITH RECURSIVE语句)的明显案例。

它看起来像这样:

WITH RECURSIVE dep_event AS (
  SELECT ev.serialid, ev.time, ev.sender, ev.dependency
  FROM events ev
  WHERE ev.sender = 'John' -- start condition here
  UNION
  SELECT ev.serialid, ev.time, ev.sender, ev.dependency
  FROM  events ev
  JOIN dep_event dev ON ev.serialid = dev.dependency -- connect conditions here
                     OR dev.serialid = ev.dependency
)
SELECT *
FROM dep_event;

此处的其他信息http://www.postgresql.org/docs/current/static/queries-with.html

RECURSIVE查询将处理任何级别的依赖关系。

UPD:修复了查询中的一些错误并创建了 SQLFiddle 示例http://sqlfiddle.com/#!12/4e915/4

于 2012-11-22T10:30:06.037 回答