WIP: optimization on displaying studies list

I got really tired of how slow is the studies display when having so many studies so I started working on https://github.com/biocore/qiita/issues/601. This is the initial optimization of /private/. I want to have some feedback before working on the others. 
\timing might be a little misleading because of caching/behind the scenes optimization (e.g., a simple query such as `select pmid from qiita.study_pmid where study_id = 1001` takes 46ms on my laptop on first execution, then only 0.6 ms on subsequent executions).  Better to use `EXPLAIN`, which will estimate the cost:
First Query:
```sql
SELECT S.study_id, S.study_title, U.name as owner_name, U.email as owner_email,
       S.metadata_complete, S.number_samples_collected, SP.name as pi_name,    
       SP.email as pi_email, count(SRD.raw_data_id) as num_raw_data, SS.status
FROM qiita.study S                                                             
     JOIN qiita.study_status SS on S.study_status_id = SS.study_status_id
     JOIN qiita.study_raw_data SRD on S.study_id = SRD.study_id          
     JOIN qiita.study_person SP on                                             
        S.principal_investigator_id = SP.study_person_id
     JOIN qiita.qiita_user U on S.email = U.email                
WHERE S.study_id = %s                                   
GROUP BY S.study_id, S.study_title, S.metadata_complete,          
         S.number_samples_collected, SP.name, SP.email, SS.status;             
```
Second Query:
```sql
SELECT U.name, U.email                              
FROM qiita.qiita_user U
     JOIN qiita.study_users SU on U.email = SU.email
WHERE U.email = %s;
```
Third Query:
```sql
SELECT pmid from qiita.study_pmid where study_id = %s;
```
## Explain
First query: cost 47.86
```
[                                                                                    +
   {                                                                                  +
     "Plan": {                                                                        +
       "Node Type": "Aggregate",                                                      +
       "Strategy": "Hashed",                                                          +
       "Startup Cost": 47.77,                                                         +
       "Total Cost": 47.86,                                                           +
       "Plan Rows": 9,                                                                +
       "Plan Width": 213,                                                             +
       "Plans": [                                                                     +
         {                                                                            +
           "Node Type": "Nested Loop",                                                +
           "Parent Relationship": "Outer",                                            +
           "Join Type": "Inner",                                                      +
           "Startup Cost": 4.81,                                                      +
           "Total Cost": 47.55,                                                       +
           "Plan Rows": 9,                                                            +
           "Plan Width": 213,                                                         +
           "Plans": [                                                                 +
             {                                                                        +
               "Node Type": "Nested Loop",                                            +
               "Parent Relationship": "Outer",                                        +
               "Join Type": "Inner",                                                  +
               "Startup Cost": 0.59,                                                  +
               "Total Cost": 32.70,                                                   +
               "Plan Rows": 1,                                                        +
               "Plan Width": 205,                                                     +
               "Plans": [                                                             +
                 {                                                                    +
                   "Node Type": "Nested Loop",                                        +
                   "Parent Relationship": "Outer",                                    +
                   "Join Type": "Inner",                                              +
                   "Startup Cost": 0.44,                                              +
                   "Total Cost": 24.52,                                               +
                   "Plan Rows": 1,                                                    +
                   "Plan Width": 173,                                                 +
                   "Plans": [                                                         +
                     {                                                                +
                       "Node Type": "Nested Loop",                                    +
                       "Parent Relationship": "Outer",                                +
                       "Join Type": "Inner",                                          +
                       "Startup Cost": 0.29,                                          +
                       "Total Cost": 16.34,                                           +
                       "Plan Rows": 1,                                                +
                       "Plan Width": 117,                                             +
                       "Plans": [                                                     +
                         {                                                            +
                           "Node Type": "Index Scan",                                 +
                           "Parent Relationship": "Outer",                            +
                           "Scan Direction": "Forward",                               +
                           "Index Name": "pk_study",                                  +
                           "Relation Name": "study",                                  +
                           "Alias": "s",                                              +
                           "Startup Cost": 0.14,                                      +
                           "Total Cost": 8.16,                                        +
                           "Plan Rows": 1,                                            +
                           "Plan Width": 93,                                          +
                           "Index Cond": "(study_id = 1)"                             +
                         },                                                           +
                         {                                                            +
                           "Node Type": "Index Scan",                                 +
                           "Parent Relationship": "Inner",                            +
                           "Scan Direction": "Forward",                               +
                           "Index Name": "pk_study_status",                           +
                           "Relation Name": "study_status",                           +
                           "Alias": "ss",                                             +
                           "Startup Cost": 0.15,                                      +
                           "Total Cost": 8.17,                                        +
                           "Plan Rows": 1,                                            +
                           "Plan Width": 40,                                          +
                           "Index Cond": "(study_status_id = s.study_status_id)"      +
                         }                                                            +
                       ]                                                              +
                     },                                                               +
                     {                                                                +
                       "Node Type": "Index Scan",                                     +
                       "Parent Relationship": "Inner",                                +
                       "Scan Direction": "Forward",                                   +
                       "Index Name": "pk_study_person",                               +
                       "Relation Name": "study_person",                               +
                       "Alias": "sp",                                                 +
                       "Startup Cost": 0.14,                                          +
                       "Total Cost": 8.16,                                            +
                       "Plan Rows": 1,                                                +
                       "Plan Width": 72,                                              +
                       "Index Cond": "(study_person_id = s.principal_investigator_id)"+
                     }                                                                +
                   ]                                                                  +
                 },                                                                   +
                 {                                                                    +
                   "Node Type": "Index Scan",                                         +
                   "Parent Relationship": "Inner",                                    +
                   "Scan Direction": "Forward",                                       +
                   "Index Name": "pk_user",                                           +
                   "Relation Name": "qiita_user",                                     +
                   "Alias": "u",                                                      +
                   "Startup Cost": 0.15,                                              +
                   "Total Cost": 8.17,                                                +
                   "Plan Rows": 1,                                                    +
                   "Plan Width": 64,                                                  +
                   "Index Cond": "((email)::text = (s.email)::text)"                  +
                 }                                                                    +
               ]                                                                      +
             },                                                                       +
             {                                                                        +
               "Node Type": "Bitmap Heap Scan",                                       +
               "Parent Relationship": "Inner",                                        +
               "Relation Name": "study_raw_data",                                     +
               "Alias": "srd",                                                        +
               "Startup Cost": 4.22,                                                  +
               "Total Cost": 14.76,                                                   +
               "Plan Rows": 9,                                                        +
               "Plan Width": 16,                                                      +
               "Recheck Cond": "(study_id = 1)",                                      +
               "Plans": [                                                             +
                 {                                                                    +
                   "Node Type": "Bitmap Index Scan",                                  +
                   "Parent Relationship": "Outer",                                    +
                   "Index Name": "idx_study_raw_data",                                +
                   "Startup Cost": 0.00,                                              +
                   "Total Cost": 4.22,                                                +
                   "Plan Rows": 9,                                                    +
                   "Plan Width": 0,                                                   +
                   "Index Cond": "(study_id = 1)"                                     +
                 }                                                                    +
               ]                                                                      +
             }                                                                        +
           ]                                                                          +
         }                                                                            +
       ]                                                                              +
     }                                                                                +
   }                                                                                  +
 ]
```
Second Query: cost 21.89
```
[                                                                   +
   {                                                                 +
     "Plan": {                                                       +
       "Node Type": "Nested Loop",                                   +
       "Join Type": "Inner",                                         +
       "Startup Cost": 4.35,                                         +
       "Total Cost": 21.89,                                          +
       "Plan Rows": 6,                                               +
       "Plan Width": 64,                                             +
       "Plans": [                                                    +
         {                                                           +
           "Node Type": "Index Scan",                                +
           "Parent Relationship": "Outer",                           +
           "Scan Direction": "Forward",                              +
           "Index Name": "pk_user",                                  +
           "Relation Name": "qiita_user",                            +
           "Alias": "u",                                             +
           "Startup Cost": 0.15,                                     +
           "Total Cost": 8.17,                                       +
           "Plan Rows": 1,                                           +
           "Plan Width": 64,                                         +
           "Index Cond": "((email)::text = 'test@foo.bar'::text)"    +
         },                                                          +
         {                                                           +
           "Node Type": "Bitmap Heap Scan",                          +
           "Parent Relationship": "Inner",                           +
           "Relation Name": "study_users",                           +
           "Alias": "su",                                            +
           "Startup Cost": 4.20,                                     +
           "Total Cost": 13.67,                                      +
           "Plan Rows": 6,                                           +
           "Plan Width": 32,                                         +
           "Recheck Cond": "((email)::text = 'test@foo.bar'::text)", +
           "Plans": [                                                +
             {                                                       +
               "Node Type": "Bitmap Index Scan",                     +
               "Parent Relationship": "Outer",                       +
               "Index Name": "idx_study_users_1",                    +
               "Startup Cost": 0.00,                                 +
               "Total Cost": 4.20,                                   +
               "Plan Rows": 6,                                       +
               "Plan Width": 0,                                      +
               "Index Cond": "((email)::text = 'test@foo.bar'::text)"+
             }                                                       +
           ]                                                         +
         }                                                           +
       ]                                                             +
     }                                                               +
   }                                                                 +
 ]
```
Third query: cost 13.67
```
 [                                          +
   {                                        +
     "Plan": {                              +
       "Node Type": "Bitmap Heap Scan",     +
       "Relation Name": "study_pmid",       +
       "Alias": "study_pmid",               +
       "Startup Cost": 4.20,                +
       "Total Cost": 13.67,                 +
       "Plan Rows": 6,                      +
       "Plan Width": 32,                    +
       "Recheck Cond": "(study_id = 1)",    +
       "Plans": [                           +
         {                                  +
           "Node Type": "Bitmap Index Scan",+
           "Parent Relationship": "Outer",  +
           "Index Name": "idx_study_pmid_0",+
           "Startup Cost": 0.00,            +
           "Total Cost": 4.20,              +
           "Plan Rows": 6,                  +
           "Plan Width": 0,                 +
           "Index Cond": "(study_id = 1)"   +
         }                                  +
       ]                                    +
     }                                      +
   }                                        +
 ]
```
**TOTAL COST: 83.42**

## Initially proposed query
**TOTAL COST: 89.45**
```
[                                                                                   +
   {                                                                                 +
     "Plan": {                                                                       +
       "Node Type": "Sort",                                                          +
       "Startup Cost": 89.44,                                                        +
       "Total Cost": 89.45,                                                          +
       "Plan Rows": 1,                                                               +
       "Plan Width": 101,                                                            +
       "Sort Key": ["study.study_id"],                                               +
       "Plans": [                                                                    +
         {                                                                           +
           "Node Type": "Aggregate",                                                 +
           "Strategy": "Hashed",                                                     +
           "Parent Relationship": "Outer",                                           +
           "Startup Cost": 23.06,                                                    +
           "Total Cost": 89.43,                                                      +
           "Plan Rows": 1,                                                           +
           "Plan Width": 101,                                                        +
           "Plans": [                                                                +
             {                                                                       +
               "Node Type": "Nested Loop",                                           +
               "Parent Relationship": "Outer",                                       +
               "Join Type": "Left",                                                  +
               "Startup Cost": 4.37,                                                 +
               "Total Cost": 23.02,                                                  +
               "Plan Rows": 8,                                                       +
               "Plan Width": 101,                                                    +
               "Plans": [                                                            +
                 {                                                                   +
                   "Node Type": "Index Scan",                                        +
                   "Parent Relationship": "Outer",                                   +
                   "Scan Direction": "Forward",                                      +
                   "Index Name": "idx_study",                                        +
                   "Relation Name": "study",                                         +
                   "Alias": "study",                                                 +
                   "Startup Cost": 0.14,                                             +
                   "Total Cost": 8.16,                                               +
                   "Plan Rows": 1,                                                   +
                   "Plan Width": 93,                                                 +
                   "Index Cond": "((email)::text = 'test@foo.bar'::text)"            +
                 },                                                                  +
                 {                                                                   +
                   "Node Type": "Bitmap Heap Scan",                                  +
                   "Parent Relationship": "Inner",                                   +
                   "Relation Name": "study_raw_data",                                +
                   "Alias": "study_raw_data",                                        +
                   "Startup Cost": 4.22,                                             +
                   "Total Cost": 14.76,                                              +
                   "Plan Rows": 9,                                                   +
                   "Plan Width": 16,                                                 +
                   "Recheck Cond": "(study.study_id = study_id)",                    +
                   "Plans": [                                                        +
                     {                                                               +
                       "Node Type": "Bitmap Index Scan",                             +
                       "Parent Relationship": "Outer",                               +
                       "Index Name": "idx_study_raw_data",                           +
                       "Startup Cost": 0.00,                                         +
                       "Total Cost": 4.22,                                           +
                       "Plan Rows": 9,                                               +
                       "Plan Width": 0,                                              +
                       "Index Cond": "(study.study_id = study_id)"                   +
                     }                                                               +
                   ]                                                                 +
                 }                                                                   +
               ]                                                                     +
             },                                                                      +
             {                                                                       +
               "Node Type": "Hash Join",                                             +
               "Parent Relationship": "SubPlan",                                     +
               "Subplan Name": "SubPlan 1",                                          +
               "Join Type": "Inner",                                                 +
               "Startup Cost": 13.74,                                                +
               "Total Cost": 28.19,                                                  +
               "Plan Rows": 135,                                                     +
               "Plan Width": 64,                                                     +
               "Hash Cond": "((qiita_user.email)::text = (study_users.email)::text)",+
               "Plans": [                                                            +
                 {                                                                   +
                   "Node Type": "Seq Scan",                                          +
                   "Parent Relationship": "Outer",                                   +
                   "Relation Name": "qiita_user",                                    +
                   "Alias": "qiita_user",                                            +
                   "Startup Cost": 0.00,                                             +
                   "Total Cost": 12.70,                                              +
                   "Plan Rows": 270,                                                 +
                   "Plan Width": 64                                                  +
                 },                                                                  +
                 {                                                                   +
                   "Node Type": "Hash",                                              +
                   "Parent Relationship": "Inner",                                   +
                   "Startup Cost": 13.67,                                            +
                   "Total Cost": 13.67,                                              +
                   "Plan Rows": 6,                                                   +
                   "Plan Width": 32,                                                 +
                   "Plans": [                                                        +
                     {                                                               +
                       "Node Type": "Bitmap Heap Scan",                              +
                       "Parent Relationship": "Outer",                               +
                       "Relation Name": "study_users",                               +
                       "Alias": "study_users",                                       +
                       "Startup Cost": 4.20,                                         +
                       "Total Cost": 13.67,                                          +
                       "Plan Rows": 6,                                               +
                       "Plan Width": 32,                                             +
                       "Recheck Cond": "(study_id = study.study_id)",                +
                       "Plans": [                                                    +
                         {                                                           +
                           "Node Type": "Bitmap Index Scan",                         +
                           "Parent Relationship": "Outer",                           +
                           "Index Name": "idx_study_users_0",                        +
                           "Startup Cost": 0.00,                                     +
                           "Total Cost": 4.20,                                       +
                           "Plan Rows": 6,                                           +
                           "Plan Width": 0,                                          +
                           "Index Cond": "(study_id = study.study_id)"               +
                         }                                                           +
                       ]                                                             +
                     }                                                               +
                   ]                                                                 +
                 }                                                                   +
               ]                                                                     +
             },                                                                      +
             {                                                                       +
               "Node Type": "Index Scan",                                            +
               "Parent Relationship": "SubPlan",                                     +
               "Subplan Name": "SubPlan 2",                                          +
               "Scan Direction": "Forward",                                          +
               "Index Name": "pk_study_person",                                      +
               "Relation Name": "study_person",                                      +
               "Alias": "study_person",                                              +
               "Startup Cost": 0.14,                                                 +
               "Total Cost": 8.17,                                                   +
               "Plan Rows": 1,                                                       +
               "Plan Width": 64,                                                     +
               "Index Cond": "(study_person_id = study.principal_investigator_id)"   +
             },                                                                      +
             {                                                                       +
               "Node Type": "Bitmap Heap Scan",                                      +
               "Parent Relationship": "SubPlan",                                     +
               "Subplan Name": "SubPlan 3",                                          +
               "Relation Name": "study_pmid",                                        +
               "Alias": "study_pmid",                                                +
               "Startup Cost": 4.20,                                                 +
               "Total Cost": 13.67,                                                  +
               "Plan Rows": 6,                                                       +
               "Plan Width": 32,                                                     +
               "Recheck Cond": "(study_id = study.study_id)",                        +
               "Plans": [                                                            +
                 {                                                                   +
                   "Node Type": "Bitmap Index Scan",                                 +
                   "Parent Relationship": "Outer",                                   +
                   "Index Name": "idx_study_pmid_0",                                 +
                   "Startup Cost": 0.00,                                             +
                   "Total Cost": 4.20,                                               +
                   "Plan Rows": 6,                                                   +
                   "Plan Width": 0,                                                  +
                   "Index Cond": "(study_id = study.study_id)"                       +
                 }                                                                   +
               ]                                                                     +
             },                                                                      +
             {                                                                       +
               "Node Type": "Index Scan",                                            +
               "Parent Relationship": "SubPlan",                                     +
               "Subplan Name": "SubPlan 4",                                          +
               "Scan Direction": "Forward",                                          +
               "Index Name": "pk_user",                                              +
               "Relation Name": "qiita_user",                                        +
               "Alias": "qiita_user_1",                                              +
               "Startup Cost": 0.15,                                                 +
               "Total Cost": 8.17,                                                   +
               "Plan Rows": 1,                                                       +
               "Plan Width": 64,                                                     +
               "Index Cond": "((email)::text = (study.email)::text)"                 +
             },                                                                      +
             {                                                                       +
               "Node Type": "Index Scan",                                            +
               "Parent Relationship": "SubPlan",                                     +
               "Subplan Name": "SubPlan 5",                                          +
               "Scan Direction": "Forward",                                          +
               "Index Name": "pk_study_status",                                      +
               "Relation Name": "study_status",                                      +
               "Alias": "study_status",                                              +
               "Startup Cost": 0.15,                                                 +
               "Total Cost": 8.17,                                                   +
               "Plan Rows": 1,                                                       +
               "Plan Width": 32,                                                     +
               "Index Cond": "(study_status_id = study.study_status_id)"             +
             }                                                                       +
           ]                                                                         +
         }                                                                           +
       ]                                                                             +
     }                                                                               +
   }                                                                                 +
 ]
```