改善时间戳之间的查询结果(时期)

时间:2018-07-04 16:52:22

标签: sqlite query-optimization

我有一张表,其中一个字段在纪元时间记录timestamp,并且我有一些查询需要使用“时间”范围返回结果。

例如

where timestamp >= 1512000000 and timestamp <=1513000000

我为timestamp设置了索引,但是查询结果花费的时间很长。

解释查询计划:

SEARCH TABLE data USING INDEX timestamp_idx (timestamp>? AND timestamp<?)

我已经阅读了一些有关树结构的文章,但是对于如何在现有系统中实现它们却一无所知。

包括完整的EXPLAIN(已编辑)查询。比我想要的还要复杂

sqlite> explain select fielda , timestamp , fieldb from (select fielda , timestamp , fieldb from main.db ) where timestamp >= a and timestamp <= b and fieldb in (x , x , x , x , x , x ) limit 0, -1;
addr  opcode         p1    p2    p3    p4             p5  comment      
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     46    0                    00  Start at 46  
1     Integer        -1    1     0                    00  r[1]=-1; LIMIT counter
2     Integer        20    2     0                    00  r[2]=20      
3     MustBeInt      2     0     0                    00  OFFSET counter
4     OffsetLimit    1     3     2                    00  if r[1]>0 then r[3]=r[1]+max(0,r[2]) else r[3]=(-1); LIMIT+OFFSET
5     OpenRead       1     2     0     18             00  root=2 iDb=0; db
6     OpenRead       2     4     0     k(2,,)         02  root=4 iDb=0; fieldb_idx
7     Once           0     27    0                    00               
8     OpenEphemeral  4     1     0     k(1,B)         00  nColumn=1    
9     Integer        103   5     0                    00  r[5]=103     
10    MakeRecord     5     1     6     D              00  r[6]=mkrec(r[5])
11    IdxInsert      4     6     5     1              00  key=r[6]     
12    Integer        108   5     0                    00  r[5]=108     
13    MakeRecord     5     1     6     D              00  r[6]=mkrec(r[5])
14    IdxInsert      4     6     5     1              00  key=r[6]     
15    Integer        104   5     0                    00  r[5]=104     
16    MakeRecord     5     1     6     D              00  r[6]=mkrec(r[5])
17    IdxInsert      4     6     5     1              00  key=r[6]     
18    Integer        109   5     0                    00  r[5]=109     
19    MakeRecord     5     1     6     D              00  r[6]=mkrec(r[5])
20    IdxInsert      4     6     5     1              00  key=r[6]     
21    Integer        105   5     0                    00  r[5]=105     
22    MakeRecord     5     1     6     D              00  r[6]=mkrec(r[5])
23    IdxInsert      4     6     5     1              00  key=r[6]     
24    Integer        110   5     0                    00  r[5]=110     
25    MakeRecord     5     1     6     D              00  r[6]=mkrec(r[5])
26    IdxInsert      4     6     5     1              00  key=r[6]     
27    Rewind         4     45    0                    00               
28      Column         4     0     4                    00  r[4]=        
29      IsNull         4     44    0                    00  if r[4]==NULL goto 44
30      Affinity       4     1     0     D              00  affinity(r[4])
31      SeekGE         2     44    4     1              00  key=r[4]     
32        IdxGT          2     44    4     1              00  key=r[4]     
33        DeferredSeek   2     0     1                    00  Move 1 to 2.rowid if needed
34        Column         1     0     6                    00  r[6]=db.timestamp
35        Lt             7     43    6     (BINARY)       54  if r[6]<r[7] goto 43
36        Gt             8     43    6     (BINARY)       54  if r[6]>r[8] goto 43
37        IfPos          2     43    1                    00  if r[2]>0 then r[2]-=1, goto 43; OFFSET
38        Column         1     17    9                    00  r[9]=db.fielda
39        Copy           6     10    0                    00  r[10]=r[6]   
40        Column         2     0     11                   00  r[11]=db.fieldb
41        ResultRow      9     3     0                    00  output=r[9..11]
42        DecrJumpZero   1     45    0                    00  if (--r[1])==0 goto 45
43      Next           2     32    0                    00               
44    NextIfOpen     4     28    0                    00               
45    Halt           0     0     0                    00               
46    Transaction    0     0     3     0              01  usesStmtJournal=0
47    Integer        1519862400  7     0                    00  r[7]=1519862400
48    Integer        1522540800  8     0                    00  r[8]=1522540800
49    Goto           0     1     0                    00    

0 个答案:

没有答案