Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Left() or SUBSTRING()?

Both of these queries will produce same result. But which one is better, faster.

SELECT LEFT(textField, 100) AS first100Chars

SELECT SUBSTRING(textField, 1, 100 )

Thanks.

like image 437
Pars Avatar asked Dec 04 '13 18:12

Pars


People also ask

Is substring faster than left?

According to the results, on nonindexed columns, LEFT/RIGHT are consistently faster than SUBSTRING.

What is left () in MySQL?

LEFT() function in MySQL is used to extract a specified number of characters from the left side of a given string. It uses its second argument to decide, how many characters it should return.

Is Left () a string function?

The LEFT function is one of the built-in string functions and returns the left part of the input character string according to the specified number of characters.

What is a substring in MySQL?

SUBSTRING() : function in MySQL is used to derive substring from any given string . It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string.


2 Answers

By itself there is little difference, however left can potentially utilize an index while substring cannot.

like image 52
Konstantin Avatar answered Sep 21 '22 12:09

Konstantin


Substring will take little more time than left function. Though your case is straight forward use left function only.

like image 20
Pankhuri Kaushik Avatar answered Sep 18 '22 12:09

Pankhuri Kaushik