0

I have a task which combines multiple queries and it works fine.

db.task(t => {
const a = studies => t.any ('SELECT facility_contacts.contact_type, facility_contacts.name, facility_contacts.email, facility_contacts.phone FROM facility_contacts WHERE facility_contacts.nct_id = $1', studies.nct_id).then(facility_contacts => {
    studies.facility_contacts = facility_contacts;
    return studies;
  });
  const b = studies => t.any ('SELECT eligibilities.criteria, eligibilities.gender FROM eligibilities WHERE eligibilities.nct_id = $1', studies.nct_id).then(eligibilities => {
      studies.eligibilities = eligibilities;
      return studies;
    });
    const c = studies => t.any ('SELECT interventions.intervention_type, interventions.name, interventions.description FROM interventions WHERE interventions.nct_id = $1', studies.nct_id).then(interventions => {
        studies.interventions = interventions;
        return studies;
      });
      const d = studies => t.any ('SELECT design_groups.group_type, design_groups.title, design_groups.description FROM design_groups  WHERE design_groups.nct_id = $1', studies.nct_id).then(design_groups => {
          studies.design_groups = design_groups;
          return studies;
        });

    const e = studies => t.batch([a(studies), b(studies), c(studies), d(studies)]);
    return t.map('SELECT DISTINCT ON (facilities.nct_id) studies.nct_id, studies.phase, studies.enrollment, studies.overall_status, facilities.city FROM studies INNER JOIN facilities ON facilities.nct_id = studies.nct_id AND facilities.country LIKE \'%Ireland%\' ORDER BY facilities.nct_id LIMIT 20 OFFSET ($1 - 1) * 20', [page],e).then(t.batch);
}).then(studies => {
  res.send(studies);
}).catch(error => {
  console.log(error);
});

But in the response, I get same values multiple times and I've tried the queries individually and the responses were not having any duplicates. My current response is as follows (only top 3 are shown):

{
        "nct_id": "NCT00002755",
        "phase": "Phase 3",
        "enrollment": 600,
        "overall_status": "Completed",
        "city": "Dublin",
        "facility_contacts": [],
        "eligibilities": [
            {
                "criteria": "\n        DISEASE CHARACTERISTICS: Histologically confirmed stage II or IIIA breast cancer with at\n        least 4 positive axillary nodes Definitive resection required, preferably within 4 weeks\n        prior to entry No overt residual axillary nodal carcinoma after surgery Hormone receptor\n        status: Not specified\n\n        PATIENT CHARACTERISTICS: Age: Over 18 Sex: Female Menopausal status: Not specified\n        Performance status: ECOG 0 or 1 Hematopoietic: Absolute neutrophil count greater than\n        1,500/mm3 Platelet count greater than 100,000/mm3 Hemoglobin greater than 9 g/dL PT and\n        aPTT normal Hepatic: Bilirubin normal (unless benign congenital hyperbilirubinemia) Normal\n        liver biopsy required in patients with active hepatitis B or C Renal: Creatinine normal\n        Cardiovascular: No active heart disease Normal wall motion on MUGA or echocardiogram Other:\n        Adequate nutritional status (i.e., more than 1,000 calories/day orally) HIV negative No\n        serious medical or psychiatric disease No second malignancy except: Basal cell skin cancer\n        Carinoma in situ of the cervix Not pregnant Negative pregnancy test\n\n        PRIOR CONCURRENT THERAPY: At least 2 weeks since major surgery\n      ",
                "gender": "Female"
            }
        ],
        "interventions": [
            {
                "intervention_type": "Biological",
                "name": "filgrastim",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "CMF regimen",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "cyclophosphamide",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "doxorubicin hydrochloride",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "fluorouracil",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "methotrexate",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "tamoxifen citrate",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "thiotepa",
                "description": null
            },
            {
                "intervention_type": "Procedure",
                "name": "autologous bone marrow transplantation",
                "description": null
            },
            {
                "intervention_type": "Procedure",
                "name": "peripheral blood stem cell transplantation",
                "description": null
            },
            {
                "intervention_type": "Radiation",
                "name": "low-LET cobalt-60 gamma ray therapy",
                "description": null
            },
            {
                "intervention_type": "Radiation",
                "name": "low-LET electron therapy",
                "description": null
            },
            {
                "intervention_type": "Radiation",
                "name": "low-LET photon therapy",
                "description": null
            }
        ],
        "design_groups": []
    },
    {
        "nct_id": "NCT00002755",
        "phase": "Phase 3",
        "enrollment": 600,
        "overall_status": "Completed",
        "city": "Dublin",
        "facility_contacts": [],
        "eligibilities": [
            {
                "criteria": "\n        DISEASE CHARACTERISTICS: Histologically confirmed stage II or IIIA breast cancer with at\n        least 4 positive axillary nodes Definitive resection required, preferably within 4 weeks\n        prior to entry No overt residual axillary nodal carcinoma after surgery Hormone receptor\n        status: Not specified\n\n        PATIENT CHARACTERISTICS: Age: Over 18 Sex: Female Menopausal status: Not specified\n        Performance status: ECOG 0 or 1 Hematopoietic: Absolute neutrophil count greater than\n        1,500/mm3 Platelet count greater than 100,000/mm3 Hemoglobin greater than 9 g/dL PT and\n        aPTT normal Hepatic: Bilirubin normal (unless benign congenital hyperbilirubinemia) Normal\n        liver biopsy required in patients with active hepatitis B or C Renal: Creatinine normal\n        Cardiovascular: No active heart disease Normal wall motion on MUGA or echocardiogram Other:\n        Adequate nutritional status (i.e., more than 1,000 calories/day orally) HIV negative No\n        serious medical or psychiatric disease No second malignancy except: Basal cell skin cancer\n        Carinoma in situ of the cervix Not pregnant Negative pregnancy test\n\n        PRIOR CONCURRENT THERAPY: At least 2 weeks since major surgery\n      ",
                "gender": "Female"
            }
        ],
        "interventions": [
            {
                "intervention_type": "Biological",
                "name": "filgrastim",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "CMF regimen",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "cyclophosphamide",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "doxorubicin hydrochloride",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "fluorouracil",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "methotrexate",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "tamoxifen citrate",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "thiotepa",
                "description": null
            },
            {
                "intervention_type": "Procedure",
                "name": "autologous bone marrow transplantation",
                "description": null
            },
            {
                "intervention_type": "Procedure",
                "name": "peripheral blood stem cell transplantation",
                "description": null
            },
            {
                "intervention_type": "Radiation",
                "name": "low-LET cobalt-60 gamma ray therapy",
                "description": null
            },
            {
                "intervention_type": "Radiation",
                "name": "low-LET electron therapy",
                "description": null
            },
            {
                "intervention_type": "Radiation",
                "name": "low-LET photon therapy",
                "description": null
            }
        ],
        "design_groups": []
    },
    {
        "nct_id": "NCT00002755",
        "phase": "Phase 3",
        "enrollment": 600,
        "overall_status": "Completed",
        "city": "Dublin",
        "facility_contacts": [],
        "eligibilities": [
            {
                "criteria": "\n        DISEASE CHARACTERISTICS: Histologically confirmed stage II or IIIA breast cancer with at\n        least 4 positive axillary nodes Definitive resection required, preferably within 4 weeks\n        prior to entry No overt residual axillary nodal carcinoma after surgery Hormone receptor\n        status: Not specified\n\n        PATIENT CHARACTERISTICS: Age: Over 18 Sex: Female Menopausal status: Not specified\n        Performance status: ECOG 0 or 1 Hematopoietic: Absolute neutrophil count greater than\n        1,500/mm3 Platelet count greater than 100,000/mm3 Hemoglobin greater than 9 g/dL PT and\n        aPTT normal Hepatic: Bilirubin normal (unless benign congenital hyperbilirubinemia) Normal\n        liver biopsy required in patients with active hepatitis B or C Renal: Creatinine normal\n        Cardiovascular: No active heart disease Normal wall motion on MUGA or echocardiogram Other:\n        Adequate nutritional status (i.e., more than 1,000 calories/day orally) HIV negative No\n        serious medical or psychiatric disease No second malignancy except: Basal cell skin cancer\n        Carinoma in situ of the cervix Not pregnant Negative pregnancy test\n\n        PRIOR CONCURRENT THERAPY: At least 2 weeks since major surgery\n      ",
                "gender": "Female"
            }
        ],
        "interventions": [
            {
                "intervention_type": "Biological",
                "name": "filgrastim",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "CMF regimen",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "cyclophosphamide",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "doxorubicin hydrochloride",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "fluorouracil",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "methotrexate",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "tamoxifen citrate",
                "description": null
            },
            {
                "intervention_type": "Drug",
                "name": "thiotepa",
                "description": null
            },
            {
                "intervention_type": "Procedure",
                "name": "autologous bone marrow transplantation",
                "description": null
            },
            {
                "intervention_type": "Procedure",
                "name": "peripheral blood stem cell transplantation",
                "description": null
            },
            {
                "intervention_type": "Radiation",
                "name": "low-LET cobalt-60 gamma ray therapy",
                "description": null
            },
            {
                "intervention_type": "Radiation",
                "name": "low-LET electron therapy",
                "description": null
            },
            {
                "intervention_type": "Radiation",
                "name": "low-LET photon therapy",
                "description": null
            }
        ],
        "design_groups": []
    }

Even after DISTINCT keyword is used, I get same results multiple times. How can I sort this issue? Is this some issue with the ordering of my queries?

  • 1
    Start using [pg-monitor](https://github.com/vitaly-t/pg-monitor), and see what queries are actually being executed, so you can see if anything is wrong with the queries. – vitaly-t Jul 24 '18 at 13:28
  • 1
    P.S. In any case, your approach will show quite bad performance, due to too many queries you are trying to execute, while it is possible to do just one query for everything, using JSON. See https://stackoverflow.com/questions/39805736/get-join-table-as-array-of-results-with-postgresql-nodejs – vitaly-t Jul 24 '18 at 13:40
  • 1
    Working great with JSON and getting responses in less than 1 second. Thanks a ton @vitaly-t – Naveen George Thoppan Jul 25 '18 at 11:35

0 Answers0