附加组使Postgres查询速度极慢

时间:2016-04-18 13:34:26

标签: postgresql

我正在尝试使用包含我需要的数据的两个表进行查询。下面有3个查询。第一个是我需要的第一个表的查询。第二个是我需要的第二个表的查询。第三个是我需要的两个表的查询。

以下是我的查询:

/*Query with including the first table I need*/
set schema 'exampledata';
    select 
        count(signature),
        signature,
        example1
    from testtable
    group by  testtable.signature, testtable.example1
    order by count(signature) desc

/*Row count: 1852 rows
Query time: 2.1 secs*/


/*Query with including the second table I need*/
set schema 'exampledata';
    select 
        count(signature),
        signature,
        example2
    from testtable
    group by  testtable.signature, testtable.example2
    order by count(signature) desc

/*Row count: 1794 rows
Query time: 2.1 secs*/


/*Query with including the first and second tables I need*/
set schema 'exampledata';
    select 
        count(signature),
        signature,
        example2,
        example1
    from testtable
    group by  testtable.signature, testtable.example2, testtable.example1
    order by count(signature) desc

/*Row count: 1872 rows
Query time: 23.5 secs*/

为什么一旦我向查询添加一个表,处理所需的时间增加了十倍?是因为表没有共享相同数量的行吗?

以下是解释,尊重所显示的每个查询:

"Sort  (cost=151128.71..151149.41 rows=8280 width=79) (actual time=1563.842..1563.869 rows=1852 loops=1)"
"  Output: (count(signature)), signature, example1"
"  Sort Key: (count(testtable.signature)) DESC"
"  Sort Method: quicksort  Memory: 316kB"
"  ->  HashAggregate  (cost=150507.08..150589.88 rows=8280 width=79) (actual time=1563.141..1563.476 rows=1852 loops=1)"
"        Output: count(signature), signature, example1"
"        Group Key: testtable.signature, testtable.example1"
"        ->  Seq Scan on data.testtable  (cost=0.00..129087.90 rows=2855890 width=79) (actual time=0.005..618.993 rows=2807146 loops=1)"
"              Output: example14, example3, example4, example5, example1, example2, example6, example7, example8, example9, example10, example11, example12, example13"
"Planning time: 0.063 ms"
"Execution time: 1564.021 ms"



"Sort  (cost=152197.99..152249.74 rows=20700 width=85) (actual time=1584.287..1584.312 rows=1794 loops=1)"
"  Output: (count(signature)), signature, example2"
"  Sort Key: (count(testtable.signature)) DESC"
"  Sort Method: quicksort  Memory: 313kB"
"  ->  HashAggregate  (cost=150507.08..150714.08 rows=20700 width=85) (actual time=1583.538..1583.916 rows=1794 loops=1)"
"        Output: count(signature), signature, example2"
"        Group Key: testtable.signature, testtable.example2"
"        ->  Seq Scan on data.testtable  (cost=0.00..129087.90 rows=2855890 width=85) (actual time=0.005..628.403 rows=2807146 loops=1)"
"              Output: example14, example3, example4, example5, example1, example2, example6, example7, example8, example9, example10, example11, example12, example13"
"Planning time: 0.059 ms"
"Execution time: 1584.542 ms"



"Sort  (cost=794824.44..795341.94 rows=207000 width=92) (actual time=22771.282..22771.307 rows=1872 loops=1)"
"  Output: (count(signature)), signature, example2, example1"
"  Sort Key: (count(testtable.signature)) DESC"
"  Sort Method: quicksort  Memory: 350kB"
"  ->  GroupAggregate  (cost=728162.97..765931.60 rows=207000 width=92) (actual time=18818.703..22770.504 rows=1872 loops=1)"
"        Output: count(signature), signature, example2, example1"
"        Group Key: testtable.signature, testtable.example2, testtable.example1"
"        ->  Sort  (cost=728162.97..735302.70 rows=2855890 width=92) (actual time=18803.887..22232.128 rows=2807146 loops=1)"
"              Output: signature, example2, example1"
"              Sort Key: testtable.signature, testtable.example2, testtable.example1"
"              Sort Method: external merge  Disk: 286688kB"
"              ->  Seq Scan on data.testtable  (cost=0.00..129087.90 rows=2855890 width=92) (actual time=0.008..960.797 rows=2807146 loops=1)"
"                    Output: signature, example2, example1"
"Planning time: 0.084 ms"
"Execution time: 23085.289 ms"

0 个答案:

没有答案