Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle query join

Posted on 2014-03-25
4
Medium Priority
?
697 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
[X]
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
4 Comments
 
LVL 35

Accepted Solution

by:
johnsone earned 2000 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 -> 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
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.
0
 
LVL 49

Expert Comment

by:PortletPaul
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:

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 35

Expert Comment

by:johnsone
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
What we learned in Webroot's webinar on multi-vector protection.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

730 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