I interested why we get 2 different query plans on Sybase 15.5 and 15.7 for similar queries
declare @MID int
select @MID = 25717
select MID , max(Date) from CB where MID = @MID group by MID
select @MID, max(Date) from CB where MID = @MID
The index is on (MID, Date).
The first does forward scan and the second does a backward scan.
With our data, the first query does 8000 page reads (with a SAN read costing 4ms = 32s)
and the second query does 4 page reads (with a SAN read costing 4ms = 16ms)
1)
| | |GROUP SORTED Operator (VA = 1)
| | | Evaluate Grouped MAXIMUM AGGREGATE.
| | |
| | | |SCAN Operator (VA = 0)
| | | | FROM TABLE
| | | | CB
| | | | Index : IDX1
| | | | Forward Scan.
| | | | Positioning by key.
| | | | Index contains all needed columns. Base table will not be read.
| | | | Keys are:
| | | | MID ASC
| | | | Using I/O Size 16 Kbytes for index leaf pages.
| | | | With LRU Buffer Replacement Strategy for index leaf pages.
2)
| | Evaluate Ungrouped MAXIMUM AGGREGATE.
| | Scanning only up to the first qualifying row.
| |
| | |SCAN Operator (VA = 0)
| | | FROM TABLE
| | | CB
| | | Index : IDX1
| | | Backward scan.
| | | Positioning by key.
| | | Index contains all needed columns. Base table will not be read.
| | | Keys are:
| | | MID ASC
| | | Using I/O Size 2 Kbytes for index leaf pages.
| | | With LRU Buffer Replacement Strategy for index leaf pages.
Why does the optimiser generate different plans ?