1

这是一个奇怪的问题。让我列出我的表格结构......

我的“服务器”表为我提供了服务器的状态信息(我每分钟都会收到一些此类消息)。我的“客户端”表使用客户端信息更新(显然,客户端连接到服务器)。

集群中的一台服务器始终是“主服务器”,可以这么说(其他服务器是辅助服务器)。每个客户端都向我的每个服务器提供延迟信息。

我需要查看客户端延迟大于 60 秒的行,但我还需要知道每个服务器的状态。

这是示例表(服务器,然后是客户端):

  server_name |   server_role   |      sstat_time
--------------+-----------------+----------------------
  server1     |  PRIMARY        |  2013-05-15 01:01:00
  server2     |  SECONDARY      |  2013-05-15 01:02:00
  server3     |  SECONDARY      |  2013-05-15 01:02:00
  server1     |  PRIMARY        |  2013-05-15 01:05:00
  server2     |  SECONDARY      |  2013-05-15 01:06:00
  server3     |  PRIMARY        |  2013-05-15 01:10:00
  server1     |  SECONDARY      |  2013-05-15 01:11:00
  server1     |  PRIMARY        |  2013-05-15 01:22:00
  server3     |  SECONDARY      |  2013-05-15 01:23:00

客户:

  client_name |  server_dest  |  latency  |     cstat_time
  ------------+---------------+-----------+--------------------
    client1   |  server1      |  2        | 2013-05-15 01:01:30
    client2   |  server2      |  68       | 2013-05-15 01:01:40
    client2   |  server1      |  99       | 2013-05-15 01:01:50
    client1   |  server3      |  5        | 2013-05-15 01:10:00
    client2   |  server3      |  78       | 2013-05-15 01:10:30
    client2   |  server1      |  15       | 2013-05-15 01:10:50

所以,我想要的这个查询的结果是:

client_name   |  server_name  |  latency  |  server_role |    cstat_time
--------------+---------------+-----------+--------------+--------------------
    client2   |  server2      |  68       |  SECONDARY   | 2013-05-15 01:01:04
    client2   |  server1      |  99       |  PRIMARY     | 2013-05-15 01:01:50
    client2   |  server3      |  78       |  PRIMARY     | 2013-05-15 01:10:30

我需要知道延迟何时超过 60 秒,还需要知道潜在服务器在那个时间点的作用。

任何线索如何做到这一点?

我在 Postgres 8.4 上运行。

4

4 回答 4

2

如果我理解正确,您正在尝试确定特定时间的服务器角色。了解提供了开始时间,但结束时间在该服务器的下一行。要解决此问题,您需要在同一行上创建一个开始时间和结束时间的临时表,以解决 JOIN BETWEEN 操作。所以它在 MS SQL 中看起来像这样(对不起,你可能需要为 Postgres 翻译)

-- This is TSQL code SQL Server 2008 compatible
create table #svr(
    server_name varchar(10),
    server_role varchar(10),
    stime datetime
)
create table #client(
    client_name varchar(10),
    server_name varchar(10),
    latency int,
    ctime datetime
)
create table #role(
    server_name varchar(10),
    server_role varchar(10),
    stime datetime,
    etime datetime
)
insert #svr values
    ('server1','PRIMARY','2013-05-15 01:01:00'),
    ('server2','SECONDARY','2013-05-15 01:02:00'),
    ('server3','SECONDARY','2013-05-15 01:02:00'),
    ('server1','PRIMARY','2013-05-15 01:05:00'),
    ('server2','SECONDARY','2013-05-15 01:06:00'),
    ('server3','PRIMARY','2013-05-15 01:10:00'),
    ('server1','SECONDARY','2013-05-15 01:11:00'),
    ('server1','PRIMARY','2013-05-15 01:22:00'),
    ('server3','SECONDARY','2013-05-15 01:23:00')

insert #client values
('client1','server1',2,'2013-05-15 01:01:30'),
('client2','server2',68,'2013-05-15 01:01:40'),
('client2','server1',99,'2013-05-15 01:01:50'),
('client1','server3',5,'2013-05-15 01:10:00'),
('client2','server3',78,'2013-05-15 01:10:30'),
('client2','server1',15,'2013-05-15 01:10:50')

insert #role
select s1.server_name, s1.server_role, s1.stime, s2.stime
from (  select row_number() over(order by server_name,stime) as RowId,*
        from #svr
     )  as s1
join (  select row_number() over(order by server_name,stime) as RowId,*
        from #svr
     )  as s2
on s1.RowId = s2.RowId-1

select C.client_name, C.server_name, C.latency, R.server_role, C.ctime
from #client C
left join #role R on R.server_name = C.server_name 
and C.ctime between R.stime and R.etime
WHERE C.latency > 60

结果如下:

SSMS 中的结果

于 2013-05-15T18:44:54.640 回答
1

嗨,这是解决方案

SELECT C.Client_name , S.Server_name, C.latency , S.Server_role, C.Cstat_time  
from Server_table S
INNER JOIN Client C
ON C.server_dest = S.server_name
Where C.latency > 60 
于 2013-05-15T15:16:19.973 回答
1

正如模型所展示的那样,我发现您无法通过将两个表(客户端和服务器)简单地交叉到服务器名称来获取“server_role”的数据。

澄清这一点,查询

select c. *, s.server_role 
from client c left outer join server s    
on c.server_dest = s.server_name
where c.latency> 60

我们返回了以下信息:

client_name   |  server_name  |  latency  |  server_role |    cstat_time
--------------+---------------+-----------+--------------+--------------------
    client2   |  server2      |  68       |  SECONDARY   | 2013-05-15 01:01:40
    client2   |  server2      |  68       |  SECONDARY   | 2013-05-15 01:01:40
    client2   |  server1      |  99       |  PRIMARY     | 2013-05-15 01:01:50
    client2   |  server1      |  99       |  PRIMARY     | 2013-05-15 01:01:50
    client2   |  server1      |  99       |  SECONDARY   | 2013-05-15 01:01:50
    client2   |  server1      |  99       |  PRIMARY     | 2013-05-15 01:01:50
    client2   |  server3      |  78       |  SECONDARY   | 2013-05-15 01:10:30
    client2   |  server3      |  78       |  PRIMARY     | 2013-05-15 01:10:30
    client2   |  server3      |  78       |  SECONDARY   | 2013-05-15 01:10:30

我会问,当延迟 99 以将 client2 获取到服务器'server2'时,分析确实得到了 PRIMARY 服务器角色。

有什么规则要知道服务器的作用,如果你使用两个表中的时间。

于 2013-05-15T17:47:12.640 回答
0

这是一个简单的连接查询,如果我理解正确的话:

select c.*, sys.server_role
from client c left outer join
     server s
     on c.server_desc = s.server_name
where c.latency > 60
于 2013-05-15T15:15:53.767 回答