SAP HANA | TRUNCATE PARTITION - partitioning

Is there a way we can truncate partition in SAP HANA? As official documents only say about dropping partition?

The official documentation is correct on this topic. Table partitions can be dropped and re-created but not TRUNCATEd.

Remove all data from specific partition of table is possible
Syntax : truncate table <table_name> partition <partition_name>
Except : You cannot use truncate table <table_name> partition <partition_name> when the table object is marked for replication and is created using hash- or round-robin-based partitions. Doing so will end up in error.
Check official document by SAP : https://help.sap.com/viewer/e0d4539d39c34f52ae9ef822c2060077/16.0.3.3/en-US/ab461944bc2b1014bc78d29ab3759f49.html

Related

partitioning in MySQL : insert into partition

I come from an Apache Hive background.
In that language, you would say the below to insert into date 20220601:
insert into table db.tablename partition(date=20220601)
In MySQL; I can't get such an insert statement to work. I have been Googling & it seems it just sorts itself out?
So if I did
insert into db.tablename
select * from db.othertable
Would it automatically partition the ingested data?
I feel like I am missing something here!
If the table is partitioned, the values you insert determine which partition the row goes into. Partitioning a table requires you define the mapping, so it's always deterministic which partition a row goes into.
Therefore you don't need to tell INSERT which partition to insert the row into. It's determined automatically by the values you insert in the row.
Partitioning in MySQL is not required for a table. By default, a table is not partitioned. This is normal and sufficient in almost all cases.
Perhaps partitioning in Apache Hive is necessary and does something different from the feature called partitioning in MySQL? I don't know Apache Hive, so I can't answer that.
I suggest you read the MySQL manual chapter about partitioning if you want to learn more about it: https://dev.mysql.com/doc/refman/8.0/en/partitioning.html

How to rename a partition name in mysql 5.6.16

I'm using mysql 5.6.16 and now want to change a partition name. I cannot find rename partition syntax, and it is inefficient to use statement as below:
alter table name reorganize partition old into ( partition new ...).
I got it by the way of exchange pasrtition. Firstly, I exchange the data in the partition to a normal empty table, Secondly, I reorganize the empty partition to wanted name. Finally, I exhchange the data with the normal table again

JDBI ALTER TABLE DROP PARTITION

Hey I'm having an issue with altering table partitions using JDBI. Here is an example of the query I'm trying to run:
ALTER TABLE table1 DROP PARTITION P_1
This runs fine in MySQL when dropping the partition "P_1" from the table "table1."
I've implemented it in my java code as the following:
#SqlUpdate("ALTER TABLE table1 DROP PARTITION :partition;")
public void deletePartition(#Bind("partition") String partition);
And call this function as such
deletePartition("P_1")
However, this results in the following error:
Causing: org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''P_1'' at line 1 [statement:"ALTER TABLE table1 DROP PARTITION :partition;", located:"ALTER TABLE table1 DROP PARTITION :partition;", rewritten:"/* HiveDropBoxDBI.deletePartition */ ALTER TABLE table1 DROP PARTITION ?;", arguments:{ positional:{}, named:{partition:'P_1'}, finder:[]}]
Is this functionality not supported? Or am I missing something with my syntax?
Thanks
We need to use #Define for changing the query. #Bind is only for used for binding parameters (like some_field = :value).
#SqlUpdate("ALTER TABLE table1 DROP PARTITION :partition;")
public void deletePartition(#Bind("partition") String partition);
You can't use bind parameters for DDL such as CREATE, ALTER and DROP.
In order to drop your partition, you must concatenate the partition name onto the query instead.
However, taking a partition name and appending it straight into a SQL string which gets executed is a recipe for a security vulnerability. Consider some of the following:
'escaping' the partition name with backticks,
checking that the partition name contains only certain whitelisted characters (e.g. alphanumerics and underscores),
querying the INFORMATION_SCHEMA.PARTITIONS table to see if the partition you're trying to drop exists. (This might not be a good idea if there is a chance that the partition may be created or dropped between checking for its existence and dropping it. I don't know your application well enough to say whether this would be a problem.)
As JDBI relies on constant strings for #SqlUpdate annotations, you will not be able to use JDBI in this way to drop partitions, unless you only ever want to drop the same partition.

SQL command to automatically create table based on data being inserted

I have to load some data into a temporary table, but the data is never uniform, the datatype and number of columns will always be different.
Is there an SQL command that will automatically create table specifications based on data that will be loaded into it?
Assuming that you're populating it from a query, you can use the syntax CREATE TABLE tablename SELECT ...; see ยง12.1.14.1. CREATE TABLE ... SELECT Syntax in the MySQL 5.6 Reference Manual.
SELECT ... INTO should do what you want.

create an index without locking the DB

I have a table with 10+ million rows. I need to create an index on a single column, however, the index takes so long to create that I get locks against the table.
It may be important to note that the index is being created as part of a 'rake db:migrate' step... I'm not adverse to creating the index manually if that will work.
UPDATE: I suppose I should have mentioned that this a write often table.
MySQL NDBCLUSTER engine can create index online without locking the writes to the table. However, the most widely used InnoDB engine does not support this feature. Another free and open source DB Postgres supports 'create index concurrently'.
you can prevent the blockage with something like this (pseudo-code):
create table temp like my_table;
update logger to log in temp;
alter table my_table add index new_index;
insert into my_table select * from temp;
update logger to log in my_table;
drop table temp
Where logger would be whatever adds rows/updates to your table in regular use(ex.: php script). This will set up a temporary table to use while the other one updates.
Try to make sure that the index is created before the records are inserted. That way, the index will also be filled during the population of the table. Although that will take longer, at least it will be ready to go when the rake task is done.