Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. ---
  2. title: "Benchmarking database queries"
  3. author: "Barbara Borges Ribeiro"
  4. date: "June 23, 2016"
  5. output: html_document
  6. ---
  7.  
  8. ```{r setup, include=FALSE}
  9. knitr::opts_chunk$set(echo = TRUE)
  10. library(DBI)
  11. library(pool)
  12. ```
  13.  
  14. ### One connection per app
  15.  
  16. ```{r app}
  17. app_times <- numeric(0)
  18.  
  19. conn <- DBI::dbConnect(
  20. drv = RMySQL::MySQL(),
  21. dbname = "shinydemo",
  22. host = "shiny-demo.csa7qlmguqrf.us-east-1.rds.amazonaws.com",
  23. username = "guest",
  24. password = "guest"
  25. )
  26.  
  27. for (id in 1:30) {
  28. time <- system.time({
  29. sql <- paste0("SELECT * FROM City WHERE ID = ", id, ";")
  30. dbGetQuery(conn, sql)
  31. })
  32. app_times <- c(app_times, time["elapsed"])
  33. }
  34.  
  35. invisible(DBI::dbDisconnect(conn))
  36. ```
  37.  
  38. The average of these 30 measurements is `r mean(app_times)` seconds.
  39.  
  40. ### One connection per query
  41.  
  42. ```{r query}
  43. query_times <- numeric(0)
  44.  
  45. args <- list(
  46. drv = RMySQL::MySQL(),
  47. dbname = "shinydemo",
  48. host = "shiny-demo.csa7qlmguqrf.us-east-1.rds.amazonaws.com",
  49. username = "guest",
  50. password = "guest"
  51. )
  52.  
  53. for (id in 1:30) {
  54. time <- system.time({
  55. conn <- do.call(DBI::dbConnect, args)
  56. sql <- paste0("SELECT * FROM City WHERE ID = ", id, ";")
  57. dbGetQuery(conn, sql)
  58. DBI::dbDisconnect(conn)
  59. })
  60. query_times <- c(query_times, time["elapsed"])
  61. }
  62. ```
  63.  
  64. The average of these 30 measurements is `r mean(query_times)` seconds.
  65.  
  66. ### Using pool
  67.  
  68. ```{r pool}
  69. pool_times <- numeric(0)
  70.  
  71. pool <- dbPool(
  72. drv = RMySQL::MySQL(),
  73. dbname = "shinydemo",
  74. host = "shiny-demo.csa7qlmguqrf.us-east-1.rds.amazonaws.com",
  75. username = "guest",
  76. password = "guest"
  77. )
  78.  
  79. for (id in 1:30) {
  80. time <- system.time({
  81. sql <- paste0("SELECT * FROM City WHERE ID = ", id, ";")
  82. dbGetQuery(pool, sql)
  83. })
  84. pool_times <- c(pool_times, time["elapsed"])
  85. }
  86.  
  87. poolClose(pool)
  88. ```
  89.  
  90. The average of these 30 measurements is `r mean(pool_times)` seconds.
  91.  
  92. ## Overall comparison
  93.  
  94. ```{r times}
  95. (times <- data.frame(app_times, query_times, pool_times))
  96. ```
  97.  
  98. As you can see, the app-level measurements are the fastest. This is no suprise. The pool measurements take a little over double that time, since you still do have to fetch and return the connection to the pool (in addition to calculating the query itself). Finally, the query-level measurements are the longest. Again, this makes sense because for each query, you're actually fetching a connection from the remote database.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement