I have a query with date columns returned as output but formatted, which s causing performance degradation (for below query returning 10k records, performance degraded from 1 to 2 seconds).
Should it be stored as formatted in the table itself(may not be possible to format all existing data though) or use some date/time table instead to join and get formatted date/time as a return column ?
Is there any other ways to increase the performance degradation caused by the formatted date return columns ?
Below query shows sample formatted date columns in query
SELECT A.TransactionId ,
A.TransactionItemId ,
tid.TransactionItemDateId ,
tid.WorkDate ,
DATENAME (WEEKDAY, tid.WorkDate) AS DAY ,
CONCAT (DATENAME (WEEKDAY, tid.WorkDate), ' (', tid.WorkDate, ')') AS Code ,
tid.PayUnit ,
tid.BillUnit ,
FORMAT (CAST (tid.ClockIn AS DATETIME), 'HH:mm') ClockIn ,
FORMAT (CAST (tid.BreakIn AS DATETIME), 'HH:mm') BreakIn ,
FORMAT (CAST (tid.BreakOut AS DATETIME), 'HH:mm') BreakOut ,
FORMAT (CAST (tid.ClockOut AS DATETIME), 'HH:mm') ClockOut ,
ISNULL (tid.[Break], 0) [Break] ,
tidl.ListItem AS STATUS ,
tid.IsOverNight ,
osp.TransactionTemplateListItemId ,
tid.Note ,
tid.InvoiceText
FROM #AllTransactionItem AS A
INNER JOIN #Txn t ON t.TransactionId = A.TransactionId
INNER JOIN dbo.TransactionItemDate AS tid ON tid.TransactionItemId = A.TransactionItemId
INNER JOIN dbo.TransactionLink AS tl ON tl.TransactionId = A.TransactionId
LEFT JOIN dbo.OrganizationServiceProfile AS osp ON osp.OrganizationId = tl.OrganizationId
LEFT JOIN dbo.ListItem tidl ON tidl.ListItemId = tid.EntryStatusListItemId
LEFT JOIN dbo.Shift AS s ON s.ShiftId = tl.ShiftId
ORDER BY tid.WorkDate;