Database administrators (DBAs) constantly strive to optimize query performance. Traditional indexing on raw column values often falls short when queries involve functions like month()
. This blog delves into a revolutionary feature introduced in MySQL 8.0.13 - functional indexes. We'll explore how functional indexes overcome limitations and unlock significant query speedups, even when using functions in your WHERE clauses.
MySQL has introduced the concept of functional index in MySQL 8.0.13. It is one of the much needed feature for query optimisation , we have seen about histogram in my last blog. Let us explore the functional index and its use cases.
For the below explanation, I have used a production scenario which has 16 core cpu, 32GB RAM and with MySQL version 8.0.16(Latest at the time of writing).
MySQL do support indexing on columns or prefixes of column values (length).
Example:
In MySQL 5.7, We can create an index on generated columns (Computed based on Expressions)
From MySQL 8.0.13, It is even easier by using the feature Functional Indexing. It is also implemented as a hidden virtual column. It inherits all restrictions that apply to generated columns,
Let’s see how it can ease DBA’s life.
The app_user’s table has around 9M records and data from Sep 2018.
Now Let us consider a case to query the records which are created in the month of May. I have used pager md5sum for easier result verification.
It took 5 seconds to fetch 74322 records. Here is the explain plan for the above query
No index is used. Let us try adding an index on column createdOn to speed up this query.
Explain plan post indexing
Even after adding index, it goes for full table scan (Indexing is not used) because we are using month() function in WHERE clause it masks the index usage. And there is no improvement in query performance.
In this case, we need to rewrite the query replacing the date functions to use the index or else we can create a virtual column for the functional column in where clause of the query and create an index on top of it. But in MySQL 8.0, it makes our work even Simpler. We can create a functional index.
Now it is using an optimal (functional) index and query execution time is also reduced significantly.
But there are a few restrictions on creating a functional index.
1) Only functions permitted for generated columns(5.7) are permitted for functional key parts.
2) Primary key cannot be included in functional key parts.
3) Spatial and full text indexes cannot have functional key parts
To drop the columns containing functional index, we need to remove index first before dropping the column else it will throw an error.
Let us try to drop the column createdOn (contains functional index).
The functional index is an interesting feature in MySQL 8.0 and a must to tried out by DBA’s.
Ready to experience the power of functional indexes and optimize your MySQL queries? Let Mydbops Manage Your MySQL Performance: Free Consultation! Contact Us Today !
{{cta}}
Featured Image Courtesy Photo by Antoine Dautry on Unsplash