我想到的天真的做法是:
SELECT name, lev FROM
(SELECT name, levenshtein(name, *parameter*) as lev FROM my_table)
WHERE
lev = (SELECT MIN(lev) FROM
(SELECT name, levenshtein(name, *parameter*) as lev FROM my_table ));
然而,“(SELECT name, levenshtein(name, parameter) as lev FROM my_table)”子查询非常昂贵(巨大的表),重复了两次,这看起来效率极低。
我不知何故虽然你可以写:
SELECT name, lev FROM
(SELECT name, levenshtein(name, *parameter*) as lev FROM my_table) as my_temp_table
WHERE
lev = (SELECT MIN(lev) FROM my_temp_table);
但是好像没啥作用。
有没有一种干净的方法来优化查询速度?我错过了一些明显的事情吗?
我必须依赖临时表吗? (由于开销/复杂性而试图避免它,因为它们似乎不适合非常频繁/并发的查询)
SQL 忍者的任何意见都将不胜感激;)
select * from
(
SELECT *
FROM `test`.`test`
)
as temp
where compute_total_price_single=(select min(compute_total_price_single))
;
这就是我为我的问题所做的,因为它有效,我怀疑以下方法也有效:
SELECT name, lev FROM
(SELECT name, levenshtein(name, *parameter*) as lev FROM my_table) as my_temp_table
WHERE
lev = (SELECT MIN(lev));
我正在使用 MySQL 5。
SELECT * FROM
(
SELECT *
FROM `test`.`test`
) as temp
WHERE compute_total_price_single = (SELECT MIN(compute_total_price_single));
如果您只是想获取具有最小值的一行,您可以这样做:
SELECT name, levenshtein(name, *parameter*) AS lev
FROM my_table
ORDER BY lev ASC LIMIT 1;
SELECT name, min(levenshtein(name, *parameter)) as lev
FROM my_table
GROUP BY name;