2

我正在尝试在 Postgres 中使用以下 SQL 添加一个简单的索引,但该命令一直超时:

CREATE INDEX playlist_tracklinks_playlist_enid ON playlist_tracklinks (playlist_enid);

表定义如下:

=> \d playlist_tracklinks
         Table "public.playlist_tracklinks"
     Column     |     Type      |     Modifiers
----------------+---------------+--------------------
 playlist_enid  | numeric(20,0) | not null default 0
 tracklink_enid | numeric(20,0) | not null default 0
 position       | integer       | not null default 1

表中有大约 22 亿行,它失败并出现以下错误:

ERROR:  canceling statement due to user request

我尝试使用以下方法增加查询超时时间:

SET statement_timeout TO 360000000;

然而,它仍然达到了这个门槛。我已经尝试过使用和不使用CONCURRENTLY,并且有点不知所措。任何建议将不胜感激。

4

3 回答 3

1

算术numerics很慢。这包括构建和使用索引所需的比较。我建议您将enid类型更改为char(20)或仅varchar在您不对它们进行任何算术(比较除外)时,或者bigint如果您这样做的话。对于最大可能的 20 位数字,Bigint 还不够——我不知道这些 id 携带什么样的信息,如果它们真的有那么大。

于 2013-09-18T16:59:32.893 回答
1

这是 Heroku 杀死连接(服务器用完了临时空间)。联系 Heroku 支持是最终的解决方案......

于 2013-09-25T18:13:38.080 回答
1

您可以尝试索引表的一部分,例如使用该WHERE语句的前 10k 行。然后你可能会看到它是否有效以及需要多长时间。WHERE在这里使用的参考CREATE INDEXhttp ://www.postgresql.org/docs/9.1/static/sql-createindex.html

您的列是否可能包含非唯一数字?这可能会导致问题(在这种情况下,我不确定索引是否需要列上的唯一值)。

于 2013-09-17T01:46:48.930 回答