if (require("RSQLite") && has_lahman("sqlite")) {
batting <- tbl(lahman_sqlite(), "Batting")
# Note that you have to do something that actually triggers a query
# inside the explain function
explain_sql(nrow(batting))
explain_sql(nrow(batting))
# nrow requires two queries the first time because it's the same as dim(x)[1]
# but the results are cached
show_sql(head(batting))
explain_sql(head(batting))
# If you just want to understand the sql for a tbl, use explain
explain(batting)
# The batting database has indices on all ID variables:
# SQLite automatically picks the most restrictive index
explain(filter(batting, lgID == "NL" & yearID == 2000))
# OR's will use multiple indexes
explain(filter(batting, lgID == "NL" | yearID == 2000))
}
Run the code above in your browser using DataLab