如何使用索引提高查询性能?

时间:2012-11-08 12:57:16

标签: php mysql performance indexing

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'连接到自身。我们用

的组索引创建了索引
  1. license_id
  2. service_date
  3. CUSTOMER_ID
  4. history_id(主键)
  5. op_id。
  6. 它正确执行但添加OR (t1.service_date IS NULL AND t2.service_date IS NULL)会使查询执行速度变慢。该表有两个以上缺乏数据。

    我们使用了MySQL EXPLAIN命令,这是输出

    enter image description here

    如何改善查询执行时间?

1 个答案:

答案 0 :(得分:0)

问题是'IS NULL'。 Plase阅读此内容以获取更多信息:when to use IS NULL

一种解决方案是从表中删除NULL值(例如将其更改为'0',因为它不是有效的MYSQL自动增量值),然后将'IS NULL'更改为'= 0'