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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 35

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 49

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 35

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

623 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