select
        count(distinct actiontrac0_.id) as col_0_0_ 
    from
        action_trace actiontrac0_ 
    where
        (
            actiontrac0_.id in (
                select
                    max(actiontrac3_.id) 
                from
                    action_trace actiontrac3_ 
                inner join
                    link_man linkman4_ 
                        on actiontrac3_.linkMan_id=linkman4_.id 
                inner join
                    customer customer5_ 
                        on linkman4_.customer_id=customer5_.id 
                where
                    (
                        actiontrac3_.isAction is null
                    ) 
                    and actiontrac3_.flag=1 
                    and linkman4_.flag=1 
                    and customer5_.flag=1 
                group by
                    customer5_.id ,
                    actiontrac3_.action_trace_creator
            )
        ) 
        and (
            actiontrac0_.action_trace_creator in (
                -1 , 178 , 237 , 298 , 154 , 210 , 158 , 161 , 171 , 175 , 176 , 186 , 188 , 200 , 202 , 204 , 214 , 252 , 262 , 265 , 270 , 271 , 280 , 287 , 288 , 295 , 297 , 309 , 311 , 159 , 164 , 173 , 212 , 221 , 234 , 251 , 290 , 296 , 139 , 140 , 141 , 142 , 145 , 149 , 174 , 193 , 201 , 206 , 218 , 240 , 275 , 282 , 111 , 115 , 219 , 277 , 278 , 181 , 205 , 247 , 250 , 110 , 121 , 117 , 118 , 131 , 180 , 190 , 197 , 263 , 267 , 286 , 294 , 143 , 167 , 211 , 134 , 147 , 235 , 241 , 245 , 246 , 253 , 260 , 269 , 272 , 289 , 293 , 310 , 112 , 135 , 208 , 242 , 279 , 284 , 285 , 291 , 308 , 144 , 150 , 151 , 152 , 170 , 220 , 238 , 257 , 156 , 165 , 179 , 195 , 215 , 236 , 137 , 183 , 307 , 116 , 217 , 243 , 244 , 258 , 114 , 207 , 276 , 306 , 122 , 124 , 169 , 184 , 232 , 239 , 274 , 281 , 283 , 292 , 127 , 299 , 300 , 301 , 302 , 303 , 304 , 305 , 121
            )
        ) 
        and actiontrac0_.nextActionTime='2012-05-05'

解决方案 »

  1.   

    没有啊,求助大神啊。执行这句SQL要7-8秒。估计是in造成的。
      

  2.   

    将嵌套子查询提取,换一种方式,另外,你为什么都用inner join?
      

  3.   

    你就等着别人给你把完成sql写好。
      

  4.   

    你这种 SQL 也不排整齐一下,给谁看啊?1:用的什么数据库也没有
    2:表结构也没有
    3:各表的数据量也没有
    4:表建了哪些索引也不知道结论,你就慢慢地等神仙来帮你吧!
      

  5.   

    我感觉是你的查询语句写的太过多层嵌套,不是in的问题。
    使用hibernate框架
    每个映射文件中使用一对多的映射关系配置一下就行了并使用inverse=“true”;
    如:在客户<Custom>的java类中加入一个联系人<Contact>这么一个集合属性,
    private set<Contact> contacts =new HashSet<Contact>(); 
    public void setContacts(set<Contact> contacts) {
    this.contacts = contacts;
    }Custom.hbm.xml中
    <!--映射描述文件添加one-to-many-->
     <set name="contacts" inverse="true">
    <!--与客户主键对应的contact_id-->
             <key column="contact_id"></key>
             <one-to-many  class="Contact"/>
      </set>
    至于Contact与客户<Custom>的多对一关系
    在Contact类中加入一个Custom属性
    private Custom custom;
    public void setCustom(Custom custom) {
    this.custom = custom;
    }Contact.hbm.xml映射描述文件配置
     <many-to-one name="custom" column="custom_id" class="Custom"></many-to-one>
    那个联系人与行动记录类似配置就行了
    以上仅供参考,呵呵,希望对楼主有点帮助,至于hql语句还需楼主根据具体应用来编写,没有表结构
      

  6.   

    楼主的sql估计就是hibernate翻译过来的吧
      

  7.   

    楼主你的这个位置 actiontrac0_.id in ()我感觉没有必要用in的 因为你三表关联查,查出的结果本来就只有一条(max());
     actiontrac0_.id in (
      select
      max(actiontrac3_.id)  
      from
      action_trace actiontrac3_  
      inner join
      link_man linkman4_  
      on actiontrac3_.linkMan_id=linkman4_.id  
      inner join
      customer customer5_  
      on linkman4_.customer_id=customer5_.id  
      where
      (
      actiontrac3_.isAction is null
      )  
      and actiontrac3_.flag=1  
      and linkman4_.flag=1  
      and customer5_.flag=1  
      group by
      customer5_.id ,
      actiontrac3_.action_trace_creator
      )