相关疑难解决方法(0)

如何根据行数执行条件插入?

我正在使用 Postgres 9.3,我需要根据表中已有的特定行数来防止插入到表中。这是表:

                                      Table "public.team_joins"
     Column      |           Type           |                            Modifiers                             
-----------------+--------------------------+---------------------------------------------------------
 id              | integer                  | not null default nextval('team_joins_id_seq'::regclass)
 team_id         | integer                  | not null
Indexes:
    "team_joins_pkey" PRIMARY KEY, btree (id)
    "team_joins_team_id" btree (team_id)
Foreign-key constraints:
    "team_id_refs_teams_id" FOREIGN KEY (team_id) REFERENCES teams(id) DEFERRABLE INITIALLY DEFERRED
Run Code Online (Sandbox Code Playgroud)

因此,例如,如果一个 id 为 3 的团队只允许 20 名玩家,并且SELECT COUNT(*) FROM team_joins WHERE team_id = 3等于 20,那么没有玩家应该能够加入团队 3。处理这种情况并避免并发问题的最佳方法是什么?我应该使用SERIALIZABLE事务来插入,还是可以WHERE在插入语句中使用这样的子句?

INSERT INTO team_joins (team_id)
VALUES (3)
WHERE (
  SELECT COUNT(*) FROM team_joins WHERE …
Run Code Online (Sandbox Code Playgroud)

postgresql database-design insert concurrency

9
推荐指数
1
解决办法
2万
查看次数

未使用数据类型 citext 的列上的索引

在 PostgreSQL 9.4 中,具有以下架构:

CREATE TABLE people (
    id INTEGER PRIMARY KEY,
    name TEXT,
    junk CHAR(1000)
);

INSERT INTO people(id, name)
 SELECT generate_series(1,100000), md5(random()::text);

CREATE INDEX ON people (name text_pattern_ops);
Run Code Online (Sandbox Code Playgroud)

如果我按名称搜索,则使用索引:

test=# explain analyze select id, name from people where name like 'a%';
                                                           QUERY PLAN                                                           
--------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on people  (cost=248.59..1160.92 rows=6061 width=37) (actual time=2.412..8.340 rows=6271 loops=1)
   Filter: (name ~~ 'a%'::text)
   Heap Blocks: exact=834
   ->  Bitmap Index Scan on people_name_idx  (cost=0.00..247.08 rows=6266 width=0) (actual time=2.123..2.123 rows=6271 loops=1)
         Index Cond: …
Run Code Online (Sandbox Code Playgroud)

postgresql index pattern-matching case-sensitive

3
推荐指数
1
解决办法
3868
查看次数

有效地从 am:n 表中返回两个聚合数组

我有一个表,用于表上的多对多关系users来表示用户之间的跟随关系:

CREATE TABLE users (
    id text PRIMARY KEY,
    username text NOT NULL
);

CREATE TABLE followers (
    userid text,
    followid text,
    PRIMARY KEY (userid, followid),
    CONSTRAINT followers_userid_fk   FOREIGN KEY (userid)   REFERENCES users (id),
    CONSTRAINT followers_followid_fk FOREIGN KEY (followid) REFERENCES users (id)
);

CREATE INDEX followers_followid_idx ON followers (followid);
Run Code Online (Sandbox Code Playgroud)

当我想使用与用户相关的数据创建 JSON 响应时,我有两种情况:

  • 通过 id 请求单个用户,
  • 通过 id 列表请求用户对象数组

用户数据对象应包含两个用户 ID 数组,一个是他们关注的用户,另一个是关注他们的用户。为了创建这两个字段,我使用了以下SELECT语句。

DECLARE follows RECORD;
SELECT  array (select followid FROM followers where userid = Puserid) AS following, …
Run Code Online (Sandbox Code Playgroud)

postgresql performance view many-to-many postgresql-10

2
推荐指数
1
解决办法
1120
查看次数