ref_ queries are one of those ServiceNow features that are useful, slightly obscure, and easy to misuse.
They show up when you start from a base table such as task, but need to filter on a field that exists only on a child table such as incident.caller_id.
Querying tables is one of those things that looks harmless until the same script runs against production-sized data. A query that feels fine with a few hundred records can become slow, expensive, and noisy when the table grows.
These are the habits I try to keep in every ServiceNow script that reads data.
1. Filter in the query, not in the loop The most common mistake is querying a broad set of records and then doing the real filtering inside while (gr.next()).