英文:
Can't create a functional index on top of my custom function
问题
I'm playing with functional index in MySQL 8, and I'm using the employees database.
So I created this function:
DELIMITER $$
CREATE FUNCTION salary_range2(salary DECIMAL(10,2))
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
RETURN CASE
WHEN salary < 40000 THEN 1
WHEN salary < 80000 THEN 2
WHEN salary < 120000 THEN 3
ELSE 4
END;
END$$
DELIMITER ;
And this statement fails:
CREATE INDEX idx_salary_range ON salaries ((salary_range2(salary)));
With error: [HY000][3758] Expression of functional index 'idx_salary_range' contains a disallowed function.
My doubt was if the function was deterministic or not, but it seems this is not the problem, because the deterministic signature is not checked by MySQL. And by the way, it seems to me deterministic.
Am I missing something? Or any workaround to this problem, like creating a temporary column, views, or a table? But I was interested in experimenting with some functional index out of hello world tutorials.
I tried to create a functional index with my own custom function, and it fails with the error [HY000][3758] Expression of functional index 'idx_salary_range' contains a disallowed function.
英文:
I'm playing with functional index in MySQL 8, and i'm using the *employees* database.
So I created this function:
DELIMITER $$
CREATE FUNCTION salary_range2(salary DECIMAL(10,2))
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
RETURN CASE
WHEN salary < 40000 THEN 1
WHEN salary < 80000 THEN 2
WHEN salary < 120000 THEN 3
ELSE 4
END;
END$$
DELIMITER ;
And this statement fails:
CREATE INDEX idx_salary_range ON salaries ((
salary_range2
(salary)));
With error: [HY000][3758] Expression of functional index 'idx_salary_range' contains a disallowed function.
My doubt was if the function was deterministic or not, but it seems this is not the problem, because the deterministc signature is not checked by MySQL. And by the way it seems to me deterministc.
Am I missing something ? Or any workaround to this problem, like creating temporary column, views, or table ? But i was interested in experimenting some funcional index out of hello world tutorials.
I tried to create a functional index with my own custom function and it fails with the error [HY000][3758] Expression of functional index 'idx_salary_range' contains a disallowed function.
答案1
得分: -1
如mysql文档中的创建索引所述:
> 函数键部分继承所有适用于生成列的限制。例如:
> 仅允许用于生成列的函数可用于函数键部分。
> 子查询、参数、变量、存储函数 和可加载函数不允许。
您不能在函数索引中使用函数。但是,您可以直接在函数索引的函数体中使用该表达式。
英文:
As mysql documentation on create index says:
> Functional key parts inherit all restrictions that apply to generated columns. Examples:
> Only functions permitted for generated columns are permitted for functional key parts.
> Subqueries, parameters, variables, stored functions, and loadable functions are not permitted.
You simply cannot use your function in a functional index. However, you can use the expression in the body of the function directly in the functional index.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论