Error
Error Code:
3758
MySQL Error 3758: Disallowed Functional Index Function
Description
This error occurs when attempting to create or alter a functional index in MySQL, and the expression used for the index contains a function that is not permitted. Functional indexes have specific limitations on the types of functions that can be included in their definitions, often requiring them to be deterministic and safe for indexing operations.
Error Message
Expression of functional index '%s' contains a disallowed function.
Known Causes
4 known causesUsing Non-Deterministic Functions
Functional indexes require deterministic functions that always return the same result for the same input. Functions like RAND(), NOW(), or user-defined functions (UDFs) that are non-deterministic are typically disallowed.
Referencing Restricted Built-in Functions
Some built-in MySQL functions, even if deterministic, might be explicitly restricted from functional index expressions due to internal limitations, performance implications, or potential side effects not suitable for indexing.
Incorrect Function Syntax or Parameters
While less common, using a function with incorrect syntax or invalid parameters might lead to it being interpreted as fundamentally flawed or disallowed, preventing the index from being created.
Security or Resource-Intensive Functions
Functions that could pose security risks (e.g., file system access) or are excessively resource-intensive may be restricted from functional indexes to maintain database stability and security.
Solutions
3 solutions available1. Replace Disallowed Function with Allowed Alternative medium
Identify and substitute the problematic function in your functional index expression with one that is permitted by MySQL.
1
Identify the disallowed function in the functional index expression. The error message usually provides the name of the function.
SELECT INDEX_NAME, COLUMN_NAME, SUB_PART, EXPRESSION FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = 'your_database_name' AND EXPRESSION LIKE '%disallowed_function_name%';
2
Consult the MySQL documentation for a list of allowed functions for functional indexes. Common disallowed functions include those that are non-deterministic or have side effects.
Refer to the official MySQL documentation for 'Functional Indexes' or 'Generated Columns' for a comprehensive list of supported functions.
3
Rewrite the functional index expression using an allowed function. For example, if `RAND()` was used (which is disallowed), you might need to rethink the indexing strategy or use a deterministic function.
ALTER TABLE your_table_name DROP INDEX your_functional_index_name;
ALTER TABLE your_table_name ADD INDEX your_new_functional_index_name (your_column, allowed_function(another_column));
4
Verify the index has been created successfully by checking the information schema.
SHOW CREATE TABLE your_table_name;
2. Remove the Functional Index easy
If the functional index is not critical for performance or can be achieved through other means, remove it to resolve the error.
1
Identify the name of the functional index causing the error. This is typically found in the error message.
SHOW CREATE TABLE your_table_name;
2
Drop the functional index using the `DROP INDEX` statement.
ALTER TABLE your_table_name DROP INDEX your_functional_index_name;
3
Confirm the index has been removed by re-checking the table structure.
SHOW CREATE TABLE your_table_name;
3. Use a Generated Column Instead of a Functional Index medium
MySQL 5.7+ supports generated columns, which can sometimes serve a similar purpose to functional indexes and may have different function restrictions.
1
Identify the expression used in the problematic functional index.
SELECT EXPRESSION FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = 'your_database_name' AND INDEX_NAME = 'your_functional_index_name';
2
Determine if the expression can be used to define a generated column. MySQL supports both `VIRTUAL` and `STORED` generated columns.
Refer to MySQL documentation on 'Generated Columns' for syntax and supported expressions.
3
Drop the problematic functional index.
ALTER TABLE your_table_name DROP INDEX your_functional_index_name;
4
Add a generated column with the desired expression.
ALTER TABLE your_table_name ADD COLUMN generated_column_name data_type AS (your_expression) [VIRTUAL | STORED];
5
Create a regular index on the newly created generated column.
ALTER TABLE your_table_name ADD INDEX idx_generated_column (generated_column_name);