这个sql 这样优化可行吗?select  distinct e.dd from   (select distinct te.eventId from TE te ) te, et  e where   te.eventId=e.id  and e.isLatest='Y' union select   distinct e.dd from   (select DISTINCT tm.eventId from TM tm ) tm, et  e where   tm.eventId=e.id  and e.isLatest='Y';我的想法是修改为以下来优化
select  distinct e.dd from   (select distinct te.eventId from TE te ) te, et  e where   e.isLatest='Y'  and te.eventId=e.id   union select   distinct e.dd from   (select DISTINCT tm.eventId from TM tm ) tm, et  e where  e.isLatest='Y'and   tm.eventId=e.id  这样做的根据:
e.isLatest='Y'数据很多 ,这样避免每个记录都做比较, 提到前面sql可以只做到链接一部分数据 ,否则 te.eventId=e.id 都是接近走全表,   这里分析是对的吗??? (经过验证有时偶快点, 快得不是很多)

解决方案 »

  1.   

    MySQL 会自行根据索引的情况进行优化,前后顺序对它来说不重要。
      

  2.   

    select distinct e.dd from (select distinct te.eventId from TE te ) te, et e where te.eventId=e.id and e.isLatest='Y' union select distinct e.dd from (select DISTINCT tm.eventId from TM tm ) tm, et e where tm.eventId=e.id and e.isLatest='Y';那么请看看如何优化 是否每个表的  eventId   id 等 各自单独建立 一个字段的索引???
      

  3.   

    MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html