背景:用一个表中的父子级关系进行查询 优化的过程中 发现可以使用 or 来代替 union all
union all 需要查询两次 表 而 使用 or只需要 查询 一次 并且 两个字段都建立了索引
 
SELECT OrganizationCode FROM organization WHERE OrganizationCode IN ('44010000')
UNION ALL SELECT OrganizationCode FROM organization WHERE UpperComCode IN ('44010000');
 
SELECT OrganizationCode FROM organization WHERE OrganizationCode IN ('44010000') OR uppercomcode IN ('44010000')
 
第一个过程用时:0.00012s
my sql 两个 索引 时的 union  与 or 的比较

 

my sql 两个 索引 时的 union  与 or 的比较
第二个过程用时:0.00067s
my sql 两个 索引 时的 union  与 or 的比较

 

 

 
第一个过程索引
my sql 两个 索引 时的 union  与 or 的比较

 


 

第二个过程索引类型
my sql 两个 索引 时的 union  与 or 的比较

 


 

 
 
数据表数据
my sql 两个 索引 时的 union  与 or 的比较

 

 
my sql 两个 索引 时的 union  与 or 的比较

 

 
 
结论
索引类型不同
union all 在此时 比or 更快

 

相关文章: