Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 10th, 2008, 03:37 AM
ganesh_salpure ganesh_salpure is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 7 ganesh_salpure User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 48 m 44 sec
Reputation Power: 0
Table data storage and retrieval optimization

Hello Everybody,

I have a small tricky problem here...need your help.

Let me explain in detail. I have three tables

1. Emp Table: Columns-> EMPID and DeptID
2. Dept Table: Columns-> DeptName and DeptID
3. Team table : Columns -> Date, EmpID1, EmpID2, DeptNo.

There is a stored procedure which runs every day, and for "EVERY" deptID that exists in the dept table, selects two employee from emp table and puts them in the team table. Now assuming that there are several thousands of departments in the dept table, the amount of data entered in Team table is tremendous every day.
If I continue to run the stored proc for 1 month, the team table will have lots of rows in it.

The real problem is when I want to retrive data for a employee(empid1 or empid2) from Team table and view the related details like date, deptno and empid1 to empid2 from emp table.

How do we optimise the data retrieval and storage for the table Team. Please help me to optimize the query and data retrieval time from Team table.


Thanks,
Ganesh

Reply With Quote
  #2  
Old April 10th, 2008, 04:45 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,711 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 4 h 53 m 59 sec
Reputation Power: 259
I think you got everything wrong (as far as I can see from your description of course, your problem might be more complicated).
Anyway, a single query solution to get two random employees for each department is
sql Code:
Original - sql Code
  1. SELECT
  2.  * /* don't use this hack, list your columns */
  3. FROM
  4.  (
  5.  SELECT
  6.   a.emp,
  7.   a.dept,
  8.   RANK() over (Partition BY dept ORDER BY dbms_random.value) AS rank_1
  9.  FROM
  10.   (
  11.   /* this is where I build the equivalent of your emp table with the union trick */
  12.   SELECT 'emp1' emp, 'dept1' dept FROM dual
  13.    UNION ALL
  14.   SELECT 'emp2', 'dept1' FROM dual
  15.    UNION ALL
  16.   SELECT 'emp3', 'dept1' FROM dual
  17.    UNION ALL
  18.   SELECT 'emp4', 'dept1' FROM dual
  19.    UNION ALL
  20.   SELECT 'empA', 'dept2' FROM dual
  21.    UNION ALL
  22.   SELECT 'empB', 'dept2' FROM dual
  23.    UNION ALL
  24.   SELECT 'empC', 'dept2' FROM dual
  25.    UNION ALL
  26.   SELECT 'empD', 'dept2' FROM dual
  27.   ) a
  28. )
  29. WHERE
  30. rank_1 < 3;

Consider that I had to put the long UNION ALL chain to build an equivalent of your emp table, you won't need this.
Then you'll be able to join the result of this query with any table you want.

Reply With Quote
  #3  
Old April 10th, 2008, 04:48 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,711 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 4 h 53 m 59 sec
Reputation Power: 259
You posted the same question in the MsSQL forum, what's the good one?

Reply With Quote
  #4  
Old April 10th, 2008, 10:55 PM
clivew clivew is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 713 clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 17 h 48 m 33 sec
Reputation Power: 12
Starting from your schema and accepting that all you are interested in is optimizing queries by employee on the Team Table.

You need an index on the empId1 column and one on the empId2 column
(you imply that the query might be based upon either column).

If you want the results ordered by date and there will be a lot of records in the result set consider adding the date column to each index.
Make sure the statistics are kept up to date.

You might also want to consider adding a surrogate PK column to the table.

Clive

Reply With Quote
  #5  
Old April 11th, 2008, 05:45 AM
ganesh_salpure ganesh_salpure is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 7 ganesh_salpure User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 48 m 44 sec
Reputation Power: 0
Clive and Pabloj - I really appreciate you helping me out and for the solutions. I think I need to reframe my problem.

Consider this table with columns "Date Emp1 Emp2"
Date Emp1 Emp2
1-feb 1 2
2-feb 3 4
3-feb 6 1
4-feb 1 2
5-feb 1 5
6-Feb 5 7


Now, I want search for matches to employee with empid 1 in a way that if column emp1 contains 1 I want the data in column emp2, and if column emp2 contains 1, then I want data in column emp1.

The output has to be a single column with no duplicate values. In the above example, for employee id 1, the output would be, 2,5 and 6 only.

I have to scan both columns for a given employee id. Assuming this table has lakhs of records, what will be the most optimized way to retrieve the data faster. Also, do I need to restructure this table for faster data retrieval?
I have indexes on emp1 and emp2 columns.

Thanks again..looking for a reply .

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsDatabasesOracle Development > Table data storage and retrieval optimization


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway