select from_unixtime(logintime) as logtime from t_login where accountid=?id order by from_unixtime(logintime) desc limit 0,1
谁能把这句话优化一下?
数据量太大运行时间太长
有4000多条数据检查。。谁知道用什么办法比较好?

解决方案 »

  1.   

    select from_unixtime(logintime) as logtime from t_login 
    where accountid=?id order by from_unixtime(logintime) desc limit 0,1 在accountid、from_unixtime上建立索引
    select from_unixtime(logintime) as logtime from 
    select * from t_login where accountid=?id ) a
    order by from_unixtime(logintime) desc limit 0,1 
      

  2.   

    在logintime上建立索引,然后改语句为 没有必要以 from_unixtime(logintime)  排序,直接以logintime减少运算以及有效利用索引。
     
    select from_unixtime(logintime) as logtime 
    from t_login 
    where accountid=?id 
    order by logintime desc 
    limit 0,1
      

  3.   

    谢谢。。我怎么添加索引老是报错啊,
    帮忙看看哪里错了?
    谢谢啦。。alert table t_login add index logtime_index(logintime)
      

  4.   

    不需要alter table直接 create index idx_t_login_logintime on t_login (logintime);就行了
      

  5.   

    是alter (修改)  不是 alert(报警)alter table t_login add index (logintime);
      

  6.   

    http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#alter-table
    13.1.2. ALTER TABLE语法http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#create-index
    13.1.4. CREATE INDEX语法