合并复杂表

时间:2016-03-25 11:56:07

标签: sql database postgresql merge

我试图合并表格,其中行对应于多个:1与"真实"的东西。

我正在编写一个二十一点模拟器,它将游戏历史存储在一个数据库中,并且每次运行都会生成一组新表。这些表格更像是模板,因为每个游戏都有自己的3个可变表格(玩家,手和火柴)。这是布局,其中suff是用户指定的后缀,用于当前运行:

 - cards
     - id INTEGER PRIMARY KEY
     - cardValue INTEGER NOT NULL 
     - suit INTEGER NOT NULL
 - players_suff
     - whichPlayer INTEGER PRIMARY KEY
     - aiType TEXT NOT NULL
 - hands_suff
     - id BIGSERIAL PRIMARY KEY
     - whichPlayer INTEGER REFERENCES players_suff(whichPlayer) *
     - whichHand BIGINT NOT NULL
     - thisCard INTEGER REFERENCES cards(id)
 - matches_suff
     - id BIGSERIAL PRIMARY KEY
     - whichGame INTEGER NOT NULL
     - dealersHand BIGINT NOT NULL
     - whichPlayer INTEGER REFERENCES players_suff(whichPlayer)
     - thisPlayersHand BIGINT NOT NULL **
     - playerResult INTEGER NOT NULL --AKA who won

只创建了一张牌表,因为它的值是常量。

因此,在运行模拟器两次后,您可能会:

hands_firstrun
players_firstrun
matches_firstrun
hands_secondrun
players_secondrun
matches_secondrun

如果您对这两个运行使用相同的AI参数(即players_firstrun和players_secondrun完全相同),我希望能够组合这些表。问题在于我插入手的方式使得这非常麻烦:哪些手不能成为一个大手笔,因为hands_suff行与"实际手的关系"很多:1。 matches_suff以相同的方式处理,因为二十一点游戏"实际上由一组游戏组成:每个玩家对经销商的一对。所以对于3名玩家来说,每轮实际上有3行。

目前我选择表中最大的哪个手柄,向其中添加1,然后为一只手插入所有行。我担心这个"查询和插入"如果我合并两张可能任意巨大的牌桌,那将会非常缓慢。

当我合并表时,我觉得我应该能够(完全在SQL中)查询最大值,其中Hand和哪个游戏然后使用它们组合表,为每个唯一的哪个表和其他游戏增加它们正在合并的表格。

(我看到了this question,但它并没有在2个不同的地方使用生成的ID处理)。我使用Postgres,如果答案是特定的,那就没关系。

*遗憾的是postgres不允许参数化表名,因此必须通过手动字符串替换来完成。不是世界末日,因为该程序不面向网络,除了我之外没有人可能会费心,但SQL注入漏洞并不能让我满意。

** matches_suff(whichPlayersHand)最初将引用hands_suff(whichHand)但foreign keys must reference unique values。由于手是由多行组成的,每一行"持有"哪个手并不是唯一的。一张牌。要查询指针,请选择具有相同值的所有行。我不会想到一种更优雅的方式来做到这一点而不诉诸数组。

编辑:

这就是我现在所拥有的:

thomas=# \dt
            List of relations
 Schema |      Name      | Type  | Owner
--------+----------------+-------+--------
 public | cards          | table | thomas
 public | hands_first    | table | thomas
 public | hands_second   | table | thomas
 public | matches_first  | table | thomas
 public | matches_second | table | thomas
 public | players_first  | table | thomas
 public | players_second | table | thomas
(7 rows)

thomas=# SELECT * FROM hands_first
thomas-# \g
 id | whichplayer | whichhand | thiscard
----+-------------+-----------+----------
  1 |           0 |         0 |        6
  2 |           0 |         0 |       63
  3 |           0 |         0 |       41
  4 |           1 |         1 |       76
  5 |           1 |         1 |       23
  6 |           0 |         2 |       51
  7 |           0 |         2 |       29
  8 |           0 |         2 |        2
  9 |           0 |         2 |       92
 10 |           0 |         2 |        6
 11 |           1 |         3 |      101
 12 |           1 |         3 |        8
(12 rows)

thomas=# SELECT * FROM hands_second
thomas-# \g
 id | whichplayer | whichhand | thiscard
----+-------------+-----------+----------
  1 |           0 |         0 |       78
  2 |           0 |         0 |       38
  3 |           1 |         1 |       24
  4 |           1 |         1 |       18
  5 |           1 |         1 |       95
  6 |           1 |         1 |       40
  7 |           0 |         2 |       13
  8 |           0 |         2 |       84
  9 |           0 |         2 |       41
 10 |           1 |         3 |       29
 11 |           1 |         3 |       34
 12 |           1 |         3 |       56
 13 |           1 |         3 |       52



thomas=# SELECT * FROM matches_first
thomas-# \g
 id | whichgame | dealershand | whichplayer | thisplayershand | playerresult
----+-----------+-------------+-------------+-----------------+--------------
  1 |         0 |           0 |           1 |               1 |            1
  2 |         1 |           2 |           1 |               3 |            2
(2 rows)

thomas=# SELECT * FROM matches_second
thomas-# \g
 id | whichgame | dealershand | whichplayer | thisplayershand | playerresult
----+-----------+-------------+-------------+-----------------+--------------
  1 |         0 |           0 |           1 |               1 |            0
  2 |         1 |           2 |           1 |               3 |            2
(2 rows)

我希望将它们结合起来:

hands_combined table:
 id | whichplayer | whichhand | thiscard
----+-------------+-----------+----------
  1 |           0 |         0 |        6 --Seven of Spades
  2 |           0 |         0 |       63 --Queen of Spades
  3 |           0 |         0 |       41 --Three of Clubs
  4 |           1 |         1 |       76
  5 |           1 |         1 |       23
  6 |           0 |         2 |       51
  7 |           0 |         2 |       29
  8 |           0 |         2 |        2
  9 |           0 |         2 |       92
 10 |           0 |         2 |        6
 11 |           1 |         3 |      101
 12 |           1 |         3 |        8
 13 |           0 |         4 |       78
 14 |           0 |         4 |       38
 15 |           1 |         5 |       24
 16 |           1 |         5 |       18
 17 |           1 |         5 |       95
 18 |           1 |         5 |       40
 19 |           0 |         6 |       13
 20 |           0 |         6 |       84
 21 |           0 |         6 |       41
 22 |           1 |         7 |       29
 23 |           1 |         7 |       34
 24 |           1 |         7 |       56
 25 |           1 |         7 |       52

matches_combined table:
 id | whichgame | dealershand | whichplayer | thisplayershand | playerresult
----+-----------+-------------+-------------+-----------------+--------------
  1 |         0 |           0 |           1 |               1 |            1
  2 |         1 |           2 |           1 |               3 |            2
  3 |         2 |           4 |           1 |               5 |            0
  4 |         3 |           6 |           1 |               7 |            2

" thiscard"的每个值代表[1..104]范围内的扑克牌 - 52张扑克牌,其中一个额外的位代表它是面朝上还是面朝下。出于空间原因,我没有发布实际的桌子。 所以玩家0(又名经销商)在第一场比赛中有一手(黑桃七人,太空女王,俱乐部三人)。

4 个答案:

答案 0 :(得分:4)

我认为您并未按照预期的方式使用PostgreSQL,而且您的桌面设计可能不适合您想要实现的目标。虽然很难理解你希望你的解决方案能实现什么,但我写了这个,它似乎只使用少数几个表来解决你想要的一切,以及返回记录集的函数来模拟你对个别运行的需求。我使用Enums和复杂类型来说明您可能希望从PostgreSQL的强大功能中利用的一些功能。

另外,我不确定参数化的表名是什么(我从未在任何RDBMS中看到类似的东西),但PostgreSQL确实允许一些非常合适的东西:记录集返回函数。

CREATE TYPE card_value AS ENUM ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K');
CREATE TYPE card_suit AS ENUM ('Clubs', 'Diamonds', 'Hearts', 'Spades');
CREATE TYPE card AS (value card_value, suit card_suit, face_up bool);

CREATE TABLE runs (
  run_id bigserial NOT NULL PRIMARY KEY,
  run_date timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
  );

CREATE TABLE players (
  run_id bigint NOT NULL REFERENCES runs,
  player_no int NOT NULL, -- 0 can be assumed as always the dealer
  ai_type text NOT NULL,
  PRIMARY KEY (run_id, player_no)
  );

CREATE TABLE matches (
  run_id bigint NOT NULL REFERENCES runs,
  match_no int NOT NULL,
  PRIMARY KEY (run_id, match_no)
  );

CREATE TABLE hands (
  hand_id bigserial NOT NULL PRIMARY KEY,
  run_id bigint NOT NULL REFERENCES runs,
  match_no int NOT NULL,
  hand_no int NOT NULL,
  player_no int NOT NULL,
  UNIQUE (run_id, match_no, hand_no),
  FOREIGN KEY (run_id, match_no) REFERENCES matches,
  FOREIGN KEY (run_id, player_no) REFERENCES players
  );

CREATE TABLE deals (
  deal_id bigserial NOT NULL PRIMARY KEY,
  hand_id bigint NOT NULL REFERENCES hands,
  card card NOT NULL
  );

CREATE OR REPLACE FUNCTION players(int) RETURNS SETOF players AS $$
  SELECT * FROM players WHERE run_id = $1 ORDER BY player_no;
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION matches(int) RETURNS SETOF matches AS $$
  SELECT * FROM matches WHERE run_id = $1 ORDER BY match_no;
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION hands(int) RETURNS SETOF hands AS $$
  SELECT * FROM hands WHERE run_id = $1 ORDER BY match_no, hand_no;
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION hands(int, int) RETURNS SETOF hands AS $$
  SELECT * FROM hands WHERE run_id = $1 AND match_no = $2 ORDER BY hand_no;
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION winner_player (int, int) RETURNS int AS $$
  SELECT player_no
  FROM hands
  WHERE run_id = $1 AND match_no = $2
  ORDER BY hand_no DESC
  LIMIT 1
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION next_player_no (int) RETURNS int AS $$
  SELECT CASE WHEN EXISTS (SELECT 1 FROM runs WHERE run_id = $1) THEN
         COALESCE((SELECT MAX(player_no) FROM players WHERE run_id = $1), 0) + 1 END
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION next_match_no (int) RETURNS int AS $$
  SELECT CASE WHEN EXISTS (SELECT 1 FROM runs WHERE run_id = $1) THEN
         COALESCE((SELECT MAX(match_no) FROM matches WHERE run_id = $1), 0) + 1 END
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION next_hand_no (int) RETURNS int AS $$
  SELECT CASE WHEN EXISTS (SELECT 1 FROM runs WHERE run_id = $1) THEN
         COALESCE((SELECT MAX(hand_no) + 1 FROM hands WHERE run_id = $1), 0) END
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION card_to_int (card) RETURNS int AS $$
  SELECT ((SELECT enumsortorder::int-1 FROM pg_enum WHERE enumtypid = 'card_suit'::regtype AND enumlabel = ($1).suit::name) * 13 +
          (SELECT enumsortorder::int-1 FROM pg_enum WHERE enumtypid = 'card_value'::regtype AND enumlabel = ($1).value::name) + 1) *
         CASE WHEN ($1).face_up THEN 2 ELSE 1 END
$$ LANGUAGE SQL; -- SELECT card_to_int(('3', 'Spades', false))

CREATE OR REPLACE FUNCTION int_to_card (int) RETURNS card AS $$
  SELECT ((SELECT enumlabel::card_value FROM pg_enum WHERE enumtypid = 'card_value'::regtype AND enumsortorder = ((($1-1)%13)+1)::real),
          (SELECT enumlabel::card_suit  FROM pg_enum WHERE enumtypid = 'card_suit'::regtype  AND enumsortorder = (((($1-1)/13)::int%4)+1)::real),
          $1 > (13*4))::card
$$ LANGUAGE SQL; -- SELECT i, int_to_card(i) FROM generate_series(1, 13*4*2) i

CREATE OR REPLACE FUNCTION deal_cards(int, int, int, int[]) RETURNS TABLE (player_no int, hand_no int, card card) AS $$
  WITH
    hand AS (
      INSERT INTO hands (run_id, match_no, player_no, hand_no)
      VALUES ($1, $2, $3, next_hand_no($1))
      RETURNING hand_id, player_no, hand_no),
    mydeals AS (
      INSERT INTO deals (hand_id, card)
      SELECT hand_id, int_to_card(card_id)::card AS card
      FROM hand, UNNEST($4) card_id
      RETURNING hand_id, deal_id, card
      )
    SELECT h.player_no, h.hand_no, d.card
    FROM hand h, mydeals d
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION deals(int) RETURNS TABLE (deal_id bigint, hand_no int, player_no int, card int) AS $$
  SELECT d.deal_id, h.hand_no, h.player_no, card_to_int(d.card)
  FROM hands h
  JOIN deals d ON (d.hand_id = h.hand_id)
  WHERE h.run_id = $1
  ORDER BY d.deal_id;
$$ LANGUAGE SQL;

INSERT INTO runs DEFAULT VALUES; -- Add first run
INSERT INTO players VALUES (1, 0, 'Dealer'); -- dealer always zero
INSERT INTO players VALUES (1, next_player_no(1), 'Player 1');

INSERT INTO matches VALUES (1, next_match_no(1)); -- First match
SELECT * FROM deal_cards(1, 1, 0, ARRAY[6, 63, 41]);
SELECT * FROM deal_cards(1, 1, 1, ARRAY[76, 23]);
SELECT * FROM deal_cards(1, 1, 0, ARRAY[51, 29, 2, 92, 6]);
SELECT * FROM deal_cards(1, 1, 1, ARRAY[101, 8]);

INSERT INTO matches VALUES (1, next_match_no(1)); -- Second match
SELECT * FROM deal_cards(1, 2, 0, ARRAY[78, 38]);
SELECT * FROM deal_cards(1, 2, 1, ARRAY[24, 18, 95, 40]);
SELECT * FROM deal_cards(1, 2, 0, ARRAY[13, 84, 41]);
SELECT * FROM deal_cards(1, 2, 1, ARRAY[29, 34, 56, 52]);

SELECT * FROM deals(1); -- This is the output you need (hands_combined table)

-- This view can be used to retrieve the list of all winning hands
CREATE OR REPLACE VIEW winning_hands AS
  SELECT DISTINCT ON (run_id, match_no) *
  FROM hands
  ORDER BY run_id, match_no, hand_no DESC;

SELECT * FROM winning_hands;

答案 1 :(得分:3)

不会使用UNION运算符吗?

对于双手关系:

SELECT * FROM hands_first
UNION ALL
SELECT * FROM hands_second

对于匹配关系:

SELECT * FROM matches_first
UNION ALL
SELECT * FROM matches_second

作为一个更长期的解决方案,我考虑重构数据库,因为这种模式很快就会变得无法管理。为什么不通过引入游戏桌来改善规范化?

换句话说,游戏有很多匹配匹配每个游戏都有很多玩家 玩家每个比赛都有很多牌。

我建议在纸上绘制实体关系的UML(http://dawgsquad.googlecode.com/hg/docs/database_images/Database_Model_Diagram(Title).png),然后改进模式,以便使用普通的SQL运算符进行查询。

希望这有帮助。

修改

在这种情况下,您可以使用rownumber() PG函数在两个表的并集上使用子查询来表示行号:

SELECT 
  row_number() AS id,
  whichplayer,
  whichhand,
  thiscard
FROM
(
  SELECT * FROM hands_first
  UNION ALL
  SELECT * FROM hands_second
);

相同的原则适用于匹配表。显然,这对于即使是少量的表也不能很好地扩展,因此优先考虑规范化模式。

某些PG功能的文档:http://www.postgresql.org/docs/current/interactive/functions-window.html

答案 2 :(得分:1)

使用两个表的所有行构建新表,执行:

CREATE TABLE hands AS 
  select 1 as hand, id, whichplayer, whichhand, thiscard
  from hands_first
  union all
  select 2 as hand, id, whichplayer, whichhand, thiscard
  from hands_second

之后,要插入新matche的数据,请在当前最后+ 1

上创建序列
CREATE SEQUENCE matche START 3;

在插入读取序列值之前,并在插入中使用它:

SELECT nextval('matche');

答案 3 :(得分:1)

您的数据库结构不是很好,我确信它不是可扩展的方法,可以快速创建表。创建物理表而不是使用现有结构存在性能缺陷。我建议你重构你的数据库结构。

但是,您可以使用UNION运算符合并数据。