albertlyu / shot-charts-site

A Rails web application for visualizing NCAA men's basketball play-by-play shot location data
MIT License
3 stars 1 forks source link

Create and populate shots table in the db #48

Closed albertlyu closed 10 years ago

albertlyu commented 10 years ago

I think this is a more permanent solution to the issue raised up in #41 than optimizing the queries. The queries against the plays table are taking the longest because every row is being checked to see if x_coord is null. That's extremely inefficient for a non-indexed column.

So let's create and populate a new shots table, only with shots that have x_coord and y_coord available.

Additionally, from #46, add a zone_id for a shot's zone. We'll also create lookup tables for zones and details with the descriptions of zone and detail in each table (detail being the shot type such as 'Jump Shot,' event being example 'Field Goal Missed.').

albertlyu commented 10 years ago
rails generate model Shot play_id:integer player_id:integer team_id:integer game_id:integer half:integer points_type:integer detail_id:integer zone_id:integer distance:integer x_coord:float y_coord:float made:boolean

The above model generator creates the following initial migration script for creating the shots table:

class CreateShots < ActiveRecord::Migration
  def change
    create_table :shots do |t|
      t.integer :play_id
      t.integer :player_id
      t.integer :team_id
      t.integer :game_id
      t.integer :half
      t.integer :points_type
      t.integer :detail_id
      t.integer :zone_id
      t.integer :distance
      t.float :x_coord
      t.float :y_coord
      t.boolean :made

      t.timestamps
    end
  end
end
albertlyu commented 10 years ago

So these new tables should look something like this:

image