Home > database >  In the mysql query using a custom function is very slow
In the mysql query using a custom function is very slow

Time:05-01

I find mysql set function used in the query will be very slow, even do the simple calculation, as long as use the custom function, use native function slower than an order of magnitude, and each in a custom function performs an even the simplest statement will be slower,
The following is a simple test: I do
First create a including ten thousand data list:
The create table TMP (s varchar (1000)) as the select... Limit 10000;
Then create three custom function, simple calculation characters in length and 10 times addition:

Mylength1: add ten times in a single statement;
 
DELIMITER $$
The CREATE FUNCTION ` mylength1 ` (s1 VARCHAR (255)) RETURNS an int (11)
DETERMINISTIC
The BEGIN
DECLARE s1_len, INT I;
The SET s1_len=0;
The SET s1_len=s1_len + length (s1) + length (s1) + length (s1) + length (s1) + length (s1) + length (s1) + length (s1) + length (s1) + length (s1) + length (s1);
RETURN s1_len;
END; $$

Mylength2: through ten plus ten times in the statement;
 
DELIMITER $$
The CREATE FUNCTION ` mylength2 ` (s1 VARCHAR (255)) RETURNS an int (11)
DETERMINISTIC
The BEGIN
DECLARE s1_len, INT I;
The SET s1_len=0;
The SET s1_len=s1_len + length (s1);
The SET s1_len=s1_len + length (s1);
The SET s1_len=s1_len + length (s1);
The SET s1_len=s1_len + length (s1);
The SET s1_len=s1_len + length (s1);
The SET s1_len=s1_len + length (s1);
The SET s1_len=s1_len + length (s1);
The SET s1_len=s1_len + length (s1);
The SET s1_len=s1_len + length (s1);
The SET s1_len=s1_len + length (s1);
RETURN s1_len;
END; $$


Mylength3: use a while loop statements complete plus ten times;
 
DELIMITER $$
The CREATE FUNCTION ` mylength3 ` (s1 VARCHAR (255)) RETURNS an int (11)
DETERMINISTIC
The BEGIN
DECLARE s1_len, INT I;
The SET s1_len=length (s1);
The SET I=0;
WHILE I & lt;=10 DO
The SET s1_len=s1_len + length (s1);
The SET I=I + 1;
END the WHILE;
RETURN s1_len;
END; $$


Then compare directly using the native function, used for comparison of SQL is as follows:
 
The select Max (length (s)) from a TMP.
The select Max (length (s) + length (s) + length (s) + length (s) + length (s) + length (s) + length (s) + length (s) + length (s) + length (s) from a TMP.
The select Max (mylength1 (s)) from TMP a;
The select Max (mylength2 (s)) from TMP a;
The select Max (mylength3 (s)) from TMP a;
The select Max (levenshtein (' any string 's)) from TMP a;


The results are as follows:


Can see from the above results:
1. The primary function called one and took almost 10 times, is done 0.01 seconds; In fact calls 10 secondary slightly slow, sometimes is 0.02 seconds, but not much difference;
2. Call mylength1, a custom function in a statement to complete 10 times addition, takes 0.39 seconds, increased dozens of times;
3. Call mylenght2, custom function use 10 statements complete addition, 10 times takes 0.52 seconds, and increased by 30%;
4. Call mylength3 custom function using a while loop to do addition, 10 times takes 1.06 seconds, double again;
5. Call the levenshtein custom function, takes 9.21 seconds, and increased nearly 10 times;

Can call the custom function in mysql query efficiency low, what is the solution?
I find the article on the BBS, but does not solve the problem:
https://bugs.mysql.com/bug.php? Id=34254
  • Related