WL#1509: Pack values of non-sorted fields in the sort buffer

Status: Complete

For queries with group by and order by clauses an external file sorting might 
be performed. The way how this operation is accomplished for a query depends on 
the value of the max-length-for-sort-data parameter (system variable).
If the value is greater than the total length of the sorted fields + total 
length of the fields to be retrieved the values of all these fields are read 
into the sort buffer. Otherwise only the values of sort fields + a reference to 
the row are read into the buffer.
The former requires more time for sorting but guaranties a fast retrieval of 
the result set, as values to be returned are read sequentially from an external 
sort file.

Currently the values of the additional fields appended to the values of the 
sort fields are not packed. As a result a lot of space in the sort buffer and 
in the external sort file is wasted. E.g. a value of the type varchar(255) of 
the length 3 characters takes 255 bytes. It causes a significant slow down of 
the external sort procedure, as less sorted data can be allocated in the sort 
buffer and consequently more merge passes have to be performed.

Thus, values of the appended fields of flexible types should be packed. If 
total length of the sorted fields and the appended packed values for a row 
exceeds the value of max-length-for-sort-data a reference to the row is 
appended to the values of sorted fields.

User Documentation
==================

http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html
http://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-3.html