measure queries

This commit is contained in:
Cat /dev/Nulo 2024-09-16 11:25:01 -03:00
parent d5ad504dc6
commit 8f573150c4
3 changed files with 44 additions and 9 deletions

View file

@ -1,13 +1,27 @@
import { sql } from '$lib/server/db';
import type { PageServerLoad } from './$types';
import * as Sentry from '@sentry/sveltekit';
export const load: PageServerLoad = async ({ setHeaders }) => {
// https://www.cybertec-postgresql.com/en/postgresql-count-made-fast/
const count = await sql`
const q = sql`
SELECT reltuples::bigint
FROM pg_catalog.pg_class
WHERE relname = 'precios';
`;
// https://github.com/getsentry/sentry-javascript/discussions/8117#discussioncomment-7623605
const describe = await q.describe();
const count = await Sentry.startSpan(
{
op: 'db.query',
name: describe.string,
data: { 'db.system': 'postgresql' }
// these properties are important if you want to utilize Queries Performance
// read more: https://docs.sentry.io/product/performance/queries/#span-eligibility
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
() => q
);
setHeaders({
'Cache-Control': 'public, max-age=600'

View file

@ -3,9 +3,10 @@ import type { PageServerLoad } from './$types';
import { datasets, precios, sucursales } from '$lib/server/db/schema';
import { and, eq, sql } from 'drizzle-orm';
import { error } from '@sveltejs/kit';
import * as Sentry from '@sentry/sveltekit';
export const load: PageServerLoad = async ({ params, setHeaders }) => {
const id = BigInt(params.id);
const preciosRes = await db
const preciosQuery = db
.select({
id_comercio: precios.id_comercio,
id_bandera: precios.id_bandera,
@ -47,6 +48,15 @@ ORDER BY d1.id_comercio)
)
)
.leftJoin(datasets, eq(datasets.id, precios.id_dataset));
const preciosRes = await Sentry.startSpan(
{
op: 'db.query',
name: preciosQuery.toSQL().sql,
data: { 'db.system': 'postgresql' }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
() => preciosQuery
);
setHeaders({
'Cache-Control': 'public, max-age=600'

View file

@ -1,6 +1,7 @@
import { db } from '$lib/server/db';
import { sql } from 'drizzle-orm';
import type { PageServerLoad } from './$types';
import * as Sentry from '@sentry/sveltekit';
export const load: PageServerLoad = async ({ params, setHeaders }) => {
// const latestDatasetsSq = db.$with('latest_datasets').as(
@ -23,12 +24,7 @@ export const load: PageServerLoad = async ({ params, setHeaders }) => {
.replaceAll(/ó/giu, 'o')
.replaceAll(/ú/giu, 'u')
.replaceAll(/ñ/giu, 'n');
const productos = await db.execute<{
id_producto: string;
productos_descripcion: string;
productos_marca: string | null;
in_datasets_count: number;
}>(sql`
const productosQuery = sql`
SELECT id_producto, productos_descripcion, productos_marca,
(WITH latest_datasets AS (
SELECT d1.id
@ -46,7 +42,22 @@ WHERE p.id_producto = index.id_producto) as in_datasets_count
WHERE productos_descripcion ILIKE ${`%${query}%`}
ORDER BY in_datasets_count desc
LIMIT 100
`);
`;
const productos = await Sentry.startSpan(
{
op: 'db.query',
name: productosQuery,
data: { 'db.system': 'postgresql' }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
() =>
db.execute<{
id_producto: string;
productos_descripcion: string;
productos_marca: string | null;
in_datasets_count: number;
}>(productosQuery)
);
const collapsedProductos = productos.reduce(
(acc, producto) => {
const existingProduct = acc.find((p) => p.id_producto === producto.id_producto);