How can I add column from one table to another in Clickhouse? - clickhouse

I have a table having following columns: (id, col1,col2). I need to add col3 from a temporary table having (id,col3). So that after the operation table 1 should be: (id,col1,col2,col3) . After this, I drop the temporary table. How can this be done in Clickhouse?
I know of an approach that uses join table engine. However, join table data is stored in memory and I have memory resitrictions. How can I achieve the same result by not creating a in-memory table?

There is no magic in this realm. And the spoon does exists.
You can use that approach with samples and make many updates. Piece by piece by 10% for example.

Related

How can I merge two tables using ROWID in oracle?

I know that ROWID is distinct for each row in different tables.But,I am seeing somewhere that two tables are being merged using rowid.So,I also tried to see it,but I am getting the blank output.
I have person table which looks as:
scrowid is the column which contains rowid as:
alter table ot.person
add scrowid VARCHAR2(200) PRIMARY KEY;
I populated this person table as:
insert into ot.person(id,name,age,scrowid)
select id,name, age,a.rowid from ot.per a;
After this I also created another table ot.temp_person by same steps.Both table has same table structure and datatypes.So, i wanted to see them using inner join and I tried them as:
select * from ot.person p inner join ot.temp_person tp ON p.scrowid=tp.scrowid
I got my output as empty table:
Is there is any possible way I can merge two tables using rowid? Or I have forgotten some steps?If there is any way to join these two tables using rowid then suggest me.
Define scrowid as datatype ROWID or UROWID then it may work.
However, in general the ROWID may change at any time unless you lock the record, so it would be a poor key to join your tables.
I think perhaps you misunderstood the merging of two tables via rowid, unless what you actually saw was a Union, Cross Join, or Full Outer Join. Any attempt to match rowid, requardless of you define it, doomed to fail. This results from it being an internal definition. Rowid in not just a data type it is an internal structure (That is an older version of description but Oracle doesn't link documentation versions.) Those fields are basically:
- The data object number of the object
- The data block in the datafile in which the row resides
- The position of the row in the data block (first row is 0)
- The datafile in which the row resides (first file is 1). The file
number is relative to the tablespace.
So while it's possible for different tables to have the same rowid, it would be exteremly unlikely. Thus making an inner join on them always return null.

Is constraints important for for select?

I have table on database1 with some constraints like primary key and check. I have all my logic on this database for insert data in this table, then I need this table on database2 to just select data.
So I made replication for this table using database links (select data from database1 insert data into database2), from database1 to database2. I made this table on database2 without constraints, I just create index on the fields which I need in my select's where clause.
Is there any reason why I need to create same constraints(primary key and checks) on database2, when I need this table just for select on this base? Maybe it gets performance difference?
Is there any reason why I need to create same constraints(primary key
and checks) on database2, when I need this table just for select on
this base? Maybe it gets performance difference?
There is no need to have same constraints on your table in database2 until you are not populating that table other that source table of database1 which is having constraints. This is because all validation is already been done while inserting records to table of database1. There is no performance related improvement with constraints addition. Indexing table of database2 will help in performance.

Oracle, Spring JDBC, the fastest way how to delete data from table

I use JdbcTemplate's batchUpdate to insert big amount of data into db tables
What is the fastest way to delete it?
I use
DELETE FROM tableName WHERE ID >= MIN_ID
where MIN_ID is the startValue of the sequence = 1000000
Note, I want to start from MIN_ID and keep data with IDs below 1000000
Is there better approach?
What best practices should one follow?
Note, I use Oracle db
If you want to delete the entire table since MIN_ID is the startValue, you can use TRUNCATE TABLE as TRUNCATE TABLE tableName;
If you want to delete big part of table it is usually cheaper to create new_table as select * from tableName where nvl(ID,-1) < MIN_ID; and swap names. Don't forget about swaping indexes and recreating constraints and other objects. Of course if someone can insert the table during process of swapping you need either lock table or implement some consistency mechanism that will take care of data inserted during swap.

Delete rows from partition table - Best way

I want to delete around 1 million records from a table which is partitioned and table size is around 10-13 millions , As of now only 2 partition exist in the table containining July month data and august month data, and i want to delete from July month.Can you please let me know if a simple delete from table paritition (0715) is ok to do ? Possibilities of fragmentation ? or any best way out?
Thank you
DELETE is rather costly operation on large partitioned tables (but 10M is not realy large). Typically you try to avoid it and remove the data partition-wise using drop partition.
The simplest schema is rolling window, where you define a range partitioning schema by dropping the oldest partitian after the retention interval.
If you need more controll you may use CTAS and exchange back approach.
Instead of deleting a large part of a partition create a copy of it
create table TMP as
select * from TAB PARTITION (ppp)
where <predicate to filter out records to be ommited for partition ppp>
Create indexes on the TMP table in the same structure as the LOCAL indexes of the partitioned table.
Than exchange the temporary table with the partition
ALTER TABLE TAB
EXCHANGE PARTITION ppp WITH TABLE TMP including indexes
WITHOUT VALIDATION
Note no fragmenatation as a result, in contrary you may use it to reorganize the partition data (e.g. with ORDER BY in CTAS or with COMPRESS etc.)
You can delete truncate the partition from the given table. Delete also you can perform if you want to delete few rows from the partition. Plz share your table structure along with the partition details so that it will be easy for people here to assist you.

Update Index Organized Tables using multiple UPDATE queries (temporary duplicates)

I need to update the primary key of a large Index Organized Table (20 million rows) on Oracle 11g.
Is it possible to do this using multiple UPDATE queries? i.e. Many smaller UPDATEs of say 100,000 rows at a time. The problem is that one of these UPDATE batches could temporarily produce a duplicate primary key value (there would be no duplicates after all the UPDATEs have completed.)
So, I guess I'm asking is it somehow possible to temporarily disable the primary key constraint (but which is required for an IOT!) or alter the table temporarily some other way. I can have exclusive and offline access to this table.
The only solution I can see is to create a new table and when complete, drop the original table and rename the new table to the original table name.
Am I missing another possibility?
You can't disable / drop the primary key constraint from an IOT, since it is a unique index by definition.
When I need to change an IOT like this, I either do a CTAS (create table as) for a new plain heap table, do my maintenance, and then CTAS a new IOT.
Something like:
create table t_temp as select * from t_iot;
-- do maintenance
create table t_new_iot as select * from t_temp;
If, however, you need to simply add or join a new field to the existing key, you can do this in one step by creating the new IOT structure, then populating directly from the old IOT with a query.
Unfortunately, this is one of the downsides to IOTs.
I would recommend following method:
Create new IOT table partitioned by system with single partition
with exactly same structure as current one.
Lock current IOT table to prevent any DML.
insert into new table as select from current table changing PK values in select. This step
could be repeated several times if needed. In this case it's better
to do it in another session to keep lock on original table.
Exchange partition of new table with original table.

Resources