Presentation on theme: "Troubleshooting SQL Server Spatial Query Performance: A Deep Dive into Spatial Indexing Michael Rys Principal Program Manager Microsoft"— Presentation transcript:
Troubleshooting SQL Server Spatial Query Performance: A Deep Dive into Spatial Indexing Michael Rys Principal Program Manager Microsoft DBI405
N O I N D E X I N D E X !
Plan choice is cost-based QO uses various information, including cardinality When can we estimate cardinality? Variables: never Literals: not for spatial since they are not literals under the covers Parameters: yes, but cached, so first call matters geometry = 'POINT (0 0)' SELECT * FROM T WHERE = 1 SELECT * FROM T WHERE T.g.STIntersects('POINT (0 0)') = 1 EXEC sp_executesql N'SELECT * FROM T WHERE = 1', geometry', N'POINT (0 0)'
In general, split predicates in two Primary filter finds all candidates, possibly with false positives (but never false negatives) Secondary filter removes false positives The index provides our primary filter Original predicate is our secondary filter Some tweaks to this scheme Sometimes possible to skip secondary filter AA BB CC DD AA BB DD AABB Primary Filter (Index lookup) Secondary Filter (Original predicate) EE
Overlay a grid on the spatial object2. Identify grids for spatial object to store in index 3. Identify grids for query object(s)4. Intersecting grids identifies candidates Indexing Phase Primary FilterSecondary Filter 5. Apply actual CLR method on candidates to find matches
Deepest-cell Optimization: Only keep the lowest level cell in index Covering Optimization: Only record higher level cells when all lower cells are completely covered by the object / (“cell 0”) Cell-per-object Optimization: User restricts max number of cells per object /4/2/3/1
Prim_keygeography 1g1 2g2 3g3 Prim_keycell_idsridcell_attr 10x x x0000A422 30x0000B420 30x0000C421 10x0000D420 20x Base Table T Internal Table for sixd CREATE SPATIAL INDEX sixd ON T(geography) 0 – cell at least touches the object (but not 1 or 2) 1 – guarantee that object partially covers cell 2 – object covers cell 15 columns and 895 byte limitation Spatial Reference ID Have to be the same to produce match Varbinary(5) encoding of grid cell id
Optimal value (theoretical) is somewhere between two extremes Time needed to process false positives Default values: Geometry AUTO grid Geography AUTO grid MANUAL grids SELECT * FROM table t WITH (SPATIAL_WINDOW_MAX_CELLS=256) WHERE
demo Spatial Query Hinting
demo Spatial Performance in SQL Server 2012
Find the closest 50 business points to a specific location (out of 22 million in total)
sp_spatial_help_geometry_histogram sp_spatial_help_geography_histogram Used for spatial data and index analysis
Arguments Results in property name/value pair table of the format: name of the table for which the index has been index name to be core set of properties is reported 1 all properties are being representative query sample that will be used to test the usefulness of the index. It may be a representative object or a query window. PropName: nvarchar(256)PropValue: sql_variant
name of the table for which the index has been index name to be core set of properties is reported 1 all properties are being representative query sample that will be used to test the usefulness of the index. It may be a representative object or a query is an output parameter that contains the returned properties in an XML fragment
PropertyTypeDescription Base_Table_RowsBigintAllNumber of rows in the base table Index properties-Allindex properties: bounding box, grid densities, cell per object Total_Primary_Index_R ows BigintAllNumber of rows in the index Total_Primary_Index_P ages BigintAllNumber of pages in the index Total_Number_Of_Obje ctCells_In_Level0_For_ QuerySample BigintCoreIndicates whether the representative query sample falls outside of the bounding box of the geometry index and into the root cell (level 0 cell). This is either 0 (not in level 0 cell) or 1. If it is in the level 0 cell, then the investigated index is not an appropriate index for the query sample. Total_Number_Of_Obje ctCells_In_Level0_In_I ndex BigintCoreNumber of cell instances of indexed objects that are tessellated in level 0. For geometry indexes, this will happen if the bounding box of the index is smaller than the data domain. A high number of objects in level 0 may require a costly application of secondary filters if the query window falls partially outside the bounding box. If the query window falls inside the bounding box, having a high number of objects in level 0 may actually improve the performance.
PropertyTypeDescription Number_Of_Rows_Selected_By_Primary _Filter bigintCoreP = Number of rows selected by the primary filter. Number_Of_Rows_Selected_By_Internal _Filter bigintCoreS = Number of rows selected by the internal filter. For these rows, the secondary filter is not called. Number_Of_Times_Secondary_Filter_Is_ Called bigintCoreNumber of times the secondary filter is called. Percentage_Of_Rows_NotSelected_By_Pr imary_Filter floatCoreSuppose there are N rows in the base table, suppose P are selected by the primary filter. This is (N-P)/N as percentage. Percentage_Of_Primary_Filter_Rows_Sel ected_By_Internal_Filter floatCoreThis is S/P as a percentage. The higher the percentage, the better is the index in avoiding the more expensive secondary filter. Number_Of_Rows_OutputbigintCoreO=Number of rows output by the query. Internal_Filter_EfficiencyfloatCoreThis is S/O as a percentage. Primary_Filter_EfficiencyfloatCoreThis is O/P as a percentage. The higher the efficiency is, the less false positives have to be processed by the secondary filter.
Some Spatial Presentations (with further links) and whitepapers: data-queries-in-sql-server-2012.aspx Forum: Blogs: Find Me Later Breakout Sessions DBI324 - Taking SQL Server into the World of Spatial Data Management
#msTechEd mva Microsoft Virtual Academy SQL Server 2012 Eval Copy Get Certified! Hands-On Labs
Connect. Share. Discuss. Learning Microsoft Certification & Training Resources TechNet Resources for IT Professionals Resources for Developers