mirror of
https://github.com/fosrl/pangolin.git
synced 2026-06-27 17:49:04 +00:00
Update query to be more efficient
This commit is contained in:
@@ -152,34 +152,64 @@ export async function buildClientConfigurationForNewtClient(
|
|||||||
|
|
||||||
const targetsToSend: SubnetProxyTargetV2[] = [];
|
const targetsToSend: SubnetProxyTargetV2[] = [];
|
||||||
|
|
||||||
for (const resource of allSiteResources) {
|
if (allSiteResources.length === 0) {
|
||||||
// Get clients associated with this specific resource
|
return {
|
||||||
const resourceClients = await db
|
peers: validPeers,
|
||||||
.select({
|
targets: targetsToSend
|
||||||
clientId: clients.clientId,
|
};
|
||||||
pubKey: clients.pubKey,
|
}
|
||||||
subnet: clients.subnet
|
|
||||||
})
|
|
||||||
.from(clients)
|
|
||||||
.innerJoin(
|
|
||||||
clientSiteResourcesAssociationsCache,
|
|
||||||
eq(
|
|
||||||
clients.clientId,
|
|
||||||
clientSiteResourcesAssociationsCache.clientId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
clientSiteResourcesAssociationsCache.siteResourceId,
|
|
||||||
resource.siteResourceId
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const resourceTargets = await generateSubnetProxyTargetV2(
|
// Batch fetch all client associations for every site resource in one query
|
||||||
resource,
|
// to avoid an N+1 lookup that would issue thousands of queries when a site
|
||||||
resourceClients
|
// has many resources.
|
||||||
|
const siteResourceIds = allSiteResources.map((r) => r.siteResourceId);
|
||||||
|
|
||||||
|
const resourceClientRows = await db
|
||||||
|
.select({
|
||||||
|
siteResourceId: clientSiteResourcesAssociationsCache.siteResourceId,
|
||||||
|
clientId: clients.clientId,
|
||||||
|
pubKey: clients.pubKey,
|
||||||
|
subnet: clients.subnet
|
||||||
|
})
|
||||||
|
.from(clients)
|
||||||
|
.innerJoin(
|
||||||
|
clientSiteResourcesAssociationsCache,
|
||||||
|
eq(clients.clientId, clientSiteResourcesAssociationsCache.clientId)
|
||||||
|
)
|
||||||
|
.where(
|
||||||
|
inArray(
|
||||||
|
clientSiteResourcesAssociationsCache.siteResourceId,
|
||||||
|
siteResourceIds
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const clientsByResourceId = new Map<
|
||||||
|
number,
|
||||||
|
{ clientId: number; pubKey: string | null; subnet: string | null }[]
|
||||||
|
>();
|
||||||
|
for (const row of resourceClientRows) {
|
||||||
|
let list = clientsByResourceId.get(row.siteResourceId);
|
||||||
|
if (!list) {
|
||||||
|
list = [];
|
||||||
|
clientsByResourceId.set(row.siteResourceId, list);
|
||||||
|
}
|
||||||
|
list.push({
|
||||||
|
clientId: row.clientId,
|
||||||
|
pubKey: row.pubKey,
|
||||||
|
subnet: row.subnet
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const resourceTargetsArr = await Promise.all(
|
||||||
|
allSiteResources.map((resource) =>
|
||||||
|
generateSubnetProxyTargetV2(
|
||||||
|
resource,
|
||||||
|
clientsByResourceId.get(resource.siteResourceId) ?? []
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const resourceTargets of resourceTargetsArr) {
|
||||||
if (resourceTargets) {
|
if (resourceTargets) {
|
||||||
targetsToSend.push(...resourceTargets);
|
targetsToSend.push(...resourceTargets);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user