Database connections (DBI, RSQLite) in R Programming - Time & Space Complexity
When working with database connections in R using DBI and RSQLite, it is important to understand how the time to run your code changes as your data or queries grow.
We want to know how the number of operations changes when connecting to a database and running queries.
Analyze the time complexity of the following code snippet.
library(DBI)
library(RSQLite)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "numbers", data.frame(x = 1:1000))
result <- dbGetQuery(con, "SELECT * FROM numbers WHERE x > 500")
dbDisconnect(con)
This code connects to a database, writes a table with 1000 rows, queries rows where x is greater than 500, then disconnects.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing and reading rows in the database table.
- How many times: The write operation processes 1000 rows; the query scans rows to find those matching the condition.
As the number of rows increases, the time to write and query grows roughly in proportion to the number of rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 rows written and scanned |
| 100 | About 100 rows written and scanned |
| 1000 | About 1000 rows written and scanned |
Pattern observation: The operations grow linearly as the number of rows increases.
Time Complexity: O(n)
This means the time to write and query grows roughly in direct proportion to the number of rows.
[X] Wrong: "Connecting to the database takes the same time no matter how many rows I write or query."
[OK] Correct: While connecting is usually fast, writing and querying data depends on how many rows you handle, so time grows with data size.
Understanding how database operations scale with data size helps you write efficient code and explain performance in real projects.
"What if we added an index on column x before querying? How would the time complexity change?"