Can the sort operation costing 43.7% be elliminated from the plan ?

Pearljammer1 2014-11-25 10:08:59

SQLkiwi 2014-11-25 11:21:45
The 43.7% cost is an estimate produced to help the optimizer choose between plan alternatives; it does not mean that 43.7% of the query execution time will be spent doing this sort. This is particularly true if the estimated number of rows differs from that encountered at run time, or if the distribution of values within those rows is different from the estimate.

With that said, yes it is possible to eliminate this sort by creating a suitable index on the TIMESHEET_LINE table. A suitable index could have TIMESHEET_HEADER_REF as its leading key, with the other needed columns as includes. For example:

CREATE INDEX IX_dbo_TIMESHEET_LINE__TIMESHEET_HEADER_REF
ON dbo.TIMESHEET_LINE (TIMESHEET_HEADER_REF)
INCLUDE
(
    TIMESHEET_LINE_REF
    TIMESHEET_GROUP_INPUT_TYPE_REF,
    TIMESHEET_ABSENCE_REF,
    MONETARY_VALUE, 
    MONDAY_HOURS, 
    TUESDAY_HOURS, 
    WEDNESDAY_HOURS, 
    THURSDAY_HOURS, 
    FRIDAY_HOURS, 
    SATURDAY_HOURS, 
    SUNDAY_HOURS, 
    WEEK_HOURS
);

Alternatively, if TIMESHEET_HEADER_REF and TIMESHEET_LINE_REF for a unique key, you could consider creating the index as UNIQUE on those columns and INCLUDEing the others. The general idea is the same: provide the required order using the index key, and provide the other columns as keys or includes as wider considerations dictate.