Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support WFS Paging at database level for MSSQL Driver #5842

geographika opened this issue Jul 31, 2019 · 1 comment

Support WFS Paging at database level for MSSQL Driver #5842

geographika opened this issue Jul 31, 2019 · 1 comment


Copy link

geographika commented Jul 31, 2019

Database Paging

WFS requests support a STARTINDEX parameter which allows results to be paged through in conjunction with MAXFEATURES.

A sample request would look as follows:

mapserv -nh ""

The PostGIS driver supports paging at the database level. Other drivers fetch all the results and then MapServer carries out the paging in mapquery.c. Supporting drivers implement layer->vtable->LayerGetPaging.

Allowing WFS Paging at database level for the MSSQL Driver would greatly improve performance when paging through results.

MS SQL 2012+ can simulate paging in SQL with OFFSET and FETCH. A fallback for older versions (and for Azure hosted SQL Server) is RowNumber. The latter approach may be prereable as it is more universal.

Performance Issues

The lack of paging causes a huge overhead and performance hit when providing &startIndex=0&count=20. The database runs the full SQL query for the layer (with no paging) 3 times when running a WFS 2.0.0 GetFeature request:

  1. In mapwfs.c with the msWFSRetrieveFeatures function (and then msLayerWhichShapes in mapquery.c (with no paging so the full query is run).

  2. In mapwfs.c - msWFSComputeMatchingFeatures which again calls msWFSRetrieveFeatures and then LayerDefaultGetShapeCount in maplayer.c which runs the full query again.
    The MSSQL has the LayerGetShapeCount set to use the default rather than a database optimised SELECT COUNT(*) FROM.... This function does exist however in the driver as msMSSQL2008LayerGetNumFeatures and is set to: layer->vtable->LayerGetNumFeatures

Could this be used rather than LayerDefaultGetShapeCount?

  1. Finally the query is reissued in msMSSQL2008LayerGetShape. msOGRWriteFromQuery in mapogroutput calls msLayerGetShape which then due to the following in the MSSQL driver causes another full query:
// variables are as follows
// resultindex = 0
// layerinfo->row_num = 6607 // all features in table

    if( resultindex < layerinfo->row_num) {
      /* re-issue the query */

SQL Profiler for a single GetFeature request below:


@szekerest szekerest self-assigned this Aug 25, 2019
szekerest added a commit to szekerest/mapserver that referenced this issue Jan 12, 2020
szekerest added a commit that referenced this issue Jan 17, 2020
* MSSQL: Implement server side paging support (#5842)

* MSSQL: Fix paging query problem if sortBy is not specified (#5842)

* Update mapmssql2008.c

Co-Authored-By: Even Rouault <>

* Update mapmssql2008.c

Co-Authored-By: Even Rouault <>

* Apply simplifications in USE_ICONV conditions

* Remove USE_PROJ condition

Co-authored-by: Even Rouault <>
Copy link
Member Author

geographika commented May 18, 2020

Closed with 8ef65a2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

2 participants