2016-09-01 6 views
2

3つの表に対して結合を実行しているため、何らかの理由で表の1つで索引を使用できません。私はフィールド(date, team)(date, team, player_id)とどちらも適用されているため、DFSにインデックスを持っているSQL結合の索引

SELECT * FROM nfl_schedules ns 
join nfl_daily_fantasy_players dfs 
    on dfs.date = ns.date and dfs.team = ns.home_team 
left join fantasy_salaries sal 
    on sal.platform_id = (SELECT id FROM platforms p WHERE p.platform = 'draft_kings') and sal.game_key = ns.game_key and sal.player_id = dfs.player_id 
left join nfl_players np 
    on np.player_id = dfs.player_id 
where ns.timeframe = @frame; 

:ここ

はクエリです。これは、索引が適用されていない唯一の表です。余分にそれはどこを使用していると言います。

アイデア?

id,select_type,table,type,possible_keys,key,key_len,ref,rows,Extra 
1,PRIMARY,dfs,ALL,"date_team,date_team_player_id",NULL,NULL,NULL,4920,"Using where" 
1,PRIMARY,ns,ref,"date_team_timeframe,date_team",date_team_timeframe,264,"draftcrunch_new.dfs.date,draftcrunch_new.dfs.team",1,"Using index condition" 
1,PRIMARY,sal,ref,"unique,index3,index4,index5",index4,262,"draftcrunch_new.dfs.player_id,draftcrunch_new.ns.game_key",1,"Using where" 
1,PRIMARY,np,ref,"unique_players,index3",unique_players,4,draftcrunch_new.dfs.player_id,1,NULL 
2,SUBQUERY,p,const,platform,platform,257,const,1,"Using index" 
+0

投稿プランの説明 –

+0

書式を設定する最善の方法は不明です。しかしそこにはあります。 1行目では、dfsのインデックスを使用していません。 –

+0

create indexステートメントをdfsに表示できますか? – ESG

答えて

0
where ns.timeframe = @frame  --> INDEX(timeframe) 
ON dfs.date = ns.date and dfs.team = ns.home_team 
            --> INDEX(team, date) 
WHERE p.platform = 'draft_kings' --> INDEX(platform) 
sal.platform_id = (SELECT...)  --> Use JOIN, not IN 
sal.platform_id = ... 
     and sal.game_key = ns.game_key 
     and sal.player_id = dfs.player_id 
       --> INDEX(game_key, player_id, platform_id) (in any order) 
ON np.player_id = dfs.player_id  --> INDEX(player_id) 

に注意してください:

  • PRIMARY KEYUNIQUEキーがINDEXです。
  • INDEX(a), INDEX(a,b)がある場合、最初は冗長です。

Index cookbook

さらにヘルプが必要な場合は、表ごとにSHOW CREATE TABLEを入力してください。