如何在SQLite中使用LEFT JOINS,DISTINCT和ORDER提高慢查询的性能?

时间:2016-05-30 14:19:02

标签: sql performance sqlite

我在SQLite中运行的查询速度慢(2500+ ms)。这是EXPLAIN:

sqlite> .explain
sqlite> explain SELECT DISTINCT vino_maridaje.ID_maridaje, maridaje_texto
   ...>  FROM maridaje
   ...>  LEFT OUTER JOIN vino_maridaje ON vino_maridaje.ID_maridaje = maridaje.ID AND maridaje.ID_categoria = 1
   ...>  LEFT OUTER JOIN maridaje_texto ON maridaje_texto.ID_maridaje = maridaje.ID AND maridaje_texto.ID_idioma = 1
   ...>  LEFT OUTER JOIN vino ON vino.ID = ID_vino
   ...>  WHERE vino.ID_pais = 1 AND vino.ID_tipo = 6 AND activo = 1
   ...>  ORDER BY maridaje_texto;
addr  opcode         p1    p2    p3    p4             p5  comment      
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     83    0                    00               
1     SorterOpen     4     3     0     k(1,B)         00               
2     OpenEphemeral  5     0     0     k(2,B,B)       08               
3     OpenRead       0     718   0     2              00               
4     OpenRead       1     1586  0     3              00               
5     OpenRead       6     2794  0     k(2,nil,nil)   00               
6     OpenRead       2     726   0     4              00               
7     OpenRead       7     3698  0     k(2,nil,nil)   00               
8     OpenRead       3     1412  0     20             00               
9     Rewind         0     67    0                    00               
10      Integer        0     1     0                    00               
11      Rowid          0     2     0                    00               
12      IsNull         2     62    0                    00               
13      SeekGE         6     62    2     1              00               
14        IdxGT          6     62    2     1              00               
15        IdxRowid       6     3     0                    00               
16        Seek           1     3     0                    00               
17        Column         0     1     4     NULL           00               
18        Integer        1     5     0                    00               
19        Ne             5     61    4     (BINARY)       6c               
20        Integer        1     1     0                    00               
21        Integer        0     6     0                    00               
22        Integer        1     7     0                    00               
23        SeekGE         7     57    7     1              00               
24          IdxGT          7     57    7     1              00               
25          IdxRowid       7     8     0                    00               
26          Seek           2     8     0                    00               
27          Column         2     1     4     NULL           00               
28          Rowid          0     5     0                    00               
29          Ne             5     56    4     (BINARY)       6b               
30          Integer        1     6     0                    00               
31          Integer        0     9     0                    00               
32          Column         1     1     10    NULL           00               
33          MustBeInt      10    53    0                    00               
34          NotExists      3     53    10                   00               
35          Integer        1     9     0                    00               
36          Column         3     7     5     NULL           00               
37          Ne             11    53    5     (BINARY)       6c               
38          Column         3     5     4     NULL           00               
39          Ne             12    53    4     (BINARY)       6c               
40          Column         3     19    13    NULL           00               
41          Ne             11    53    13    (BINARY)       6c               
42          Column         6     0     14    NULL           00               
43          Column         2     2     15    NULL           00               
44          Found          5     53    14    2              00               
45          MakeRecord     14    2     16                   00               
46          IdxInsert      5     16    0                    00               
47          MakeRecord     14    2     16                   00               
48          Column         2     2     18    NULL           00               
49          Sequence       4     19    0                    00               
50          Move           16    20    1                    00               
51          MakeRecord     18    3     17                   00               
52          SorterInsert   4     17    0                    00               
53          IfPos          9     56    0                    00               
54          NullRow        3     0     0                    00               
55          Goto           0     35    0                    00               
56        Next           7     24    1                    00               
57        IfPos          6     61    0                    00               
58        NullRow        2     0     0                    00               
59        NullRow        7     0     0                    00               
60        Goto           0     30    0                    00               
61      Next           6     14    1                    00               
62      IfPos          1     66    0                    00               
63      NullRow        1     0     0                    00               
64      NullRow        6     0     0                    00               
65      Goto           0     20    0                    00               
66    Next           0     10    0                    01               
67    Close          0     0     0                    00               
68    Close          1     0     0                    00               
69    Close          6     0     0                    00               
70    Close          2     0     0                    00               
71    Close          7     0     0                    00               
72    Close          3     0     0                    00               
73    OpenPseudo     8     16    2                    00               
74    OpenPseudo     9     21    3                    00               
75    SorterSort     4     82    0                    00               
76      SorterData     4     21    0                    00               
77      Column         9     2     16                   20               
78      Column         8     0     14                   20               
79      Column         8     1     15                   00               
80      ResultRow      14    2     0                    00               
81    SorterNext     4     76    0                    00               
82    Halt           0     0     0                    00               
83    Transaction    0     0     311   0              01               
84    TableLock      0     718   0     maridaje       00               
85    TableLock      0     1586  0     vino_maridaje  00               
86    TableLock      0     726   0     maridaje_texto  00               
87    TableLock      0     1412  0     vino           00               
88    Integer        1     11    0                    00               
89    Integer        6     12    0                    00               
90    Goto           0     1     0                    00 

这是EXPLAIN QUERY PLAN:

0   0   0   SCAN TABLE maridaje
0   1   1   SEARCH TABLE vino_maridaje USING INDEX idx_vino_maridaje_index_ID_maridaje (ID_maridaje=?)
0   2   2   SEARCH TABLE maridaje_texto USING INDEX idx_maridaje_texto_index_ID_idioma (ID_idioma=?)
0   3   3   SEARCH TABLE vino USING INTEGER PRIMARY KEY (rowid=?)
0   0   0   USE TEMP B-TREE FOR DISTINCT
0   0   0   USE TEMP B-TREE FOR ORDER BY

正如您所看到的,表格已经有了适当的索引。那么如何才能更快地进行查询呢?

0 个答案:

没有答案