Introduction
When working with large datasets in SAP OData, server-side paging can be a useful technique for improving performance and reducing the amount of data that needs to be transmitted over the network. Two commonly used parameters for server-side paging are
$skip
and
$skiptoken
. While they may seem similar, they serve different purposes and have different use cases.
In this technical blog, we will explore the difference between
$skip
and
$skiptoken
in SAP OData and provide examples of how they can be used in different scenarios. We will also discuss best practices for using these parameters and provide tips for optimizing performance when working with large datasets.
What is $skip
: Please refer to this blog for more details about
$skip
-
Implementing All OData Query/URI Options – Part 2
What is $skiptoken
: Please refer to this blog for more details about
$skiptoken
Implementing All OData Query/URI Options – Part 2
Difference between $skip
and $skiptoken
:
$skip is used to skip a specified number of items in a collection of data before returning the remaining results. For example, if we want to retrieve all employees after skipping the first 10 employees, we would use the following query:
/sap/opu/odata/sap/<service_name>/Employees?$skip=10
This query would skip the first 10 employees and return the remaining employees in the "Employees" entity set.
On the other hand, $skiptoken is used to enable efficient server-side paging of query results when the size of the data set is too large to be retrieved in a single request. When a server returns a limited number of results along with a $skiptoken value, the client can use this token in a subsequent request to retrieve the next set of results. The $skiptoken value serves as an identifier of the last result returned by the server and helps the server to skip to the next set of results efficiently.
For example, suppose we want to retrieve the first 100 orders from an "Orders" entity set that contains 500 orders. We can use the following query:
/sap/opu/odata/sap/<service_name>/Orders?$top=100
This query would retrieve the first 100 orders. However, if we want to retrieve the next 100 orders, we would need to use $skiptoken. When the server returns the first 100 orders, it would also return a $skiptoken value that identifies the last order returned. We can then use this $skiptoken value in a subsequent request to retrieve the next 100 orders:
/sap/opu/odata/sap/<service_name>/Orders?$top=100&$skiptoken=<skiptoken_value>
In this query,
<skiptoken_value>
is the $skiptoken value returned by the server in the previous response. The server would skip the first 100 orders and return the next 100 orders after the order is identified by the $skiptoken value.
Few interesting examples that we could use in an SAP OData technical blog about server-side paging using $skip and $skiptoken:
- Retrieving large amounts of data efficiently: Imagine we have an entity set containing a large number of records, such as a sales order entity set that contains thousands of orders. Instead of retrieving all the orders in a single request, we can use $skip and $skiptoken to retrieve them in smaller, more manageable batches. This can significantly improve performance and reduce the load on the server.
- Implementing infinite scrolling: With server-side paging, we can implement infinite scrolling, where data is loaded dynamically as the user scrolls down a page. This can provide a better user experience and prevent the need to load all the data at once, which can be slow and resource-intensive.
- Combining $skip and $filter to retrieve specific data: We can combine $skip and $filter to retrieve specific data from an entity set. For example, if we have an employee entity set and we want to retrieve all the employees in the sales department after skipping the first 10 employees, we can use the following query:
/sap/opu/odata/sap/<service_name>/Employees?$skip=10&$filter=Department eq 'Sales'
- Using $skiptoken with SAP Gateway: SAP Gateway provides built-in support for $skiptoken, allowing us to easily implement server-side paging in our OData services. We can use the
go_paging
method in our Gateway class to handle $skiptoken values and efficiently retrieve large amounts of data.
Conclusion:
In conclusion, understanding the difference between
$skip
and
$skiptoken
in SAP OData is crucial when working with large datasets. By using these parameters effectively, developers can improve the performance and scalability of their OData services and provide a better user experience for their customers. By following best practices and optimizing their queries, developers can ensure that their OData services are performing at their best and delivering value to their users.
If you found this post helpful, please like and share it with your network 🙂
Kind Regards,
Nitin Sharma