Skip to content

Commit

Permalink
Increase Speed of Getting Random Rows
Browse files Browse the repository at this point in the history
  • Loading branch information
njfdev committed Aug 20, 2024
1 parent f6c534d commit 4cfe0a8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/app/api/v0/faa/aircraft_model/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export async function GET(req: NextRequest) {
const prisma = new PrismaClient();

// if no specified id, then get a random aircraft model
const count = await prisma.faaAircraftInfo.count();
const count = Number(
(
(await prisma.$queryRaw`SELECT count(*) from "FaaAircraftInfo";`) as any
)[0].count
);
const randomIndex = Math.floor(Math.random() * count);

const aircraft_info = (
Expand Down
5 changes: 4 additions & 1 deletion src/app/api/v0/faa/engine_model/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export async function GET(req: NextRequest) {
const prisma = new PrismaClient();

// if no specified id, then get a random engine model
const count = await prisma.faaEngineInfo.count();
const count = Number(
((await prisma.$queryRaw`SELECT count(*) from "FaaEngineInfo";`) as any)[0]
.count
);
const randomIndex = Math.floor(Math.random() * count);

const engine_info = (
Expand Down
6 changes: 5 additions & 1 deletion src/app/api/v0/faa/registration/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export async function GET(req: NextRequest) {
const prisma = new PrismaClient();

// if no specified id, then get a random registration
const count = await prisma.faaAircraftRegistration.count();
const count = Number(
(
(await prisma.$queryRaw`SELECT count(*) from "FaaAircraftRegistration";`) as any
)[0].count
);
const randomIndex = Math.floor(Math.random() * count);

const registration = (
Expand Down

0 comments on commit 4cfe0a8

Please sign in to comment.