Solved

Oracle query join

Posted on 2014-03-25
4
673 Views
Last Modified: 2014-04-10
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
Comment
Question by:toooki
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
Comment Utility
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
 

Author Comment

by:toooki
Comment Utility
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
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
"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
 
LVL 34

Expert Comment

by:johnsone
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now