select count(distinct t.test) from general_event t where exists(select distinct a.test from general_event_20130428 a where a.test=t.testand a.eventTag='你好' and eventid='hello') and eventid='world';
找出一张表中eventTag为‘你好’,eventid为'hello'的记录的test,然后根据test筛选eventid='world'的记录总数,不知道这么写对不对,但是好像一直在“正在查询”状态。有没有效率更高的方案?

解决方案 »

  1.   

    create index xxx on general_event(eventid);
    create index yyy on general_event_20130428(test,eventTag,eventid);
    select count(distinct t.test) 
    from general_event t 
    where exists(select 1 from general_event_20130428 a where a.test=t.testand a.eventTag='你好' and eventid='hello') 
    and eventid='world';
      

  2.   

    SELECT COUNT(DISTINCT t.test) FROM general_event t INNER JOIN general_event_20130428 a ON a.test=t.testand WHERE a.eventTag='你好' AND a.eventid='hello' AND t.eventid='world';
      

  3.   

    只是偶尔查一下数据是否正常,所以没有考虑过特意建一个索引,有没有从sql方面优化的方案?
      

  4.   


    select count(distinct t.test) 
    from general_event t 
    where exists(select 1 from general_event_20130428 a where a.test=t.testand a.eventTag='你好' and eventid='hello') 
    and eventid='world';
      

  5.   


    这个语句已经优化过了。好的,我试试,谢谢斑竹sql跑了10几分钟,还是一直“执行查询”,不知道哪儿出问题了