现每天8点需要查询 某个用户访问网站最近的时间,如果该用户今天没有访问,就查询出该用户最近访问网站的时间,
由于存放数据的table 相当大,已有几千万笔数据了,请教各位有没有性能较好的SQL 实现
上述功能:例:table A  存放数据格式如下:
今天查询的时间为  2010-05-27 8:00:00,想查询jack 访问网站最近的时间,SQL怎么写比较好id        name         访问时间                   1         jack        2010-02-15 7:15:00        2         john        2010-02-15 7:15:00        3         mike        2010-02-15 7:15:00        1         jack        2010-01-10 5:04:00 
         
...
==============================================
想得到数据为:
1         jack        2010-02-15 7:15:00  

解决方案 »

  1.   


    select id,name,访问时间
    from (select id,name,访问时间,
    row_number() over(partition by id,name order by 访问时间 desc) rn
    from tb) a
    where rn=1 and id=1select * from tb a 
    where exists(select 1 from tb where a.id=id and a.name=name  and a.访问时间>访问时间)
    and id=1
    --去掉id=1就是所有的用户最后的访问时间
      

  2.   

    SELECT NAME,MAX(DATE) VISIT_DATE
    FROM A
    WHERE
    NAME='jack'
    GROUP BY NAME;
      

  3.   

    table: 8千万用户: 多少个?优化在于怎么样建索引
    ----------------------------------------为什么,不在用户表中加一个字段,记下最后访问时间?
      

  4.   

    支持第二条sql,在id和name建立索引速度更快
      

  5.   

    第二条好像不太对 应该改一下吧
    select * from tb a 
    where not exists(select 1 from tb where a.id=id and a.name=name  and a.访问时间 < 访问时间)
      

  6.   

    SQL> select * from tb;
     
    NAME                 TIME
    -------------------- ------------------------------
    a                    2010-5-28 21:37:36
    a                    2010-5-18 21:37:39
    b                    2010-5-25 21:37:44
    b                    2010-5-23 21:37:47
    c                    2010-5-28 21:37:50
     
    SQL> 
    SQL> select * from tb a
      2  where not exists(select 1 from tb where name = a.name and time > a.time)
      3  ;
     
    NAME                 TIME
    -------------------- ------------------------------
    b                    2010-5-25 21:37:44
    c                    2010-5-28 21:37:50
    a                    2010-5-28 21:37:36
     
    SQL> 
    SQL> select * from tb a
      2  where exists(select 1 from tb where name = a.name and a.time > time)
      3  ;
     
    NAME                 TIME
    -------------------- ------------------------------
    a                    2010-5-28 21:37:36
    b                    2010-5-25 21:37:44
      

  7.   

    SELECT MAX(访问时间) FROM TB WHERE TB.ID = 1;
    访问时间和id加索引~
      

  8.   

    select * from (select * from A desc 访问时间) where rownum = 1 and name = 'jack';
      

  9.   

    支持,在标志字段上加个位图索引,并在ID或者NAME列上建索引