Optimize Data Access and Queries
Query Data with SuiteQL or N/query
If your task, script, or project requires raw data for processing, write a SuiteQL statement or use the N/query module. Avoid using saved searches and N/record loads for raw data, as they create full record objects and may add the unnecessary overhead result of pagination, governance checks, and JSON serialization.
-
Skip the record-load cost with SuiteQL and N/query, as they return lightweight result sets without instantiating record objects.
-
Use query.expression filters to further optimize your N/query statements
-
Use bound parameters to further optimize your SuiteQL statements
-
Use customScriptID for efficient query calls in openSearch
Combine Data into a Single Query
Search and query performance in your script may be improved by retrieving needed data in one big query and processing the results in memory. Running the same or many queries inside a loop may add avoidable overhead and may slow every page that calls your script.
-
Write one SuiteQL or N/query statement that returns all columns and data required for your operation
For example, if you need to process a set of invoices, you could query the entire set of invoice data, package it with N/record, and then iterate through the result set rather than calling N/record for each invoice.
Run One Query from your Client Script
When you run a client script, a browser can only send 4-8 HTTP requests concurrently. Too many N/query calls at the same time will create a queue, extend page-load times, and reduce performance for users. Browsers, not NetSuite, impose the concurrency limit on JavaScript, so even optimized queries stall when you trigger too many from your script.
-
Combine requests whenever practical and write a single SuiteQL or N/query statement that returns every column you need
-
Consider moving data collection to the backend. If you need multiple queries, use a map/reduce script, scheduled script, or RESTlet, then pass the packaged results to the client script.
Warning:Do not replace many small queries with a single statement full of OR clauses or UNION statements if you have multiple queries that cannot be consolidated. This type of combined query may be worse for performance than a few target queries.
Use N/search.lookupFields()
If you need to read or look up a few field values, and you do not need to save the record, use lookupFields(). This lightweight search avoids calling record.load() and record.save for read-only data.