Database query performance issues have always been a core challenge for DBAs. In some cases, inefficient queries not only affect individual operations but can also trigger instance-level performance meltdowns. That’s why every seasoned DBA must master the methodology of query performance analysis and troubleshooting.
Although there are many factors that affect query performance, the key to solving problems always lies in deeply interpreting the SQL execution plan. The execution plan shows us the sequence of steps for SQL execution (such as physical operators and logical operation order), allowing us to precisely locate performance issues.
In this article, we will focus on a commonly overlooked but extremely harmful performance killer — implicit conversions.
1. What Are Implicit Conversions?
In layman’s terms, when an operator is used with operands of different types, type conversion occurs to make the operands compatible, and this is when implicit conversion happens. The presence of implicit conversion often means that the execution efficiency of SQL will be significantly reduced.
2. What Impact Does Implicit Conversion Have on Performance?
In normal operations and maintenance scenarios, for a SQL query to execute efficiently, it will try to hit an index. The index column is stored in order according to the column type at the underlying level. When the query value and the column type are inconsistent, the database needs to traverse the index data and convert it for comparison, which prevents the index from being hit.
Next, we will combine several common scenarios to let you actually experience what implicit conversion is and how to deal with it.
3. Scenarios
The following scenarios will use MySQL version 8.0.37 for examples.
3.1 Data Preparation
First, use a stored procedure to generate 1 million test data rows.
-- Create test data table
DROPTABLEIFEXISTSemployees;CREATETABLEemployees(emp_novarchar(25)NOTNULL,namevarchar(25)NOTNULL,salaryint(10)NOTNULL,rolevarchar(25)NOTNULL,INDEXidx_emp_no(emp_no),INDEXidx_name(name),INDEXidx_salary(salary))ENGINE=InnoDBDEFAULTCHARSET=utf8mb4;-- Stored procedure to insert 1 million test data
DELIMITER$$CREATEPROCEDUREinsert_test_data()BEGINDECLAREiINTDEFAULT0;DECLARErandom_roleVARCHAR(25);WHILEi<1000000DOSETrandom_role=IF(RAND()<0.5,'Software Engineer','DevOps Engineer');INSERTINTOemployees(emp_no,name,salary,role)VALUES(CONCAT('',i),CONCAT('User_',LEFT(UUID(),8)),-- Generate random name
FLOOR(RAND()*8000),-- Generate random salary (0-8000)
random_role);SETi=i+1;IFi%1000=0THEN-- Commit every 1000 rows
COMMIT;ENDIF;ENDWHILE;END$$DELIMITER;callinsert_test_data();
3.2 Implicit Conversion Caused by Inconsistent Data Types Between Constants and Fields
A classic scenario is when the data type of the constant does not match the field type, causing implicit conversion. This is also the most common situation we encounter.
The Type part of the execution plan shows ALL, indicating a full table scan, and the index idx_emp_no is not used. This usually happens when the data type passed does not match the actual field type.
If we make a slight adjustment, the SQL query becomes:
When the input data matches the varchar type of the field, the Type part of the execution plan shows ref, indicating that the SQL query can use the index properly.
From the example above, developers should be extra cautious when writing code like the following:
1
2
3
cursor.execute("SELECT * FROM employees WHERE emp_no = %s",(emp_no,));cursor.execute("SELECT * FROM employees WHERE emp_no = '%s'",(emp_no,));
The presence or absence of quotes around %s can have a significant impact on performance.
3.3 Implicit Conversion Caused by Inconsistent Data Types in Join Fields
In practical scenarios, joining tables is also very common. When the data types of the join fields are inconsistent, implicit conversion can occur.
Let’s prepare two more tables:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATETABLEorders_1(emp_noint(12)NOTNULL,-- Data type inconsistent with emp_no in employees
order_datedatetimeDEFAULTNULL,INDEXidx_emp_no(emp_no))ENGINE=InnoDBDEFAULTCHARSET=utf8mb4COLLATE=utf8mb4_0900_ai_ci;insertintoorders_1values(12,NOW());CREATETABLEorders_2(emp_novarchar(25)NOTNULL,-- Data type inconsistent with emp_no in employees
order_datedatetimeDEFAULTNULL,INDEXidx_emp_no(emp_no))ENGINE=InnoDBDEFAULTCHARSET=utf8mb4COLLATE=utf8mb4_0900_ai_ci;insertintoorders_2values(12,NOW());
Let’s test the situation where the join fields have inconsistent data types.
The emp_no field in orders_3 has the same data type as in employees, but the collation of the character set is inconsistent. The collation is used to compare two strings, and when they are inconsistent, the database cannot compare them and resorts to a full table scan, which greatly reduces query performance.
The examples above are relatively simple and can be identified with the naked eye. However, in development and operations, once the SQL becomes complex, the manual tuning cost will skyrocket.
4. SQLFLASH Demo
Let’s see how SQLFLASH can help us identify SQL performance optimization points and transform them.
There is an implicit conversion in line 10 of the above example. Let’s see how SQLFLASH performs:
SQLFLASH clearly informed me that it helped optimize an implicit conversion. Let’s see if the modification is correct:
SQLFLASH has correctly modified it. For other optimization rules, interested friends can experiment on their own.
5. Summary
In this article, we have mainly introduced various scenarios of implicit conversions, such as inconsistent field types, inconsistent join field types, inconsistent character set types, or inconsistent collation rules. When SQL performance issues arise due to implicit conversions, analyzing the corresponding scenarios and taking appropriate measures can resolve the problem.
In addition, in our daily development and operations, we can also use tools to help us improve efficiency. SQLFLASH does a great job in this regard.
SQLFlash is your AI-powered SQL Optimization Partner.
Based on AI models, we accurately identify SQL performance bottlenecks and optimize query performance, freeing you from the cumbersome SQL tuning process so you can fully focus on developing and implementing business logic.