Virtual partitioning

If you want to divide a table into an equal number of partitions by rowid range then you can use this SQL.

If you want to divide a table into an equal number of partitions by rowid range then you can use the following SQL. Just replace the <TABLE_NAME> with the actual table and run. When asked for input, enter the number of rows you want in each partition.

                
1 select 
2  (CASE WHEN ( 
3     case when rnum = 1 OR mod(rnum,&&PART)= 1 then ceil(rnum/&&PART) end) 
4          IS NOT NULL AND 
5            (case when mod(rnum,&&PART)= 0 OR mod(rnum,total) = 0 then 
6                    ceil(rnum/&&PART) end) IS NOT NULL 
7       THEN 
8            (NVL((case when rnum = 1 OR mod(rnum,&&PART)= 1 then 
9                                  ceil(rnum/&&PART) end),0) + 
10             NVL((case when mod(rnum,&&PART)= 0 OR mod(rnum,total) = 0 then 
11                              ceil(rnum/&&PART) end),0))/2 
12       ELSE 
13            (NVL((case when rnum = 1 OR mod(rnum,&&PART)= 1 then 
14                                  ceil(rnum/&&PART) end),0) + 
15            NVL((case when mod(rnum,&&PART)= 0 OR mod(rnum,total) = 0 then 
16                              ceil(rnum/&&PART) end),0)) 
17       END) seq 
18  ,MAX((case when rnum = 1 OR mod(rnum,&&PART)= 1 THEN b.rid end)) lrowid 
19  ,MAX((CASE WHEN mod(rnum,&&PART)= 0 OR mod(rnum,total) = 0 
20                    THEN b.rid end)) hrowid 
21  ,MAX((case when rnum = 1 OR mod(rnum,&&PART)= 1 THEN b.rnum end)) lrnum 
22  ,MAX((CASE WHEN mod(rnum,&&PART)= 0 OR mod(rnum,total) = 0 
23                    THEN b.rnum end)) hrnum 
24  ,(MAX((CASE WHEN mod(rnum,&&PART)= 0 OR mod(rnum,total) = 0 
25                    THEN b.rnum end)) 
26  -MAX((case when rnum = 1 OR mod(rnum,&&PART)= 1 THEN b.rnum end))+1) recnum 
27  from ( 
28  select rid,total,rnum from 
29  ( 
30  select rowid rid 
31  ,count(rowid) over () total 
32  ,row_number() over (order by rowid) rnum 
33  from <TABLE_NAME>
34  ) )b 
35  where b.rnum IN (1,total 
36  ,case when mod(rnum,&&PART) = 0 OR mod(rnum-1,&&PART) = 0 then rnum end) 
37  group by 
38      (CASE WHEN ( 
39        case when rnum = 1 OR mod(rnum,&&PART)= 1 then ceil(rnum/&&PART) end) 
40                    IS NOT NULL AND 
41                (case when mod(rnum,&&PART)= 0 OR mod(rnum,total) = 0 then 
42                                     ceil(rnum/&&PART) end) IS NOT NULL THEN 
43       ( NVL((case when rnum = 1 OR mod(rnum,&&PART)= 1 then 
44                                  ceil(rnum/&&PART) end),0) + 
45         NVL((case when mod(rnum,&&PART)= 0 OR mod(rnum,total) = 0 then 
46                              ceil(rnum/&&PART) end),0))/2 
47       ELSE 
48       ( NVL((case when rnum = 1 OR mod(rnum,&&PART)= 1 then 
49                                  ceil(rnum/&&PART) end),0) + 
50         NVL((case when mod(rnum,&&PART)= 0 OR mod(rnum,total) = 0 then 
51                              ceil(rnum/&&PART) end),0)) 
52       END) 
53  / 

For More Information

  • What do you think about this tip? E-mail the Editor at tdichiara@techtarget.com with your feedback.
  • The Best Oracle Web Links: tips, tutorials, scripts, and more.
  • Have an Oracle tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
  • Ask your technical Oracle questions--or help out your peers by answering them--in our live discussion forums.
  • Check out our Ask the Experts feature: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.

This was first published in December 2001

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close