Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql query optimization
    primarykey
    data
    text
    <p>I am working on a Rails project and hitting a performance problem. Here is the simplified db schema</p> <pre> table gaming_platforms ( ~5 rows) id table games ( ~10k rows) id gaming_platform_id winner (black or white or n/a) black_id => online_players.id white_id => online_players.id table online_players ( ~1k rows) id gaming_platform_id username </pre> <p>Now given a username, I want to display players whose names match the input, with no. of games they played, won or lost. </p> <p>I created 3 views to avoid 1+n problem. </p> <pre> create or replace view online_player_games as select online_players.id as online_player_id, count(*) as games from games left join online_players on games.gaming_platform_id = online_players.gaming_platform_id and (games.black_id = online_players.id or games.white_id = online_players.id) group by online_players.id; create or replace view online_player_won_games as select online_players.id as online_player_id, count(games.id) as games from online_players left join games on games.gaming_platform_id = online_players.gaming_platform_id and ((games.winner = 1 and games.black_id = online_players.id) or (games.winner = 2 and games.white_id = online_players.id)) group by online_players.id; create or replace view online_player_lost_games as select online_players.id as online_player_id, count(games.id) as games from online_players left join games on games.gaming_platform_id = online_players.gaming_platform_id and ((games.winner = 2 and games.black_id = online_players.id) or (games.winner = 1 and games.white_id = online_players.id)) group by online_players.id; </pre> <p>Without using any index, it takes > 20 secs to query against those views. The query looks complicated. I am not sure which indexes I should create. Any opinions or suggestions are very welcome.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload