Error Reference

Slow query detected: database query exceeded performance threshold

Slow query detected: database query exceeded performance threshold

Where this appears

This error appears in the System Log under System Diagnostics > Log > Application Log when a GlideRecord query or database operation takes longer than the configured slow query threshold (typically 3-5 seconds). It commonly occurs during Business Rule execution, Script Include operations, or when processing large data sets in scheduled jobs.

Sourdough
Chrome Extension

Debugging this in a real instance?

Sourdough lets you trace "Slow query detected: database query exceeded performance threshold" through system logs, script errors, and API health in the ServiceNow tab you are already using.

Add to Chrome

Free to install. Pro $5/month after a 14-day no-card trial.
Pro requires the ServiceNow admin role. Upgrade inside the extension.

Overview
Tasks
CMDB
API
Metrics
Monitor
Internals
Instance:sourdoughdev·Version:Yokohama
Instance StateONLINE
System StatusFully Operational
Session Timeout90 minutes
Logged-In Sessions2 (20 active)
Build Nameyokohama-12-18-2024_p1
IP Address10.159.128.43
Instance HealthHealth Score: 90%
🔥 5dSourdough (Chrome Plugin)Dark Mode

Cause

Missing database indexes on frequently queried fields, causing full table scans on large tables with thousands of records. (high likelihood)

Unoptimized GlideRecord queries using encoded queries with OR conditions, LIKE operators, or queries on non-indexed reference fields. (high likelihood)

Large table queries without proper query limits or pagination, attempting to retrieve too many records at once. (medium likelihood)

Complex joins or dot-walking through multiple reference fields in a single query operation. (medium likelihood)

Database fragmentation or outdated table statistics affecting query execution plans and performance. (low likelihood)

Fix

Step 1: Identify the slow query by checking System Log at System Diagnostics > Log > Application Log and locate the specific table and query conditions mentioned in the error details.

Step 2: Navigate to System Definition > Tables & Columns and review the table schema to identify if database indexes exist for the queried fields.

Step 3: Check the query execution plan by enabling query statistics via the system property 'glide.db.query_log.enabled' and reviewing the generated logs for table scan operations.

Step 4: Create missing database indexes by navigating to System Definition > Database Indexes and adding indexes for frequently queried fields, especially those used in WHERE clauses.

Step 5: Optimize the GlideRecord query by adding query limits using setLimit(), replacing OR conditions with IN operators, and avoiding LIKE queries on large text fields.

Step 6: Implement pagination for large result sets by using chooseWindow() method or processing records in smaller batches with appropriate query limits.

Step 7: Test the optimized query performance using the Scripts - Background module to measure execution time and verify the query no longer exceeds the threshold.

Step 8: Monitor the System Log for 24-48 hours after implementing changes to confirm the slow query error no longer occurs for the optimized operations.

Test Your Knowledge

Quick 3-question quiz. See how your ServiceNow skills stack up.

Question 1 of 3Performance

A list view on a table with millions of records is slow. Best fix?

Select an answer to continue