When my application starts, I'm retrieving the date/time from SQL Server using SELECT CURRENT_TIMESTAMP and comparing the difference with the local date/time - and recording this offset. Every action which records the date/time will be adjusted based on this time difference. So whenever I save any date/time, it will be that of the server - without actually querying the server numerous times each time I need it.

The problem is, let's assume this server is halfway around the world, and there's a bit of lag in the connection. I need to account for that lag too. Not the lag from request-to-response, but rather server-receives-request-to-response.

How do I measure this lag time between the moment that the SQL Server obtains its current date/time to the moment when my application receives it?

Considering SQL Server is such a powerful database engine, I would assume there's some built-in trick to accommodate for this.

有帮助吗?

解决方案

This is a more generic problem actually in data communication theory, not just a SQL Server thing. One way to tackle it would be to do a 3 or 5 way handshake in the start and find the median time of one ACK. Then use that as Delta for all future communications.

In SQL Server case, you could run a very simple query like SELECT 1 and check how much time it takes to get back results. One pitfall here is to avoid using any low-level mechanism like TCP (in case u have control of the server side too) because executing a query will make sure the total delta you compute includes the time for SQL Server to receive the query and parse it etc. as well.

其他提示

There's no way to be sure. Consider that request-to-receipt takes 3 seconds. It's possible that it took 2 seconds for request to get to the server, and 1 second for the answer to get back to you. Or perhaps it took 1 second for request-to-server and 2 seconds for server-to-receipt.

You could assume that the two are about equal and just divide the total delay by two. You might want to make several samples and average them.

Consider also that the delay will vary over time. A request you send now might have a delay of 1 second, but a request sent an hour from now might take half that time or double that time, depending on the load on the network.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top