例子
# \donttest{
lahman_s <- dbplyr::lahman_sqlite()
#> Creating table: AllstarFull
#> Creating table: Appearances
#> Creating table: AwardsManagers
#> Creating table: AwardsPlayers
#> Creating table: AwardsShareManagers
#> Creating table: AwardsSharePlayers
#> Creating table: Batting
#> Creating table: BattingPost
#> Creating table: CollegePlaying
#> Creating table: Fielding
#> Creating table: FieldingOF
#> Creating table: FieldingOFsplit
#> Creating table: FieldingPost
#> Creating table: HallOfFame
#> Creating table: HomeGames
#> Creating table: LahmanData
#> Creating table: Managers
#> Creating table: ManagersHalf
#> Creating table: Parks
#> Creating table: People
#> Creating table: Pitching
#> Creating table: PitchingPost
#> Creating table: Salaries
#> Creating table: Schools
#> Creating table: SeriesPost
#> Creating table: Teams
#> Creating table: TeamsFranchises
#> Creating table: TeamsHalf
batting <- tbl(lahman_s, "Batting")
batting %>% show_query()
#> <SQL>
#> SELECT *
#> FROM `Batting`
batting %>% explain()
#> <SQL>
#> SELECT *
#> FROM `Batting`
#>
#> <PLAN>
#> id parent notused detail
#> 1 2 0 0 SCAN Batting
# The batting database has indices on all ID variables:
# SQLite automatically picks the most restrictive index
batting %>% filter(lgID == "NL" & yearID == 2000L) %>% explain()
#> <SQL>
#> SELECT *
#> FROM `Batting`
#> WHERE (`lgID` = 'NL' AND `yearID` = 2000)
#>
#> <PLAN>
#> id parent notused detail
#> 1 3 0 0 SEARCH Batting USING INDEX Batting_yearID (yearID=?)
# OR's will use multiple indexes
batting %>% filter(lgID == "NL" | yearID == 2000) %>% explain()
#> <SQL>
#> SELECT *
#> FROM `Batting`
#> WHERE (`lgID` = 'NL' OR `yearID` = 2000.0)
#>
#> <PLAN>
#> id parent notused detail
#> 1 4 0 0 MULTI-INDEX OR
#> 2 5 4 0 INDEX 1
#> 3 11 5 0 SEARCH Batting USING INDEX Batting_lgID (lgID=?)
#> 4 16 4 0 INDEX 2
#> 5 22 16 0 SEARCH Batting USING INDEX Batting_yearID (yearID=?)
# Joins will use indexes in both tables
teams <- tbl(lahman_s, "Teams")
batting %>% left_join(teams, c("yearID", "teamID")) %>% explain()
#> <SQL>
#> SELECT
#> `playerID`,
#> `Batting`.`yearID` AS `yearID`,
#> `stint`,
#> `Batting`.`teamID` AS `teamID`,
#> `Batting`.`lgID` AS `lgID.x`,
#> `Batting`.`G` AS `G.x`,
#> `Batting`.`AB` AS `AB.x`,
#> `Batting`.`R` AS `R.x`,
#> `Batting`.`H` AS `H.x`,
#> `Batting`.`X2B` AS `X2B.x`,
#> `Batting`.`X3B` AS `X3B.x`,
#> `Batting`.`HR` AS `HR.x`,
#> `RBI`,
#> `Batting`.`SB` AS `SB.x`,
#> `Batting`.`CS` AS `CS.x`,
#> `Batting`.`BB` AS `BB.x`,
#> `Batting`.`SO` AS `SO.x`,
#> `IBB`,
#> `Batting`.`HBP` AS `HBP.x`,
#> `SH`,
#> `Batting`.`SF` AS `SF.x`,
#> `GIDP`,
#> `Teams`.`lgID` AS `lgID.y`,
#> `franchID`,
#> `divID`,
#> `Rank`,
#> `Teams`.`G` AS `G.y`,
#> `Ghome`,
#> `W`,
#> `L`,
#> `DivWin`,
#> `WCWin`,
#> `LgWin`,
#> `WSWin`,
#> `Teams`.`R` AS `R.y`,
#> `Teams`.`AB` AS `AB.y`,
#> `Teams`.`H` AS `H.y`,
#> `Teams`.`X2B` AS `X2B.y`,
#> `Teams`.`X3B` AS `X3B.y`,
#> `Teams`.`HR` AS `HR.y`,
#> `Teams`.`BB` AS `BB.y`,
#> `Teams`.`SO` AS `SO.y`,
#> `Teams`.`SB` AS `SB.y`,
#> `Teams`.`CS` AS `CS.y`,
#> `Teams`.`HBP` AS `HBP.y`,
#> `Teams`.`SF` AS `SF.y`,
#> `RA`,
#> `ER`,
#> `ERA`,
#> `CG`,
#> `SHO`,
#> `SV`,
#> `IPouts`,
#> `HA`,
#> `HRA`,
#> `BBA`,
#> `SOA`,
#> `E`,
#> `DP`,
#> `FP`,
#> `name`,
#> `park`,
#> `attendance`,
#> `BPF`,
#> `PPF`,
#> `teamIDBR`,
#> `teamIDlahman45`,
#> `teamIDretro`
#> FROM `Batting`
#> LEFT JOIN `Teams`
#> ON (
#> `Batting`.`yearID` = `Teams`.`yearID` AND
#> `Batting`.`teamID` = `Teams`.`teamID`
#> )
#>
#> <PLAN>
#> id parent notused
#> 1 4 0 0
#> 2 6 0 0
#> detail
#> 1 SCAN Batting
#> 2 SEARCH Teams USING INDEX Teams_yearID (yearID=?) LEFT-JOIN
# }
相关用法
- R dplyr group_trim 修剪分组结构
- R dplyr slice 使用行的位置对行进行子集化
- R dplyr copy_to 将本地数据帧复制到远程src
- R dplyr sample_n 从表中采样 n 行
- R dplyr consecutive_id 为连续组合生成唯一标识符
- R dplyr row_number 整数排名函数
- R dplyr band_members 乐队成员
- R dplyr mutate-joins 变异连接
- R dplyr nth 从向量中提取第一个、最后一个或第 n 个值
- R dplyr coalesce 找到第一个非缺失元素
- R dplyr group_split 按组分割 DataFrame
- R dplyr mutate 创建、修改和删除列
- R dplyr order_by 用于排序窗口函数输出的辅助函数
- R dplyr context 有关“当前”组或变量的信息
- R dplyr percent_rank 比例排名函数
- R dplyr recode 重新编码值
- R dplyr starwars 星球大战人物
- R dplyr desc 降序
- R dplyr between 检测值落在指定范围内的位置
- R dplyr cumall 任何、全部和平均值的累积版本
- R dplyr group_map 对每个组应用一个函数
- R dplyr do 做任何事情
- R dplyr nest_join 嵌套连接
- R dplyr pull 提取单列
- R dplyr group_by_all 按选择的变量进行分组
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Explain details of a tbl。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。