Creating a table with date as range partition in SQL Server 2012 - sql

I am new to SQL Server coding. Please let me know how to create a table with range partition on date in SQL Server
A similar syntax in teradata would be the following (a table is created with order date as range partition over year 2012 with each day as single partition )
CREATE TABLE ORDER_DATA (
ORDER_NUM INTEGER NOT NULL
,CUST_NUM INTEGER
,ORDER_DATE DATE
,ORDER_TOT DECIMAL(10,2)
)
PRIMARY INDEX(ORDER_NUM)
PARTITION BY (RANGE_N ( ORDER_DATE BETWEEN DATE ‘2012-01-01’ AND DATE 2012-12-31 EACH INTERVAL ‘1’ DAY));
Thanks in advance

The process of creating partitioned table is described on MSDN as follows:
Creating a partitioned table or index typically happens in four parts:
1. Create a filegroup or filegroups and corresponding files that will hold the partitions specified by the partition scheme.
2. Create a partition function that maps the rows of a table or index into partitions based on the values of a specified column.
3. Create a partition scheme that maps the partitions of a partitioned table or index to the new filegroups.
4. Create or modify a table or index and specify the partition scheme as the storage location.
You can find code samples on MSDN.

Related

Dynamic subpartition creation to a partitioned table with data

I have a table like below
Create table t1(
X number,
Y number ,
Z varchar,
Createdatetime date);
Partition by range (createdatetime) interval (numtodsinterval(3,day));
Above table has 5 billion data already partitioned by createdatetime
Now the requirement is to add new column
There may be multiple entries for x,y combo. So the new column must have Y for recent x,y combo (based on creatdatetime) and N for old entries.
I created a column and populated data.
Now I need to subpartition the above range partition
.
Subpartition by list using new column
There will be two sub partitions Y,N based on new column under each date range partitions. How to achieve this dynamically. I tried writing a procedure which loops through the partitions and execute immediate alter table modify partition add subpartition. But I'm getting invalid partition name error .
Any other alternative approaches are also welcome

When is the feature of partitioning on integer column available in BIGQUERY table?

Do we know when google will enable partitioning on integer column in BIGQUERY table? And can we partition BIGQUERY table after it is created or it has to be partitioned at the same time when the BIGQUERY table is created?
Many thanks.
So called Integer range partitioned tables are available already as Beta in a pre-release state
Table must be set as partitioned during its creation as in example below
CREATE TABLE `project.dataset.table`
PARTITION BY RANGE_BUCKET(customer_id, GENERATE_ARRAY(0, 100, 10)) AS
SELECT 1 AS customer_id, DATE '2019-10-01' AS day
See more details in Creating and using integer range partitioned tables

Need to change the partition column to another column and reloading the data into new partitions

I am trying to change the already existing partition column to another column.
The current workflow I'm using:
Backup the existing data
Create a new table with new partition column
Reload the data into new partitions
My problem:
Since there is huge data in our existing partition tables, this way will be costly
Is there a way we can do Alter table and change partition column name to another?
You can not avoid 1-time cost of scanning the table as you can see from the error message generated from this CREATE OR REPLACE DML command
#standardSQL
CREATE OR REPLACE TABLE `project.dataset.table`
PARTITION BY DATE(ts)
AS
SELECT * FROM `project.dataset.table`
Cannot replace a table with a different partitioning spec. Instead, DROP the table, and then recreate it. New partitioning spec is interval(type:day,field:ts) and existing spec is none
What you can do to save cost is use the WHERE command to limit the number of the partition you move from existing table to the new table
CREATE TABLE project.mydataset.newPartitionTable
PARTITION BY date
OPTIONS (
partition_expiration_days=365,
description="Table with a new partition"
) AS
SELECT * from `project.dataset.table` WHERE
PARTITIONTIME >= '2019-01-23 00:00:00'
AND _PARTITIONTIME <= '2019-01-23 00:00:00'
You can consider for example not to move your Long-term storage which is data you haven't access for the last 90 days (see this link for more details)
If you want to keep your original table name you can drop/create it with the new partition field, after the copy, and use the copy option from webUI which will be free of charge

Split hive partition to create multiple partition

I have an external hive table which is partitioned on load_date (DD-MM-YYYY). however the very first period lets say 01-01-2000 has all the data from 1980 till 2000. How can I further create partitions on year for the previous data while keeping the existing data (data for load date greater than 01-01-2000) still available
First load the data of '01-01-2000' into a table and create a dynamic partition table partitioned by data '01-01-2000'. This might solve your problem.

Change range value in table partition

I have a big table with partitions.
ex:
CREATE TABLE cust_order (
cust_id NUMBER(10),
start_date VARCHAR2(25),
amount_sold NUMBER(10,2))
PARTITION BY RANGE (START_DATE)
(
PARTITION MAY VALUES LESS THAN ('20120601'),
PARTITION DEF VALUES LESS THAN(MAXVALUE));
I want to alter the table so that the MAY partition contains values less than '20120501' and that the data from '20120501' to '20120601' are stored in the DEF partition.
First, you should always store dates in DATE columns. Storing dates in a VARCHAR2(25) column is a recipe for problems down the line-- someone will inevitably insert a string that has an unexpected format.
Second, assuming that there are no partitions between the MAY and DEF partitions, you could split the MAY partition
ALTER TABLE cust_order
SPLIT PARTITION may AT ('20120501')
INTO( PARTITION may,
PARTITION june_temp )
UPDATE GLOBAL INDEXES;
and then merge the JUNE_TEMP and DEF partitions
ALTER TABLE cust_order
MERGE PARTITIONS june_temp, def
INTO PARTITION def
I'm not sure that I see the wisdom, however, in doing this... A default partition should not generally store any data-- it's generally only there so that inserts don't error out if they have an unexpectedly large partition key. So taking the data that is already in one partition and moving it into a default partition seems rather odd. If you're just going to create a JUNE partition in the future, I'm not sure why you wouldn't just split the partitions into a MAY and JUNE partition.