Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 503869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:26:17+00:00 2026-05-13T06:26:17+00:00

I am working on a Rails project and hitting a performance problem. Here is

  • 0

I am working on a Rails project and hitting a performance problem. Here is the simplified db schema

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

Now given a username, I want to display players whose names match the input, with no. of games they played, won or lost.

I created 3 views to avoid 1+n problem.

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;

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:26:17+00:00Added an answer on May 13, 2026 at 6:26 am

    I think you have some general scalability issues with your design. As you add games and users the way you’re counting up their wins/loses will become significantly more work for your database from an I/O perspective.

    I would suggest creating a table called player_record which is simply the players ID, WINS, LOSSES. Then create a stored procedure that is called when a game finishes. ( say spGameFinished(game_id, winner,..); ) That stored procedure would be responsible for doing any tasks needed for a game that has completed, and one of those tasks is to update that player_record table based on whom is stated to be the winner. I’m assuming when the game starts you put the game in with the 2 player ids, but if you don’t then the stored procedure would have to take those in as well at the completion of the game.

    Once this is in place, the queries needed for gathering win/loss information is trivial. This also allows you to manage the storage requirements of the games separate from the players records. (i.e. you can drop games that are really old without affecting a users record)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Both statements usually mean composition. But it seems to me… May 14, 2026 at 4:29 pm
  • Editorial Team
    Editorial Team added an answer It's a usual practice to save some values as session… May 14, 2026 at 4:29 pm
  • Editorial Team
    Editorial Team added an answer It's painful, but you can use the UIStringDrawing additions to… May 14, 2026 at 4:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.