1

我在我的数据库上使用这个查询

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 有这种行为的充分理由吗?

4

1 回答 1

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)
于 2013-03-22T07:33:16.773 回答