[R] Reading SQL data - all at once, or "as needed"?

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue Jul 15 17:01:10 CEST 2014

The actual answer (which way is faster) in general can go either way depending how much data you extract and how well optimized the SQL queries and indexes are. The amount of data you mention fit one or give weeks seems small for most modern computers, though (you did not mention how many columns but it would have to be a lot to change the story).

I would go for all five weeks at once, and only break it up if I had problems.
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
Sent from my phone. Please excuse my brevity.

On July 15, 2014 7:38:49 AM PDT, John McKown <john.archie.mckown at gmail.com> wrote:
>I have some data in an SQL data base (PostgreSQL to be exact). This
>base (one table in particular) has a lot of data in it. Years worth, in
>fact (2.3 million rows since 2012). That just for background.
>My question is: If I am going to create 5 separate graphs, one graph
>each of the previous 5 weeks (in this case a week is Sunday to
>is it better to read in all 5 weeks worth of data in a single
>where the SELECT has a WHERE clause which will get the proper 5 weeks
>of data, then subset in R. Or is it better to get a single weeks worth
>data in a dbGetQuery, with the proper SELECT ... WHERE. And then
>each week. There seems to be anywhere from 7,500 to 9,000 entries for a
>single week. The calculations for each week are independent of any
>week's data. Basically I am just creating a simple bar chart.
>In the first case, I do one SELECT; and then subset the data.frame data
>the for() loop. In the second case, I still use a for() loop, but I do
>SELECT in each iteration, but don't need to subset the data.frame.
>I have read the "Data Import/Export". The only advice I can find is
>The sort of statistical applications for which DBMS might be used are
>extract a 10% sample of the data, to cross-tabulate data to produce a
>multi-dimensional contingency table, and to extract data group by group
>from a database for separate analysis.
>The "extract data group by group ..." seems, to me, to say that I
>extract and process each week's data separately, using a dbGetQuery in
>loop. Am I interpreting this correctly?
>Thanks for your advice.

More information about the R-help mailing list