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.
查询的目的是根据上述查询条件去除重复的行。查询将表“table1”连接到自身。我们创建了带有组索引的索引
- license_id
- 服务日期
- 客户ID
- history_id(主键)
- op_id。
它可以正确执行,但添加OR (t1.service_date IS NULL AND t2.service_date IS NULL)
会使查询执行速度非常慢。该表有 2 个以上的数据缺失。
我们使用了 MySQLEXPLAIN
命令,这是输出
如何提高查询执行时间?