Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 701
  • Last Modified:

Oracle query join

I have two Oracle tables T1 and T2. T2 is a small table and T1 is a huge table (10million records). I need an output from these two tables that will give me the content of T1 along with an additional field of storeID (from T2 table). The output will show the storeID value for which the distance between the user and the store is the smallest.

T1 (around 10million records)
userID      Lat1                Long1
user1       -118.4396579      46.60330083
user2      -74.75392005      27.7732414
user3      -103.795917      40.29134354
user4      -107.5602164      41.91388636


T2 (around 1000 records)
storeID                Lat2                Long2
store1              41.701433      -88.124812
store2             41.601514      -87.844033
store3             42.267606      -88.991254
store4             42.055439      -87.838962
store5              42.01178916      -87.71209985
store6             37.734759      -89.187377
 

Output
userID       Lat1                 Long1            storeID
user1        -118.4396579      46.60330083   store4
user2      -74.75392005      27.7732414    store1
user3      -103.795917      40.29134354   store1
user4      -107.5602164      41.91388636   store5

The distance between the store and the user is calculated by the formula below:

((ACOS(SIN(Lat1 * PI() / 180) * SIN((Lat2) * PI() / 180) + COS(Long1 * PI() / 180) * COS((Lat2) * PI() / 180) * COS((Long1 - (Long2)) * PI() / 180)) * 180 / PI()) * 60 * 1.1515)  AS Distance

They way I need to write the query, I cannot write a function or so. I could use any complex queries (inner,joins,analytic queries,etc) to maximize
the performance of the query.
Thanks a lot for your suggestion and help.
0
toooki
Asked:
toooki
  • 2
1 Solution
 
johnsoneSenior Oracle DBACommented:
I am having trouble with your formula.  Somewhere it comes out with a negative number that you cannot apply one of the functions to.

I used the formula that I found in this thread -> https://community.oracle.com/thread/2569842?start=0&tstart=0

Using that formula, I come up with this query:

SELECT userid, 
       storeid, 
       lat1, 
       long1, 
       distance 
FROM   (SELECT userid, 
               storeid, 
               lat1, 
               long1, 
               distance, 
               Row_number() 
                 over ( 
                   PARTITION BY userid 
                   ORDER BY distance) rn 
        FROM   (WITH t 
                     AS (SELECT userid, 
                                storeid, 
                                lat1, 
                                long1, 
                                Power (Sin (Abs (lat2 - lat1) * ( 
                                            ( 22 / 7 ) / 180 / 2 
                                                                )), 2) + 
                                Cos ( 
                                lat1 * ( ( 22 / 7 ) / 180 )) * 
                                Cos ( 
                                lat2 * ( 
                                                               ( 22 / 7 ) / 180 
                                       )) 
                                * 
                                Power 
                                ( 
                                Sin ( 
                                Abs (long2 - ( long1 )) * 
                                ( ( 22 / 7 ) / 180 / 2 )), 2) 
                                E2 
                         FROM   t1, 
                                t2) 
                SELECT userid, 
                       storeid, 
                       lat1, 
                       long1, 
                       ( 2 * Atan2 (Sqrt (( 1 - e2 )), Sqrt (e2)) ) * 6371 AS 
                       distance 
                 FROM   t) q) r 
WHERE  rn = 1; 

Open in new window


That produces different results than your query:

USERID  STOREID LAT1            LONG1           DISTANCE
user1   store4  -118.4396579    46.60330083     7791.933022427706
user2   store3  -74.75392005    27.7732414      4730.200333106249
user3   store3  -103.795917     40.29134354     6364.714556563739
user4   store4  -107.5602164    41.91388636     6708.594551858386

Open in new window


If you need a different formula, that should be the basic query that you need to plug it into.
0
 
toookiAuthor Commented:
Thanks a lot. I am trying this one. I have one question. This part of the query:
FROM   t1, t2 means Cartesian product. Is there any way to improve the query at this part? My query is taking long and is not coming back in 20minutes.
Thank you.
0
 
PortletPaulCommented:
"FROM   t1, t2"  can this be improved?

well in syntax terms it can be improved by expressly using a cross join this way we know the Cartesian product is deliberate:

FROM t1
CROSS JOIN t2

But to improve performance all I can ask is, do you need every client distance from every store? If you do then you need the cross join.

Perhaps you could provide the explain plan for your query, this might reveal some things that can be done for performance.
0
 
johnsoneSenior Oracle DBACommented:
How can you determine which store is closest without looking at every store?  There may be something you can look at, but I don't know what it is.  There also may be some functions available in the Spatial package that could help, but I don't know them and there may be an associated cost.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now