Spatial Queries Nearest Neighbor and Join Queries Spatial
Spatial Queries Nearest Neighbor and Join Queries
Spatial Queries n n Given a collection of geometric objects (points, lines, polygons, . . . ) organize them on disk, to answer efficiently n point queries n range queries n k-nn queries n spatial joins (‘all pairs’ queries)
Spatial Queries n n Given a collection of geometric objects (points, lines, polygons, . . . ) organize them on disk, to answer n point queries n range queries n k-nn queries n spatial joins (‘all pairs’ queries)
Spatial Queries n n Given a collection of geometric objects (points, lines, polygons, . . . ) organize them on disk, to answer n point queries n range queries n k-nn queries n spatial joins (‘all pairs’ queries)
Spatial Queries n n Given a collection of geometric objects (points, lines, polygons, . . . ) organize them on disk, to answer n point queries n range queries n k-nn queries n spatial joins (‘all pairs’ queries)
Spatial Queries n n Given a collection of geometric objects (points, lines, polygons, . . . ) organize them on disk, to answer n point queries n range queries n k-nn queries n spatial joins (‘all pairs’ queries)
R-tree … 2 5 7 3 8 4 6 11 10 9 2 12 13 3 1 1
R-trees - Range search pseudocode: check the root for each branch, if its MBR intersects the query rectangle apply range-search (or print out, if this is a leaf)
R-trees - NN search P 1 P 3 AC F B q P 2 D E I G H P 4 J
R-trees - NN search n Q: How? (find near neighbor; refine. . . ) P 1 P 3 AC F B q P 2 D E I G H P 4 J
R-trees - NN search n A 1: depth-first search; then range query P 1 AC F B q P 2 D I P 3 E G H P 4 J
R-trees - NN search n A 1: depth-first search; then range query P 1 P 3 AC F B q P 2 D E I G H P 4 J
R-trees - NN search n A 1: depth-first search; then range query P 1 P 3 AC F B q P 2 D E I G H P 4 J
R-trees - NN search: Branch and Bound n n A 2: [Roussopoulos+, sigmod 95]: n At each node, priority queue, with promising MBRs, and their best and worst-case distance main idea: Every side (face) of any MBR contains at least one point of an actual spatial object!
MBR face property n n MBR is a d-dimensional rectangle, which is the minimal rectangle that fully encloses (bounds) an object (or a set of objects) MBR f. p. : Every face of the MBR contains at least one point of some object in the database
Search improvement n Visit an MBR (node) only when necessary n How to do pruning? Using MINDIST and MINMAXDIST
MINDIST n n n MINDIST(P, R) is the minimum distance between a point P and a rectangle R If the point is inside R, then MINDIST=0 If P is outside of R, MINDIST is the distance of P to the closest point of R (one point of the perimeter)
MINDIST computation n MINDIST(p, R) is the minimum distance between p and R with corner points l and u n the closest point in R is at least this distance away R u=(u 1, u 2, …, ud) u p MINDIST = 0 ri = li if pi < li = ui if pi > ui = pi otherwise p l l=(l 1, l 2, …, ld) p
MINMAXDIST n n n MINMAXDIST(p, R): for each dimension, find the closest face, compute the distance to the furthest point on this face and take the minimum of all these (d) distances MINMAXDIST(p, R) is the smallest possible upper bound of distances from p to R MINMAXDIST guarantees that there is at least one object in R with a distance to p smaller or equal to it.
MINDIST and MINMAXDIST n MINDIST(p, R) <= NN(p) <=MINMAXDIST(p, R) R 1 MINMAXDIST R 4 R 3 MINDIST MINMAXDIST R 2 MINDIST
Pruning in NN search n n n Downward pruning: An MBR R is discarded if there exists another R’ s. t. MINDIST(p, R)>MINMAXDIST(p, R’) Downward pruning: An object O is discarded if there exists an R s. t. the Actual-Dist(p, O) > MINMAXDIST(p, R) Upward pruning: An MBR R is discarded if an object O is found s. t. the MINDIST(p, R) > Actual-Dist(p, O)
Pruning 1 example n Downward pruning: An MBR R is discarded if there exists another R’ s. t. MINDIST(p, R)>MINMAXDIST(p, R’) R R’ MINDIST MINMAXDIST
Pruning 2 example n Downward pruning: An object O is discarded if there exists an R s. t. the Actual-Dist(p, O) > MINMAXDIST(p, R) O R Actual-Dist MINMAXDIST
Pruning 3 example n Upward pruning: An MBR R is discarded if an object O is found s. t. the MINDIST(p, R) > Actual-Dist(p, O) R MINDIST Actual-Dist O
Ordering Distance n MINDIST is an optimistic distance where MINMAXDIST is a pessimistic one. MINDIST P MINMAXDIST
NN-search Algorithm 1. 2. 3. 4. 5. 6. 7. Initialize the nearest distance as infinite distance Traverse the tree depth-first starting from the root. At each Index node, sort all MBRs using an ordering metric and put them in an Active Branch List (ABL). Apply pruning rules 1 and 2 to ABL Visit the MBRs from the ABL following the order until it is empty If Leaf node, compute actual distances, compare with the best NN so far, update if necessary. At the return from the recursion, use pruning rule 3 When the ABL is empty, the NN search returns.
K-NN search n n Keep the sorted buffer of at most k current nearest neighbors Pruning is done using the k-th distance
Another NN search: Best-First n Global order [HS 99] n n n Maintain distance to all entries in a common Priority Queue Use only MINDIST Repeat n n n Inspect the next MBR in the list Add the children to the list and reorder Until all remaining MBRs can be pruned
Nearest Neighbor Search (NN) with R-Trees Best-first (BF) algorihm: n y axis 8 E 1 e d 6 E 8 E 5 g i h E 9 query point contents omitted E 4 a search region b 0 E 2 f E 6 4 2 Root E 1 1 E 7 10 c E 3 2 4 6 a 5 x axis 8 follow E 6 9 c 18 d 13 e 13 E 2 E 8 E 1 2 E 2 E 4 8 h 2 E 4 E E 4 5 5 E 3 8 E 2 E 7 13 f 10 Result 2 E 3 5 E 5 E 5 5 5 E 3 {empty} 8 5 E 3 8 E 6 i 9 Report h and terminate 9 {empty} E 13 E 17 {empty} 9 7 i 10 E 13 g {empty} 7 9 13 E 10 7 13 g 13 {(h, 2 )} 9 E 8 2 E 9 17 h 2 g 13 E 8 E 5 Heap Visit Root follow E 1 follow E 5 5 E 4 10 Action b 13 E 1 E 4 5 E 2 2 i 10
HS algorithm Initialize PQ (priority queue) Inesrt. Queue(PQ, Root) While not Is. Empty(PQ) R= Dequeue(PQ) If R is an object Report R and exit (done!) If R is a leaf page node For each O in R, compute the Actual-Dists, Insert. Queue(PQ, O) If R is an index node For each MBR C, compute MINDIST, insert into PQ
Best-First vs Branch and Bound n n n Best-First is the “optimal” algorithm in the sense that it visits all the necessary nodes and nothing more! But needs to store a large Priority Queue in main memory. If PQ becomes large, we have thrashing… BB uses small Lists for each node. Also uses MINMAXDIST to prune some entries
Spatial Queries n n Given a collection of geometric objects (points, lines, polygons, . . . ) organize them on disk, to answer n n point queries range queries k-nn queries spatial joins (‘all pairs’ queries)
Spatial Join n Find all parks in each city in MA Find all trails that go through a forest in MA Basic operation n n Single-scan queries n n find all pairs of objects that overlap nearest neighbor queries, range queries Multiple-scan queries n spatial join
Algorithms n No existing index structures n Transform data into 1 -d space [O 89] n n Partition-based spatial-merge join [PW 96] n n n z-transform; sensitive to size of pixel partition into tiles that can fit into memory plane sweep algorithm on tiles Spatial hash joins [LR 96, KS 97] Sort data using recursive partitioning [BBKK 01] With index structures [BKS 93, HJR 97] n n k-d trees and grid files R-trees
R-tree based Join [BKS 93] S R
Join 1(R, S) Tree synchronized traversal algorithm n Join 1(R, S) Repeat Find a pair of intersecting entries E in R and F in S If R and S are leaf pages then add (E, F) to result-set Else Join 1(E, F) Until all pairs are examined CPU and I/O bottleneck n n R S
CPU – Time Tuning n Two ways to improve CPU – time n Restricting the search space n Spatial sorting and plane sweep
Reducing CPU bottleneck S R
Join 2(R, S, Intersected. Vol) Join 2(R, S, IV) Repeat Find a pair of intersecting entries E in R and F in S that overlap with IV If R and S are leaf pages then add (E, F) to result-set Else Join 2(E, F, Common. EF) n n n Until all pairs are examined In general, number of comparisons equals n size(R) + size(S) + relevant(R)*relevant(S) Reduce the product term
Restricting the search space Join 1: 7 of R * 7 of S 1 = 49 comparisons 1 5 5 1 3 Now: 3 of R * 2 of S =6 comp Plus Scanning: 7 of R + 7 of S = 14 comp
Using Plane Sweep S R s 1 s 2 r 1 r 2 r 3 Consider the extents along x-axis Start with the first entry r 1 sweep a vertical line
Using Plane Sweep S R s 1 s 2 r 1 r 2 r 3 Check if (r 1, s 1) intersect along y-dimension Add (r 1, s 1) to result set
Using Plane Sweep S R s 1 s 2 r 1 r 2 r 3 Check if (r 1, s 2) intersect along y-dimension Add (r 1, s 2) to result set
Using Plane Sweep S R s 1 s 2 r 1 r 2 r 3 Reached the end of r 1 Start with next entry r 2
Using Plane Sweep S R s 1 s 2 r 1 r 2 r 3 Reposition sweep line
Using Plane Sweep S R s 1 s 2 r 1 r 2 r 3 Check if r 2 and s 1 intersect along y Do not add (r 2, s 1) to result
Using Plane Sweep S R s 1 s 2 r 1 r 2 r 3 Reached the end of r 2 Start with next entry s 1
Using Plane Sweep S R s 1 s 2 r 1 r 2 r 3 Total of 2(r 1) + 1(r 2) + 0 (s 1)+ 1(s 2)+ 0(r 3) = 4 comparisons
I/O Tunning n n Compute a read schedule of the pages to minimize the number of disk accesses n Local optimization policy based on spatial locality Three methods n n n Local plane sweep with pinning Local z-order
Reducing I/O n Plane sweep again: n n n Read schedule r 1, s 2, r 3 Every subtree examined only once Consider a slightly different layout
Reducing I/O S R r 2 s 1 r 1 s 2 r 3 Read schedule is r 1, s 2, r 2, s 1, s 2, r 3 Subtree s 2 is examined twice
Pinning of nodes n After examining a pair (E, F), compute the degree of intersection of each entry n n degree(E) is the number of intersections between E and unprocessed rectangles of the other dataset If the degrees are non-zero, pin the pages of the entry with maximum degree Perform spatial joins for this page Continue with plane sweep
Reducing I/O R r 2 S s 1 r 1 s 2 r 3 After computing join(r 1, s 2), degree(r 1) = 0 degree(s 2) = 1 So, examine s 2 next Read schedule = r 1, s 2, r 3, r 2, s 1 Subtree s 2 examined only once
Local Z-Order n Idea: 1. Compute the intersections between each rectangle of the one node and all rectangles of the other node 2. Sort the rectangles according to the Z-ordering of their centers 3. Use this ordering to fetch pages
Local Z-ordering s 2 r 3 III IV II r 1 s 1 I r 4 I r 2 Read schedule: <s 1, r 2, r 1, s 2, r 4, r 3>
Number of Disk Access 5384 > 5290 Size of LRU Buffer 2373 < 2392
HMW 1 n Problem 1 a) compute the Z-values for the two regions. We can use up to 4 bits per dimension. n b) write some simple code to compute the z-values and Hilbert-values of points in 2 -d. You need to specify the first the resolution (how many bits per dimension) and then the x and y of the point. Also, you need to be able to compute the inverse. Again you specify the resolution and the value (z or Hilbert) and you give back the pixel location. n Problem 2 Example of Skyline points: n Answer set P: {a, i, k)
- Slides: 57