-1

也许这是一个基本问题,但无法回答,希望您的帮助:)

我在 MySQL 中有下表:

create table anotation
    ( 
        chromosome enum
            (
                'Chr1',
                'Chr2',
                'Chr3',
                'Chr4',
                'Chr5',
                'ChrC',
                'ChrM'
            ), 
        version varchar(10),
        type enum
            (
                'CDS',
                'chromosome',
                'exon',
                'five_prime_UTR',
                'gene',
                'mRNA',
                'mRNA_TE_gene',
                'miRNA',
                'ncRNA',
                'protein',
                'pseudogene',
                'pseudogenic_exon',
                'pseudogenic_transcript',
                'rRNA',
                'snRNA',
                'snoRNA',
                'tRNA',
                'three_prime_UTR',
                'transposable_element_gene'
            ), 
        strand enum
            (
                 '+',
                 '-'
            ), 
        phase tinyint, 
        atrributes text
    );`

它有大约 600,000 个值,我正在执行以下查询:

select distinct
            anot_1.chromosome,
            anot_1.start,
            anot_1.end,
            anot_1.atrributes 
    from 
            anotation anot_1,
            anotation anot_2 
    where
            anot_1.type='CDS'
        and
            anot_2.type='protein'
        and 
            anot_1.chromosome!='ChrM'
        and
            anot_1.chromosome!='ChrC'
        and
            anot_1.chromosome=anot_2.chromosome
        and 
        (
            (
                anot_2.start=anot_1.start
            and
                anot_1.end!=anot_2.end
            and
                anot_2.strand='+'
            ) 
        or 
            (
                anot_2.start!=anot_1.start
            and
                anot_1.end=anot_2.end
            and
                anot_2.strand='-'
            )
        );

并且实际上需要很长时间才能完成,但是当我进行查询时(相同的查询,但我从 OR 中删除了其中一个条件),它几乎立即运行:

select distinct
            anot_1.chromosome,
            anot_1.start,
            anot_1.end,
            anot_1.atrributes
    from
            anotation anot_1, 
            anotation anot_2
    where
            anot_1.type='CDS'
        and 
            anot_2.type='protein'
        and 
            anot_1.chromosome!='ChrM'
        and
            anot_1.chromosome!='ChrC'
        and
            anot_1.chromosome=anot_2.chromosome
        and
            anot_2.start=anot_1.start
        and
            anot_1.end!=anot_2.end
        and 
            anot_2.strand='+';`

任何人都知道发生了什么,如果是这样,我该如何解决?谢谢!!!

4

2 回答 2

0

这不是一个解决方案(我同意上面关于索引的评论),但我已经改变了你的 SQL 以使用一对左外连接,而不是你当前的 SQL,它在 JOIN 中有一个 OR。

虽然我不希望这在性能上有很大不同,但它可能会帮助您和其他人了解查询在做什么:-

SELECT distinct anot_1.chromosome, anot_1.start, anot_1.end, anot_1.atrributes 
FROM anotation anot_1,
LEFT OUTER JOIN anotation anot_2 ON anot_1.chromosome = anot_2.chromosome AND anot_1.start = anot_2.start AND anot_1.end != anot_2.end AND anot_2.strAND = '+' AND anot_2.type='protein'
LEFT OUTER JOIN anotation anot_3 ON anot_1.chromosome = anot_3.chromosome AND anot_1.end = anot_3.end AND anot_1.start != anot_3.start AND anot_3.strAND = '+' AND anot_3.type='protein'
WHERE anot_1.type = 'CDS'  
AND anot_1.chromosome != 'ChrM' 
AND anot_1.chromosome != 'ChrC' 
AND (anot_2.chromosome IS NOT NULL
OR anot_3.chromosome IS NOT NULL)
于 2012-11-22T11:28:54.040 回答
0

我会首先清理您的查询,将它们分开并进行联合

select
            CDS.chromosome,
            CDS.start,
            CDS.end,
            CDS.atrributes 
    from
            (
            select
                        a.chromosome,
                        a.start,
                        a.end,
                        a.attribures,
                from
                        anotation a,
                where
                        a.type='CDS'
                    and
                        not a.chromosome IN ('ChrM', 'ChrC')
            ) CDS
        join
            (
            select
                        a.strand,
                from
                        anotation a,
                where
                        a.type='protien'
            ) Protien
                on 
                        CDS.chromosome = Protien.chromosome
                   and
                        CDS.start = Protien.start
                   and
                        CDS.end != Protien.end 
where
            Protien.strand = '+'
union
    select
            CDS.chromosome,
            CDS.start,
            CDS.end,
            CDS.atrributes 
    from
            (
            select
                        a.chromosome,
                        a.start,
                        a.end,
                        a.attribures,
                from
                        anotation a,
                where
                        a.type='CDS'
                    and
                        not a.chromosome IN ('ChrM', 'ChrC')
            ) CDS
        join
            (
            select
                        a.strand,
                from
                        anotation a,
                where
                        a.type='protien'
            ) Protien
                on 
                        CDS.chromosome = Protien.chromosome
                   and
                        CDS.start != Protien.start
                   and
                        CDS.end = Protien.end 
where
            Protien.strand = '-'
于 2012-11-22T11:40:05.517 回答