If I have TEXT data type (up to 65,535 characters), is taking first 500 characters faster than the whole column? There is a LEFT function that can particularly be used in this situation, but I’m wondering if it improves performance or maybe downgrades it, since it is a function after all.
2
Answers
The InnoDB storage engine has to read the full TEXT content regardless. The
LEFT()
function operates on the full string. It doesn’t have any way of telling the storage engine to read only part of the string.In an RDBMS where functions had intimate knowledge of the storage format, string functions like
LEFT()
could be optimized in clever ways.But MySQL has a distinct plugin architecture to implement ar variety of storage engines. The storage code is separate from storage-independent things like builtin string functions. So a string function has no opportunity to request part of a TEXT column.
The code that implements MySQL’s
LEFT()
function is here: https://github.com/mysql/mysql-server/blob/8.0/sql/item_strfunc.cc#L1443-L1461The only optimization is that it checks the length of the string. If the string is already shorter than the requested substring, it just returns the whole string. This implies that the full string must be available to check the length.
In addition to Bill Karwin’s answer..
You can easily check that with the
BENCHMARK()
function, which shows that there is no difference.If you do a SELECT with LEFT() it will be of course much faster, since network traffic (but also memory usage) is much smaller.