Oracle query join

Posted on 2014-03-25
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

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.
Question by:toooki
  • 2
LVL 34

Accepted Solution

johnsone earned 500 total points
ID: 39954917
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 ->

Using that formula, I come up with this query:

SELECT userid, 
FROM   (SELECT userid, 
                 over ( 
                   PARTITION BY userid 
                   ORDER BY distance) rn 
        FROM   (WITH t 
                     AS (SELECT userid, 
                                Power (Sin (Abs (lat2 - lat1) * ( 
                                            ( 22 / 7 ) / 180 / 2 
                                                                )), 2) + 
                                Cos ( 
                                lat1 * ( ( 22 / 7 ) / 180 )) * 
                                Cos ( 
                                lat2 * ( 
                                                               ( 22 / 7 ) / 180 
                                Sin ( 
                                Abs (long2 - ( long1 )) * 
                                ( ( 22 / 7 ) / 180 / 2 )), 2) 
                         FROM   t1, 
                SELECT userid, 
                       ( 2 * Atan2 (Sqrt (( 1 - e2 )), Sqrt (e2)) ) * 6371 AS 
                 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.

Author Comment

ID: 39954950
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.
LVL 48

Expert Comment

ID: 39955324
"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:


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.
LVL 34

Expert Comment

ID: 39955658
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.

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

813 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

10 Experts available now in Live!

Get 1:1 Help Now