Datetimeoffset values retain original offset causing cross‑region query mismatch
0 reputation · 05 Sept 2021, 16:35 UTC
0 reputation · 05 Sept 2021, 16:35 UTC
Ensure that datetime values retrieved from Azure SQL Database are interpreted consistently across application instances deployed in different Azure regions, without requiring each client to apply custom logic.
Azure SQL Database stores datetimeoffset values with the original time‑zone offset and returns them unchanged on SELECT; there is no server‑side automatic conversion to UTC or a target zone. Developers must use the AT TIME ZONE expression (available from SQL Server 2016) to normalize values, but it is unclear whether applying this expression per row introduces noticeable overhead or whether a schema‑level solution (e.g., computed column) is preferable.
What is the recommended strategy for achieving UTC‑normalized results in cross‑region scenarios?
Does using AT TIME ZONE on large result sets affect query performance compared to storing UTC‑only datetime2 columns?
Can a persisted computed column with AT TIME ZONE be used to provide a normalized view without altering existing INSERT logic?
For cross-region Azure SQL Database deployments, the recommended strategy depends on whether you can change the storage schema:
datetime2 (or datetimeoffset with fixed +00:00) and normalize in the application layer before INSERT. This eliminates read-time conversion entirely.utc_ts AS (ts AT TIME ZONE 'UTC') PERSISTED. This materializes the UTC value at write time, requires no INSERT changes, and allows indexing for UTC-range queries.AT TIME ZONE 'UTC' in SELECT. Expect <5% overhead for typical OLTP page sizes; cost grows linearly with row count.datetimeoffset with the supplied offset and returns it unchanged. No server-side automatic UTC conversion exists.AT TIME ZONE 'UTC' converts per row at read time (Compute Scalar operator). Benchmarks show low single-digit microseconds per row; <5% overhead vs raw retrieval for typical page sizes.utc_ts AS (ts AT TIME ZONE 'UTC') PERSISTED is deterministic, materializes on INSERT/UPDATE, adds ~8 bytes/row, and does not alter INSERT logic — clients still supply the original offset.AT TIME ZONE may become measurable; the persisted column shifts that cost to write time.AT TIME ZONE; Azure SQL receives regular DST updates, but verify after major tzdata releases if you target non-UTC IANA zones.'UTC' vs 'UTC+0' vs IANA name) to avoid mismatches.AT TIME ZONE support: SELECT SYSDATETIMEOFFSET() AT TIME ZONE 'UTC' AS utc_now; — should return a datetimeoffset with +00:00 offset.ALTER TABLE dbo.Events
ADD utc_ts AS (ts AT TIME ZONE 'UTC') PERSISTED;
utc_ts for frequent UTC-range queries.SET STATISTICS TIME, IO ON; compare SELECT ts AT TIME ZONE 'UTC' FROM dbo.Events vs SELECT utc_ts FROM dbo.Events at expected row counts.What is the typical row count returned by your cross-region queries? If you regularly scan >100k rows per request, the persisted column becomes strongly preferred; below that, ad-hoc AT TIME ZONE is usually acceptable.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.