Sunteți pe pagina 1din 53

: How do I tell?

A: SELECT * FROM T WHERE g.STIntersects(@x) = 1


SELECT *
FROM T WITH(INDEX(T_g_idx))
WHERE g.STIntersects(@x) = 1
Plan choice is cost-based
QO uses various information, including cardinality
DECLARE @x geometry = 'POINT (0 0)'
SELECT *
FROM T
WHERE T.g.STIntersects(@x) = 1

When can we estimate cardinality?


Variables: never
Plan choice is cost-based
QO uses various information, including cardinality
SELECT *
FROM T
WHERE T.g.STIntersects('POINT (0 0)') = 1

When can we estimate cardinality?


Variables: never
Literals: not for spatial since they are not literals under the covers
Plan choice is cost-based
QO uses various information, including cardinality
EXEC sp_executesql
N'SELECT *
FROM T
WHERE T.g.STIntersects(@x) = 1',
N'@x geometry', N'POINT (0 0)'

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
C
D A B B
D A B A
Primary Filter Secondary Filter
E (Index lookup) (Original predicate)

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
Primary
Secondary
Filter
Indexing Filter
Phase
1 2 15 16
1.
4 3 14 13

5 8 9 12
3.
6 7 10 11
2.

5. Apply
1.
2.
3.
4. Overlay
Identify
Intersecting
actual
grids
a grid
grids
CLR
for
onquery
spatial
method
the
identifies
spatial
object(s)
object
oncandidates
object
candidates
to store to find
in index
matches
/4/2/3/1

/
(“cell 0”)

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-per-object Optimization: User restricts max number of cells per object
0 – cell at least
Spatial
touches
Reference
the object
ID (but not 1 or 2)
Varbinary(5)
1 – guarantee Have
thatto
objectencoding
be the
partially
same to covers
produce
cell
15 columns and 2895 byteofcovers
– object limitation
grid
matchcell
cell id

Prim_key geography Prim_key cell_id srid cell_attr


1 0x00007 42 0
1 g1
3 0x00007 42 1
2 g2 3 0x0000A 42 2

3 g3 3 0x0000B 42 0
3 0x0000C 42 1
Base Table T 1 0x0000D 42 0
2 0x00014 42 1

CREATE SPATIAL INDEX sixd Internal Table for sixd


ON T(geography)
Geometry:
STIntersects() = 1 •

STOverlaps() = 1

STEquals()= 1 •
STTouches() = 1 •

STWithin() = 1

STContains() = 1 •
STDistance() < val •
STDistance() <= val
Nearest Neighbor
Filter() = 1
Itself
Ancestors
Descendants
7 7.2 7.2.4

Spatial Index S
Optional Sort Remove dup ranges
T(@g)

Ranges
Spatial Index Seek
Fully contained
cells
Partially
contained cells
Optimal value (theoretical) is somewhere
between two extremes

Time needed to
Default values: process false positives
512 - Geometry AUTO grid
768 - Geography AUTO grid
1024 - MANUAL grids
SELECT * FROM table t WITH (SPATIAL_WINDOW_MAX_CELLS=256)
WHERE t.geom.STIntersects(@window)=1;
Give me the closest 5 Italian restaurants

SQL Server 2008/2008 R2: table scan


SQL Server 2012: uses spatial index

SELECT TOP(5) *
FROM Restaurants r
WHERE r.type = ‘Italian’
AND r.pos.STDistance(@me) IS NOT NULL
ORDER BY r.pos.STDistance(@me)
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
Parameter Type Description
@tabname nvarchar(776) the name of the table for which the index has been specified

@indexname sysname the index name to be investigated


@verboseoutput tinyint 0 core set of properties is reported
1 all properties are being reported
@query_sample geometry A representative query sample that will be used to test the
usefulness of the index. It may be a representative object or a
query window.

Results in property name/value pair table of the format:


PropName: nvarchar(256) PropValue: sql_variant
Parameter Type Description
@tabname nvarchar(776) the name of the table for which the index has been
specified
@indexname sysname the index name to be investigated
@verboseoutput tinyint 0 core set of properties is reported
1 all properties are being reported
@query_sample geography A representative query sample that will be used to
test the usefulness of the index. It may be a
representative object or a query window.

@xml_output xml This is an output parameter that contains the


returned properties in an XML fragment
Property Type Description
Base_Table_Rows Bigint All Number of rows in the base table
Index properties - All index properties: bounding box, grid densities, cell per object
Total_Primary_Index_R Bigint All Number of rows in the index
ows
Total_Primary_Index_P Bigint All Number of pages in the index
ages
Total_Number_Of_Obje Bigint Core Indicates whether the representative query sample falls outside of the
ctCells_In_Level0_For_ bounding box of the geometry index and into the root cell (level 0 cell). This
QuerySample 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 Bigint Core Number of cell instances of indexed objects that are tessellated in level 0.
ctCells_In_Level0_In_I For geometry indexes, this will happen if the bounding box of the index is
ndex 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.
Property Type Description
Number_Of_Rows_Selected_By_Primary bigint Core P = Number of rows selected by the primary filter.
_Filter
Number_Of_Rows_Selected_By_Internal bigint Core S = Number of rows selected by the internal filter. For
_Filter these rows, the secondary filter is not called.

Number_Of_Times_Secondary_Filter_Is_ bigint Core Number of times the secondary filter is called.


Called
Percentage_Of_Rows_NotSelected_By_Pr float Core Suppose there are N rows in the base table, suppose P
imary_Filter are selected by the primary filter. This is (N-P)/N as
percentage.
Percentage_Of_Primary_Filter_Rows_Sel float Core This is S/P as a percentage. The higher the
ected_By_Internal_Filter percentage, the better is the index in avoiding the
more expensive secondary filter.

Number_Of_Rows_Output bigint Core O=Number of rows output by the query.

Internal_Filter_Efficiency float Core This is S/O as a percentage.

Primary_Filter_Efficiency float Core This is O/P as a percentage. The higher the efficiency
is, the less false positives have to be processed by the
secondary filter.
http://sqlblog.com/blo
mrys@microsoft.com gs/michael_rys/

Michael
Rys @SQLServerMike
SQL Server 2012
Eval Copy Hands-On Labs

mva @sqlserver
@TechEd_europe
Get Certified! #msTechEd
http://europe.msteched.com www.microsoft.com/learning

http://microsoft.com/technet http://microsoft.com/msdn
http://europe.msteched.com/sessions

S-ar putea să vă placă și