Optimizing your mapfile for Mapserver

There are many tricks to optimize your mapfile.  After many years to build mapfiles for Mapserver, I learned many trick to optimise a mapfile.  I try here to highlight some of them:
  1. Manage your EPSG file if you specify espg code.  Only keep in your file projections used in your systems.  The original file have approx 545k and Mapserver read this file each time you add in your mapfile instruction like  "init=epsg:32198".  You can reduce it to 4k. Or, you can put the projections specify in your mapfile at the top of your epsg file...  so Mapserver don't need to scan the whole epsg file.
  2. Lowercase epsg intruction like "init=epsg:32198".  Mapserver use C function "strcmp" in lowercase first.
  3. Try to be "brief"... Don't put extra default instruction like STATUS ON or LABELCACHE ON... STATUS and LABELCACHE are ON by default.  The less you have to read, faster you are. And try to limit your comment to...
  4. In the layer definition the tag EXPRESSION can be very costly.  Alway try to use "regular expression"(REGEX) it's much faster!  If you add something like that EXPRESSION (("[DESCRIPTION]" eq "Park") OR ("[DESCRIPTION]" eq "Base") Mapserver gona built sql string then gona filtre your data! It work but it's not optimize.  In this example we just put directly in the mapfile EXPRESSION /^Park$|^Base$/ and Mapserver use it without any string manipulation.  And change this EXPRESSION ("[DESCRIPTION]" eq "Park") to this EXPRESSION "Park"!
  5. Try not to re-project your data it's costly overhead.
  6. Shapefile format ALWAYS de faster way to publish data.
  7. Shptree each of your shapefile AND don't specify shp in DATA tag.  This one is very peculiar one! If you put DATA like this DATA "/srv/data/park.shp" Mapserver use a built in reader and don't use index qix file.  But if you type DATA "/srv/data/park" Mapserver gona use OGR to read data and OGR alway use qix file!
  8. For large shapefiles, use tile4ms program to tile them.  Follow those instruction to split your shapefile.  This trick is very very usefull
  9. If you use PostGIS or Oracle Data, try to connect to only one server and keep your connection open with this instruction in each layer PROCESSING "CLOSE_CONNECTION=DEFER"
  10. Never use SIZEUNITS in the LAYER tag.  Put it in the MAP tag.
  11. If you build a mapfile for a Web map service(WMS) always add STATUS OFF in each layer.  In this way Mapserver don't prepared all cartographics parametres like labeling engine.
  12. Finally, use debug option to find out witch layers are to slow.  The best trick her is to use shp2img program to eliminate overhead of your network.  To use debug option you have to insert in the MAP tag this CONFIG MS_ERRORFILE "/srv/log/debug.log" and in each layer this tag DEBUG 5  aIn the log file check the msDrawMap for each layer.
Shp2imp example on Windows shp2img -m C:Testimg2.map -l png -o C:Testext.png -e 219589 5347860 226045 5353101 Log example [Fri Feb 15 16:05:14 2008].742000 msDrawRasterLayerLow(tif): entering. [Fri Feb 15 16:05:14 2008].762000 msDrawGDAL(): src=0,0,33477,25307, dst=0,26,935,707 [Fri Feb 15 16:05:14 2008].762000 msDrawGDAL(): red,green,blue,alpha bands = 1,2,3,0 [Fri Feb 15 16:05:15 2008].2000 msDrawMap(): Layer 2 (tif), 0.260sp