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 に答える