@@ -251,9 +251,22 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
251
251
- Verify totals match expected counts
252
252
- Use appropriate DISTINCT counts
253
253
- Document aggregation level in comments
254
+ 14. ALWAYS use simple queries when possible
255
+ 15. AVOID JOINs unless necessary
256
+ 16. USE indexes (primary keys) when available
254
257
255
258
PATTERN TEMPLATES (adapt to actual schema):
256
- 1. Basic Counts with Existence Check:
259
+ 1. Simple Aggregation Pattern:
260
+ SELECT
261
+ COUNT(*) as record_count,
262
+ ROUND(
263
+ SUM(CAST(numeric_field AS NUMERIC)),
264
+ 2
265
+ ) as total_value
266
+ FROM main_table
267
+ WHERE condition; -- Optional
268
+
269
+ 2. Basic Counts with Existence Check:
257
270
WITH base_counts AS (
258
271
SELECT
259
272
t1.id, -- Replace with actual primary key
@@ -268,7 +281,7 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
268
281
SUM(has_related) as related_count
269
282
FROM base_counts;
270
283
271
- 2 . Segmentation with Totals:
284
+ 3 . Segmentation with Totals:
272
285
WITH base_data AS (
273
286
SELECT
274
287
t1.id,
@@ -310,7 +323,7 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
310
323
SELECT * FROM totals
311
324
ORDER BY result_type DESC, segment;
312
325
313
- 3 . Correlation Analysis:
326
+ 4 . Correlation Analysis:
314
327
WITH base_data AS (
315
328
SELECT
316
329
t1.id,
@@ -352,7 +365,7 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
352
365
FROM segment_data
353
366
GROUP BY segment;
354
367
355
- 4 . Percentile-based Segmentation:
368
+ 5 . Percentile-based Segmentation:
356
369
WITH base_data AS (
357
370
SELECT
358
371
t1.*,
@@ -373,7 +386,7 @@ async function generateSqlQuery(apiKey: string, schemaInfo: string, question: st
373
386
SELECT * FROM segment_metrics
374
387
ORDER BY segment;
375
388
376
- 5 . Multi-Level Aggregation Pattern:
389
+ 6 . Multi-Level Aggregation Pattern:
377
390
WITH base_aggregates AS (
378
391
SELECT
379
392
t1.primary_key, -- Replace with actual PK
@@ -565,6 +578,14 @@ function formatQueryResponse(sqlQuery: string): string {
565
578
* )
566
579
* SELECT COUNT(*) FROM order_metrics
567
580
*
581
+ * 8. "Overloaded Error"
582
+ * Problem: Query too complex or taking too long
583
+ * Solution:
584
+ * - Use simpler queries for basic aggregations
585
+ * - Add timeout handling
586
+ * - Include simple aggregation pattern
587
+ * - Avoid unnecessary JOINs for simple calculations
588
+ *
568
589
* IMPLEMENTATION REQUIREMENTS:
569
590
* 1. Schema Awareness
570
591
* - All queries must be built using actual schema information
0 commit comments