在PostgreSQL中内部连接表时缓慢计数

时间:2016-03-09 15:07:58

标签: sql postgresql count database-performance

我正在使用postgres 9.4。我已经VACUUMANALYZE了。但inner join上的查询仍然很慢。

对于最小的例子,我有3个表:numbersalebase_numbernumberstorethroughnumber_idnumbersale中的numberstorethrough只是FK(numbersale.number_id指向base_numbernumberstorethrough.number_id指向numbersale,是的,它是可怕的命名):

                                                           Table "public.numbersale"
        Column        |           Type           |                           Modifiers                           | Storage  | Stats target | Description 
----------------------+--------------------------+---------------------------------------------------------------+----------+--------------+-------------
 id                   | integer                  | not null default nextval('numbersale_id_seq'::regclass)       | plain    |              | 
 number_id            | integer                  | not null                                                      | plain    |              | 


                                                        Table "public.base_number"
   Column    |           Type           |                        Modifiers                         | Storage  | Stats target | Description 
-------------+--------------------------+----------------------------------------------------------+----------+--------------+-------------
 id          | integer                  | not null default nextval('base_number_id_seq'::regclass) | plain    |              | 


                                                        Table "public.numberstorethrough"
    Column    |           Type           |                               Modifiers                               | Storage | Stats target | Description 
--------------+--------------------------+-----------------------------------------------------------------------+---------+--------------+-------------
 id           | integer                  | not null default nextval('numberstorethrough_id_seq'::regclass)       | plain   |              | 
 number_id    | integer                  | not null                                                              | plain   |              | 

其中包含250k到595k条目:

$ SELECT COUNT(*) FROM numbersale;
 count  
--------
 258552
(1 row)

Time: 17,845 ms

$ SELECT COUNT(*) FROM base_number;
 count  
--------
 332484
(1 row)

Time: 16,273 ms

$ SELECT COUNT(*) FROM numberstorethrough;
 count  
--------
 595812
(1 row)

Time: 56,710 ms

表格有相应的索引:

$ select * from pg_indexes where tablename = 'numbersale';
 schemaname |    tablename     |                 indexname                  | tablespace |                                                              indexdef                                                              
------------+------------------+--------------------------------------------+------------+------------------------------------------------------------------------------------------------------------------------------------
...
public      | numbersale       | numbersale_number_id_key             |            | CREATE UNIQUE INDEX numbersale_number_id_key ON numbersale USING btree (number_id)


$ select * from pg_indexes where tablename = 'numberstorethrough';
 schemaname |        tablename         |               indexname               | tablespace |                                                               indexdef                                                               
------------+--------------------------+---------------------------------------+------------+--------------------------------------------------------------------------------------------------------------------------------------
 public     | numberstorethrough       | numberstorethrough_number_id    |            | CREATE INDEX numberstorethrough_number_id ON numberstorethrough USING btree (number_id)

我的问题是以下查询:

SELECT COUNT(*) FROM "numbersale"
INNER JOIN "base_number"
   ON ( "numbersale"."number_id" = "base_number"."id" )
INNER JOIN "numberstorethrough"
  ON ( "numbersale"."id" = "numberstorethrough"."number_id" );
 count  
 --------
 595812
(1 row)

Time: 541,523 ms

解释该查询:

Aggregate  (cost=62564.67..62564.68 rows=1 width=0)
  ->  Hash Join  (cost=34443.31..61075.14 rows=595812 width=0)
        Hash Cond: (numberstorethrough.number_id = numbersale.id)
        ->  Seq Scan on numberstorethrough  (cost=0.00..10539.12 rows=595812 width=4)
        ->  Hash  (cost=30201.41..30201.41 rows=258552 width=4)
              ->  Hash Join  (cost=14411.42..30201.41 rows=258552 width=4)
                    Hash Cond: (base_number.id = numbersale.number_id)
                    ->  Seq Scan on base_number  (cost=0.00..7102.84 rows=332484 width=4)
                    ->  Hash  (cost=10169.52..10169.52 rows=258552 width=8)
                          ->  Seq Scan on numbersale  (cost=0.00..10169.52 rows=258552 width=8)

是否正常,这种带有两个内连接的基本查询需要超过半秒(有时需要长达700毫秒)?行数甚至不是数百万,它只有300-600k。

我简化了我的查询,实际上它更大,需要1秒以上,但加入问题是我的主要瓶颈。

2 个答案:

答案 0 :(得分:0)

一种可能性是连接产生的中间结果非常大,但是后来被第二次连接过滤掉了。这仍然无法解释为什么没有使用索引,但也许这可能有更好的性能:

SELECT COUNT(*)
FROM "base_number" bn
WHERE EXISTS (SELECT 1 FROM "numbersale" ns WHERE ns."number_id" = bn."id") AND
      EXISTS (SELECT 1 FROM "numberstorethrough" nst WHERE bn."id" = nst."number_id");

您已拥有此(及原始)查询的正确索引:numbersale(number_id)base_number(id)numberstorethrough(number_id)

答案 1 :(得分:0)

我对查询计划的最佳解释是不使用索引,因为查询中没有任何内容限制应该获取的行。正在读取每个表中的每一行。那么在这种情况下使用索引会有什么好处呢?数据库必须花时间阅读索引,然后仍然必须阅读“真实”数据。没有收获。如果有一个WHERE子句限制查询撤回的内容,或者如果连接条件确实限制了检索的行数,我希望索引可能起作用。索引并不神奇 - 它们的使用并不会自动“更好”,而且它们缺少查询计划并不会自动“更糟糕”。与许多事情一样,它取决于:-) - 在这种情况下,从查询优化器的角度来看,似乎使用一个或多个索引是一个净损失。还要记住COUNT并不神奇 - 它是一个只计算结果集中行数的函数。它并不神奇地“知道”结果集中预计会有多少行 - 数据库必须生成结果集,然后COUNT只计算有多少行。