就是要存储,以后看历史数据,像股票。Table [t_tension]
    Fields: 3
        [id]: INTEGER
        [value]: real
        [c_time]: text
    Foreign Keys: 0
    Indexes: 1
        [cool]
            [c_time] 
    Triggers: 0
    Unique constraints: 0
    Check constraints: 0这是我的表结构,索引为cool,c_time字段,可下面这句查询还是很慢,似乎索引没有起来到作用
select id,value,c_time from t_tension where datetime(c_time)>='2015-10-10 02:02:02' and datetime(c_time)<= '2015-10-10 02:10:00' limit 100;

解决方案 »

  1.   

    你查询条件是c_time 索引是cool 和 c_time的联合 ,因为索引的前缀才能有效果,所以这个索引只能对cool查询或者两个条件一起的时候才会生效 你可以将这个索引的字段换一下顺序 或者再在c_time上单独建立索引
      

  2.   


    那个cool 是索引的名字,不是字段,c_time这个才是字段
      

  3.   


    表结构:
    Table [t_tension]
        Fields: 3
            [id]: INTEGER
            [value]: real
            [c_time]: text
        Foreign Keys: 0
        Indexes: 1
            [cool]
                [c_time] 
        Triggers: 0
        Unique constraints: 0
        Check constraints: 0
    表的数据:
    id value c_time
    1 41      2014-12-04 08:59:30
    1 467    2014-12-04 08:59:31
    1 334    2014-12-04 08:59:32
    1 500    2014-12-04 08:59:33
    1 169   2014-12-04 08:59:34
    1 724   2014-12-04 08:59:35
    1 478 2014-12-04 08:59:36
    1 358 2014-12-04 08:59:37
    1 962 2014-12-04 08:59:38
    1 464 2014-12-04 08:59:39
    1 705 2014-12-04 08:59:40
    1 145 2014-12-04 08:59:41
    1 281 2014-12-04 08:59:42
    1 827 2014-12-04 08:59:43
    1 961 2014-12-04 08:59:44
    1 491 2014-12-04 08:59:45
    1 995 2014-12-04 08:59:46
    .........查询代码:
    select id,value,c_time from t_tension where datetime(c_time)>='2015-10-10 02:02:02' and datetime(c_time)<= '2015-10-10 02:10:00' limit 100;
    我反复测试发现,感觉sqlite对text排序太慢,虽然加了索引。结果把c_time 转成real类型,就是把时间转double存,再搜索,加索引与不加索引的差距就出来了。我不知道mysql数据库怎么样,若查询快的话我在考虑要不要换数据库