sqlite不会使用索引进行查询

时间:2013-03-21 15:58:34

标签: sqlite indexing

我在我的数据库中使用此查询

SELECT
      DISTINCT wb_expr_pattern2gene.gene
    FROM
      wb_expr_pattern2gene
    , wb_expr_pattern2anatomy_term
    , wb_anatomy_term2ancestor
    , wb_anatomy_term2cell
    WHERE
        wb_anatomy_term2cell.cell = 'P0'
    AND (
           wb_anatomy_term2cell.anatomy_term = wb_expr_pattern2anatomy_term.anatomy_term
        OR (
                wb_anatomy_term2cell.anatomy_term = wb_anatomy_term2ancestor.anatomy_term
            AND wb_anatomy_term2ancestor.ancestor_term = wb_expr_pattern2anatomy_term.anatomy_term
           )
        )
    AND wb_expr_pattern2anatomy_term.expr_pattern = wb_expr_pattern2gene.expr_pattern
;

需要很长时间。 原因是sqlite不会在wb_anatomy_term2ancestor

上使用索引
0|0|3|SEARCH TABLE wb_anatomy_term2cell USING INDEX wb_anatomy_term2cell__cell (cell=?) (~10 rows)
0|1|2|SCAN TABLE wb_anatomy_term2ancestor (~1000000 rows)
0|2|1|SEARCH TABLE wb_expr_pattern2anatomy_term USING INDEX wb_expr_pattern2anatomy_term__anatomy_term (anatomy_term=?) (~10 rows)
0|2|1|SEARCH TABLE wb_expr_pattern2anatomy_term USING INDEX wb_expr_pattern2anatomy_term__anatomy_term (anatomy_term=?) (~10 rows)
0|3|0|SEARCH TABLE wb_expr_pattern2gene USING INDEX wb_expr_pattern2gene__expr_pattern (expr_pattern=?) (~10 rows)
0|0|0|USE TEMP B-TREE FOR DISTINCT

虽然它们存在

CREATE INDEX wb_anatomy_term2ancestor__anatomy_term__ancestor_term ON wb_anatomy_term2ancestor(anatomy_term, ancestor_term);
CREATE INDEX wb_anatomy_term2ancestor__anatomy_term ON wb_anatomy_term2ancestor(anatomy_term);
CREATE INDEX wb_anatomy_term2ancestor__ancestor_term ON wb_anatomy_term2ancestor(ancestor_term);

sqlite有这个行为的充分理由吗?

1 个答案:

答案 0 :(得分:0)

此查询速度很快(但为什么?)

EXPLAIN QUERY PLAN SELECT
      wb_expr_pattern2gene.gene
    FROM
        wb_expr_pattern2gene
      , wb_expr_pattern2anatomy_term
    WHERE
    ((   
        wb_expr_pattern2anatomy_term.anatomy_term IN ( SELECT
          wb_anatomy_term2cell.anatomy_term
        FROM
          wb_anatomy_term2cell
        WHERE 
                wb_anatomy_term2cell.cell = 'P0'
        )
    )
    OR wb_expr_pattern2anatomy_term.anatomy_term IN ( SELECT
          wb_anatomy_term2ancestor.ancestor_term
        FROM
            wb_anatomy_term2ancestor
          , wb_anatomy_term2cell
        WHERE 
                wb_anatomy_term2cell.cell = 'P0'
            AND wb_anatomy_term2ancestor.anatomy_term = wb_anatomy_term2cell.anatomy_term
            AND wb_anatomy_term2cell.anatomy_term = wb_anatomy_term2ancestor.anatomy_term
       )
    )
    AND wb_expr_pattern2anatomy_term.expr_pattern = wb_expr_pattern2gene.expr_pattern
;

0|0|0|EXECUTE LIST SUBQUERY 1
1|0|0|SEARCH TABLE wb_anatomy_term2cell USING INDEX wb_anatomy_term2cell__cell (cell=?) (~10 rows)
0|0|1|SEARCH TABLE wb_expr_pattern2anatomy_term USING INDEX wb_expr_pattern2anatomy_term__anatomy_term (anatomy_term=?) (~250 rows)
0|0|0|EXECUTE LIST SUBQUERY 2
2|0|1|SEARCH TABLE wb_anatomy_term2cell USING INDEX wb_anatomy_term2cell__cell (cell=?) (~10 rows)
2|1|0|SEARCH TABLE wb_anatomy_term2ancestor USING COVERING INDEX wb_anatomy_term2ancestor__anatomy_term__ancestor_term (anatomy_term=?) (~2 rows)
0|0|1|SEARCH TABLE wb_expr_pattern2anatomy_term USING INDEX wb_expr_pattern2anatomy_term__anatomy_term (anatomy_term=?) (~250 rows)
0|0|0|EXECUTE LIST SUBQUERY 3
3|0|0|SEARCH TABLE wb_anatomy_term2cell USING INDEX wb_anatomy_term2cell__cell (cell=?) (~10 rows)
0|0|0|EXECUTE LIST SUBQUERY 4
4|0|1|SEARCH TABLE wb_anatomy_term2cell USING INDEX wb_anatomy_term2cell__cell (cell=?) (~10 rows)
4|1|0|SEARCH TABLE wb_anatomy_term2ancestor USING COVERING INDEX wb_anatomy_term2ancestor__anatomy_term__ancestor_term (anatomy_term=?) (~2 rows)
0|1|0|SEARCH TABLE wb_expr_pattern2gene USING INDEX wb_expr_pattern2gene__expr_pattern (expr_pattern=?) (~10 rows)