我在mySQL(mySQL中的第一个函数)中编写了一个简单的搜索函数,这对我来说效果很好,但性能很糟糕。我使用一个非常大的数据库,需要5秒才能得到结果。这需要太长时间。要比较,搜索"如%"不到0.1秒。
DELIMITER $$
CREATE FUNCTION search (myColumn VARCHAR (255), mySearch VARCHAR (255)) RETURNS INT
BEGIN
DECLARE i, j, scorePoints, columnLength INT;
DECLARE result, aSpace VARCHAR (225);
SET i = 1, j = 1, scorePoints = 0, columnLength = CHAR_LENGTH(myColumn);
WHILE (j <= columnLength) DO
SET result = STRCMP(SUBSTRING(myColumn, j, 1), SUBSTRING(mySearch, i, 1));
IF (result = '0') THEN SET scorePoints = (scorePoints + 1); END IF;
SET aSpace = STRCMP(SUBSTRING(myColumn, j, 1), " ");
IF aSpace = '0'THEN SET i = 0; END IF;
SET i = (i + 1), j = (j + 1);
END WHILE;
RETURN (scorePoints - columnLength);
END $$
DELIMITER ;
SELECT
CONCAT_WS(' ', t.name, t.familyname) AS myData,
SEARCH(CONCAT_WS(' ', t.name, t.familyname), REPLACE('ABC', ' ', '')) AS score,
t.name,
t.familyname
FROM
myTable t
WHERE
CONCAT_WS(' ', t.name, t.familyname) LIKE 'ABC%'
ORDER BY score DESC;
我需要&lt; 1秒的总时间。请有人帮助我提高这个mySQL函数的性能。
非常感谢。
托马斯。