Question

this is an odd question. let me lay out my table structures...

my 'server' table gives me status information of my servers (i get a few of these messages every minute). my 'client' table is updated with client info (clients connected to servers, obviously).

one server in the cluster is always the 'primary', so to speak (others are secondary). each client delivers me latency info to each of my servers.

i need to see the rows where the client latency is greater than 60 seconds, but i also need to know the status of each server as well.

here are example tables (server, then client):

  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:

  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

so, my desired result of this query would be:

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

i need to know when that latency is over 60 seconds, but also the role of the latent server at that point in time.

any clue how to do this?

i'm running on Postgres 8.4.

Was it helpful?

Solution

If I understand correctly, you are trying to determine the server role for a specific time. Understanding that Start Time is provided but End Time is on the next line for that server. To resolve this, you need to create a temp table with the start and end time on the same line to resolve the JOIN BETWEEN operation. So it would look like this in MS SQL (Sorry, you might have to translate for 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

Here's the result:

Result in SSMS

OTHER TIPS

As the model presented, I see that you can not get the data of 'server_role' with simple crossing of the two tables (client and server) to the server name.

Clarifying this, the query

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

We returned the following information:

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

I would ask that analysis did to get that is PRIMARY server role when the latency of 99 to get the client2 to server 'server2'.

What rules have to know the role of the server, if you use the time in the two tables.

hi here is the Solution

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 

This is a simple join query, if I understand it correctly:

select c.*, sys.server_role
from client c left outer join
     server s
     on c.server_desc = s.server_name
where c.latency > 60
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top