The solution to the index invalidation caused by the oracle database to_date function

Problem Description:

Query a table structure of Oralce. In the scenario where there is no time partition, the Explain is a table full when the span is two months. However, when the time range is short, the time index is normally used. This table has more than 1.3 million data. The pseudo-sql is as follows:

SELECT
	* 
FROM
	tableName number_fpri 
WHERE
	number_fpri .INSERT_DATE BETWEEN TO_DATE( '2022-01-11 00:00:00', 'yyyy-MM-dd HH24:mi:ss' ) 
	AND TO_DATE( '2023-01-11 23:59:59', 'yyyy-MM-dd HH24:mi:ss' )

The result of Explain is as follows:
insert image description here
Solution:
Replace the TO_DATE function and use the TO_CHAR function instead. Some friends will ask, is there a ball?
really!
It is useless to change TO_CHAR, you need to add the function index of TO_CHAR

create index ind_date_to_char_flightdate on tableName (TO_CHAR(INSERT_DATE ,'yyyy-MM-dd HH24:mi:ss'));

Then modify the sql as follows:

SELECT
	* 
FROM
	tableName number_fpri 
WHERE
TO_CHAR( number_fpri.INSERT_DATE , 'yyyy-MM-dd HH24:mi:ss' ) BETWEEN '2022-10-11 00:00:00' 
		AND '2023-01-11 23:59:59' 

The Explain result is as follows:
insert image description here
This completes the sql optimization

In some cases, using the TO_DATE function may cause query performance to degrade without the use of indexes, while using the TO_CHAR function can use indexes to improve query performance. This is usually because the date format stored in the database is different from the date format used in the query, or because the date format string is different, preventing the use of indexes to speed up the query.

For example, suppose we have an orders table, which contains an order_date field, the data type is DATE, and the stored date format is 'YYYY-MM-DD HH24:MI:SS'. If we want to query the order quantity of a certain day, and use the TO_DATE function to convert the parameter of string type to DATE type, we can use the following SQL statement:

SELECT COUNT(*) FROM orders WHERE order_date = TO_DATE('2022-06-26', 'YYYY-MM-DD');

In this query, since we convert the string type parameter to DATE type, we can use the index on order_date to speed up the query. However, if we use a different date format string in the query, it may cause the index to fail, so that the index cannot be used to improve query performance.

For example, the following SQL statements use different date formatting strings:

SELECT COUNT(*) FROM orders WHERE order_date = TO_DATE('2022/06/26', 'YYYY/MM/DD');

In this query, due to the use of different date format strings, the index on order_date cannot be used to speed up the query, but a full table scan is required, which affects query performance.

If we convert the order_date field to a string type using the TO_CHAR function and compare it with the date format string used in the query, we can use the index to improve query performance. For example, the following SQL statement uses the TO_CHAR function:

SELECT COUNT(*) FROM orders WHERE TO_CHAR(order_date, 'YYYY-MM-DD') = '2022-06-26';

In this query, we used the TO_CHAR function to convert the order_date field to a string type, and used the date format string used in the query for comparison. Since the values ​​of the string type are compared, an index can be created on the converted string type field to improve query performance.

In general, when using the TO_DATE function or TO_CHAR function, you need to pay attention to whether the date format string is consistent with the date format stored in the database. Inconsistent date formats can invalidate the index, making it impossible to use the index to improve query performance.

Guess you like

Origin blog.csdn.net/Tanganling/article/details/128647232