SELECT t1 . * FROM table1 t1, table1 t2
WHERE t1.history_id < t2.history_id
AND (t1.license_id = t2.license_id
OR (t1.license_id IS NULL AND t2.license_id IS NULL))
AND t1.op_id = t2.op_id
AND (t1.service_date = t2.service_date
OR (t1.service_date IS NULL AND t2.service_date IS NULL))
AND t1.customer_id = t2.customer_id
AND t1.serial_id = t2.serial_id.
The purpose of the query is to remove duplicated rows based on the above query conditions. The query join table 'table1' to itself. We have created index with group index for
- license_id
- service_date
- customer_id
- history_id (primary key)
- op_id.
It executes correctly but with the addition of OR (t1.service_date IS NULL AND t2.service_date IS NULL)
makes the query execution very slower. The table has more than 2 lacks of data.
We have used MySQL EXPLAIN
command and here is the output
How can I improve the query execution time?