Abstrct / Schemaverse

The Schemaverse is a space-based strategy game implemented entirely within a PostgreSQL database. Compete against other players using raw SQL commands to command your fleet. Or, if your PL/pgSQL-foo is strong, wield it to write AI and have your fleet command itself!
schemaverse.com
PostgreSQL License
356 stars 39 forks source link

Split events into private/global tables #17

Open cbbrowne opened 12 years ago

cbbrowne commented 12 years ago

Splitting them into separate tables allows handling the queries quite a lot more efficiently.

Notably, instead of having a (nasty-to-performance, Seq Scan-inducing) series-of-OR clauses in my_event, it becomes series of UNION ALL subqueries, each of which is quite a bit more efficient.

https://github.com/cbbrowne/Schemaverse/commit/4964097dbc2a50adf8cbddde76c3e97a943c6a66

Abstrct commented 12 years ago

I am definitely down with implementing anything that will make events useful. I have been a bit distant the last week or so but I have some time lined up this week to work on the game. I will work on adding this, the fleet script execution time/error events, and what you mentioned to help speed up my_ships (adding player_id to ship_control w\ and index).

cbbrowne commented 12 years ago

Public DB's down at the moment, so I can't offer up samples/query plans, but it looks likely to me that the use of CTEs for the event view may have been a little bit too clever.

I was trying to query for how long fleet scripts have been running, lately, hence select * from my_events where action = 'FLEET_SUCCESS' where tic > (select last_value from tic_seq) - 50;

In principle, both the tic restriction and the action restriction ought to offer excellent opportunity to restrict things to the last 50 fleet script entries.

Unfortunately, it doesn't seem that the filters on action or tic are getting pushed down into the 3 UNION ALL portions of the query, so this reverted into a trio of Seq Scans on event, and winds up timing out.

As cool as CTE is, it seems to me that you may want to turn it back into a simpler trio of UNION ALL subqueries; I think that'll be able to take advantage of the indexes rather more readily.

cbbrowne commented 12 years ago

Here's a patch that creates 3 views:

  1. My self-inflicted events
  2. Other-inflicted events
  3. Public events

https://github.com/cbbrowne/Schemaverse/commit/c027eebe99992be5fc7bc06ede1c90dce9cc1301

Grab any indexes that you like...