domains / / langfuse/langfuse
Ensure deterministic query results
Always ensure database queries produce deterministic results by including explicit ORDER BY clauses when using LIMIT operations. This prevents unpredictable result ordering and potential data inconsistencies.
Always ensure database queries produce deterministic results by including explicit ORDER BY clauses when using LIMIT operations. This prevents unpredictable result ordering and potential data inconsistencies.
Key practices:
- Include ORDER BY with meaningful columns for result ordering
- Document any assumptions about result ordering
- Consider performance implications of ordering choices
Example:
-- Incorrect: Non-deterministic ordering
SELECT *
FROM traces
WHERE project_id = ${projectId}
LIMIT 1;
-- Correct: Deterministic ordering
SELECT *
FROM traces
WHERE project_id = ${projectId}
ORDER BY event_ts DESC, id
LIMIT 1;
This practice is especially important when:
- Paginating results
- Selecting latest/newest records
- Using LIMIT with GROUP BY
- Implementing skip indexes