perf(cfdi): optimize page performance
Database optimizations: - Add indexes on fecha_emision, tipo, estado, rfc_emisor, rfc_receptor - Add trigram indexes for fast ILIKE searches on nombre fields - Combine COUNT with main query using window function (1 query instead of 2) Frontend optimizations: - Add 300ms debounce to autocomplete searches - Add staleTime (30s) and gcTime (5min) to useCfdis hook - Reduce unnecessary API calls on every keystroke Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -50,14 +50,9 @@ export async function getCfdis(schema: string, filters: CfdiFilters): Promise<Cf
|
||||
params.push(`%${filters.search}%`);
|
||||
}
|
||||
|
||||
const countResult = await prisma.$queryRawUnsafe<[{ count: number }]>(`
|
||||
SELECT COUNT(*) as count FROM "${schema}".cfdis ${whereClause}
|
||||
`, ...params);
|
||||
|
||||
const total = Number(countResult[0]?.count || 0);
|
||||
|
||||
// Combinar COUNT con la query principal usando window function
|
||||
params.push(limit, offset);
|
||||
const data = await prisma.$queryRawUnsafe<Cfdi[]>(`
|
||||
const dataWithCount = await prisma.$queryRawUnsafe<(Cfdi & { total_count: number })[]>(`
|
||||
SELECT
|
||||
id, uuid_fiscal as "uuidFiscal", tipo, serie, folio,
|
||||
fecha_emision as "fechaEmision", fecha_timbrado as "fechaTimbrado",
|
||||
@@ -68,13 +63,17 @@ export async function getCfdis(schema: string, filters: CfdiFilters): Promise<Cf
|
||||
tipo_cambio as "tipoCambio", metodo_pago as "metodoPago",
|
||||
forma_pago as "formaPago", uso_cfdi as "usoCfdi",
|
||||
estado, xml_url as "xmlUrl", pdf_url as "pdfUrl",
|
||||
created_at as "createdAt"
|
||||
created_at as "createdAt",
|
||||
COUNT(*) OVER() as total_count
|
||||
FROM "${schema}".cfdis
|
||||
${whereClause}
|
||||
ORDER BY fecha_emision DESC
|
||||
LIMIT $${paramIndex++} OFFSET $${paramIndex}
|
||||
`, ...params);
|
||||
|
||||
const total = Number(dataWithCount[0]?.total_count || 0);
|
||||
const data = dataWithCount.map(({ total_count, ...cfdi }) => cfdi) as Cfdi[];
|
||||
|
||||
return {
|
||||
data,
|
||||
total,
|
||||
|
||||
Reference in New Issue
Block a user