Two things worth noting
1) Don't use select *
. There are two reasons for that: first, you load more data then you probably need. Second is that you have no control over the order of data (that will be important once we go to point 2))
2) Don't use DictCursor
. It turns each row into a dict which eats lots of memory (since you effectively duplicate column names in each dict). Use default cursor_factory instead. Now in order to know the order of fields returned in tuples you have to specify that order in your select
query.
That should take care of your problem. If it does not, then you have to do the job over smaller set of data.