Performance implications of relying on Access Poli...
# support
r
Our application is patient/family focused: a single user may have access to 1-10 patients each of which may have 1-3 care plans. I'm wondering if there are potential performance implications to relying on access policies to search for all resources that patient has access to instead of narrowing the search via parameters. As an example, I could do this in two passes, searching for a patient's care teams like so
Copy code
const records = new Map<string, PatientRecord>();
  const patients = await client.searchResources('Patient', '_count=1000');
  await Promise.all(
    patients.map(async (patient) => {
      const pid = patient.id;
      const careTeams = await client.searchResources(
        'CareTeam',
        `subject=Patient/${pid}`,
      );
      records.set(pid, { patient, careTeam: careTeams[0] });
    }),
  );
  return { records };
or I could do this as 2 parallel requests
Copy code
const records = new Map<string, PatientRecord>();
  const [patients, careTeams] = await Promise.all([
    client.searchResources('Patient', '_count=1000'),
    client.searchResources('CareTeam', '_count=1000'),
  ])
  
  patients.map((patient) => {
    const pid = patient.id
    const careTeam = careTeams.find(careTeam =>
      careTeams.reference === `Patient/${patient.id}`
    )
    records.set(pid, { patient, careTeam });
  });
  
  return { records };
I'm wondering though is on the backend access policies are applied after resources are found. That is, whether all matching records are found and then the filter is applied in such a way that we end up essentially scanning all records and having bad performance. I imagine this isn't the case, but wanting to check before banking on this. Thank you!
t
Hi @rabbit_rabbit , My understanding is that access policies are applied during the sql query. Meaning it will not impact the number of resources found (ie if the first 1000 resources are not available to your using your access poilcy you will still have access to the 1000 next). Also I would consider using graphql or _include to fetch all datas in one time https://www.medplum.com/docs/search/includes
r
Thank you @thomabig - I figured as much, but wanted to confirm before banking fully on that. Cheers!
9 Views