Performance
The SRS engine generates and executes SQL queries within the database. To optimize performance, consider the following recommendations:
1. Validate and prepare data with srssetup
Use srssetup to validate input, check business logic, and prepare data before running SRS queries.
2. Use proper parameter types
Ensure that parameter types match the expected data type in the database.
Example: defining a uniqueidentifier parameter
| XML | |
|---|---|
See param for the full list of supported types.
3. Simplify queries and use indexes
- Keep queries simple to enhance readability and performance.
- Preprocess parameters when applicable.
- Add indexes on the columns used in joins and filters.
Example: preprocessing parameters
Before:
| XML | |
|---|---|
After:
| XML | |
|---|---|
4. Avoid casting between data types
Prevent performance issues by avoiding unnecessary casting. For example, in SQL Server:
- Default text parameter type:
nvarchar - Avoid casting between
nvarcharandvarchar.
To specify varchar, use type="ansi" in your parameter definition.
5. Optimize queries with SRS engine hints
For complex queries with numerous joins, subqueries, or nullable parameters, the SQL Server optimizer may generate inefficient plans. Use the SRS engine hint /*jq-comment-if-null*/ to dynamically comment out null parameters.
Example: optimized query
Before:
| SQL | |
|---|---|
After:
| SQL | |
|---|---|
Result (when all parameters are null):
| SQL | |
|---|---|
How it works
- The SRS engine checks the parameters passed.
- If a parameter is
null, the corresponding line is commented out. - Only the first parameter in a line is validated for nullability.
- The hint works for SQL text commands, not for stored procedures.
6. Run commands in parallel
When an SRS has more than one SQL Server command, the commands run in parallel. The total time is close to the slowest command, not the sum of all commands.
- A command with
link="self"waits until all previous commands finish. opts="jq-SkipOptimization"on thesrstag runs commands one by one, in the order of appearance. Use it only when one command depends on the side effects of another.
7. Cache results
Set cache on the srs tag to keep the result in memory for the given number of minutes.
| XML | |
|---|---|
- Only
GETrequests are served from the cache. The cache key includes parameters,pscope,poptsandpcolumns. - A
POSTexecutes the SRS again and refreshes the cache. - The response gets
Cache-Control: private, max-agefor the same time.
Warning
The cached result is shared by all users with access to the SRS. Do not use cache on an SRS whose result depends on the user, for example through column acl, dtacl or [[kv.v.user.*]] variables.
Adopting these practices ensures efficient and maintainable SQL query execution in your SRS engine.