I have a standalone Java application with embedded H2 Database(D1) and table T1. I have another mysql database(D2) with table T2 hosted on a server. My Requirement is to pull the full data from D2.T2 and push it into D1.T1 . T1 and T2 both have same table definition. First pull is not a problem. Starting from 2nd pull I only need to pull the rows which got updated in D2.T2 and update that in D1.T2 . How can this be achieved? I have to find the delta from the last time i pulled and then update those rows.
If the structures on both tables are the same you can try the following SQL statement:
INSERT INTO your_table
(your_column, ...)
VALUES
(your_value, ...)
ON DUBLICATE KEY UPDATE
your_column = your_value, ...
Your have to get the entries from your source database and execute the above statement for each one on your target database.
Related
I have a Main Table named REF_SERVICE_OFFERING where-in I already have 3 Million+ data. Now, I wanted to update the 3M records in Java based on some specific condition.
We decided to create a temporary table (where-in the records within will be used to Update the Main Table) and using the below query to Update the Main Table. The temporary table will hold more than 200k records :
UPDATE REF_SERVICE_OFFERING SET
PART_PRICE_BILL_TYPE= TEMP.PART_PRICE_BILL_TYPE,
part_price_unit_type=TEMP.part_price_unit_type,
part_price_allowed_units=TEMP.part_price_allowed_units,
part_price_discount=TEMP.part_price_discount,
part_price_source_id=TEMP.part_price_source_id
FROM REF_SERVICE_OFFERING RSO JOIN ref_offer_temp1 TEMP
ON TEMP.RECORD_NUM = RSO.RECORD_NUM
AND TEMP.SO_NAME = RSO.SO_NAME
AND TEMP.SERVICE_CASE_TYPE = RSO.SERVICE_CASE_TYPE
AND TEMP.WORK_ORDER_TYPE = RSO.WORK_ORDER_TYPE
WHERE (RSO.PART_PRICE_BILL_TYPE IS NOT NULL OR TRIM(RSO.PART_PRICE_BILL_TYPE) NOT LIKE '')
AND (RSO.PART_PRICE_EXCP_SOURCE_ID IS NOT NULL OR TRIM(RSO.PART_PRICE_EXCP_SOURCE_ID) NOT LIKE '')
Our database is Postgres 9.6. But this update is taking a lot of time and never edning. We also tried dumping only 10k records in the temporary table which will be used to update 4L records.
We tried doing EXPLAIN command and couldnt figure out the reason why.
Any help would be really appreciated.
I have 2 different databases one is MYSQL other is Oracle.Each have 1 table with different name and different columns name.Now I have to perform some db opeartions on each db from a single java application.Suppose for MYSQL db I have Emp table with columns Id,Name,Dept and for Oracle db I have Student table with StudentName and StudentDept.Now without changing code how can I manage 2 dbs?If I mention all db connection related data(connection url,username,password) in a properties file but to execute query I have to mention table name and column name in code.How can I manage it dynamically without altering the code so that in future any new db with different table name and column name is added I can only add the new one in properties file and no need to touch the code.Please suggest.
This might not be the prettiest, but one way to do this:
On application launch, parse properties files to get all DB connections. Store these however you want...List of connection pools, list of single connections, list of connection strings, etc...it doesn't matter.
Run a predefined stored procedure or select query to retrieve all table names from each database found in step 1. In sybase you can do this with
select name from sysobjects where type = 'U'
Build a Map where the key is the table name and the value is either the DB name, connection, connection string, or whatever you are using to manage your DB connections from the result set of #2. Anything that can be passed to your DB connection manager to identify which database it should connect to will work as the value.
In code, when table name is passed, lookup the required DB in the map
Execute query on returned DB Info in the map you created in step 3
As long as the tables are distinct in each DB this will work. Once this is setup, new DBs can be added to the properties file and the cache can be refreshed with an application restart. However, if new tables/columns are being sent to the code, how are these being passed without any code change?
I need some sort of persistance componnent to store id(long) and value(object) for my Java application.
All The cacheing systems I looked at where not persistant enough(If the process died the cache would erase itself) or slow
I tried to use Embedded DataBases like Derby and HSQLDB but they where not as fast as H2 as SELECT and INSERT.
For some reason the UPDATE query takes 1-2 seconds for one row if I Update a row with Blob.
Does anyone know why is it this slow?
Queries:
CREATE TABLE ENTITIES(ID BIGINT PRIMARY KEY, DATA BLOB)
INSERT INTO ENTITIES(DATA, ID) VALUES(?, ?)
UPDATE ENTITIES SET DATA = ? WHERE ID = ?
I am using JDBC with PreparedStatement
Edit:
The connection string is:
jdbc:h2:C:\temp\h2db;FILE_LOCK=NO;
I tried to add CACHE_SIZE=102400 and PAGE_SIZE=209715200 but it didn't help
DELIMITER $$
CREATE TRIGGER `classdost`.`tr_xyz_media`
BEFORE INSERT ON classdost.xyz_media
FOR EACH ROW
BEGIN
DECLARE t_id INT(20);
IF NEW.id < 5000000 THEN
INSERT INTO xyz_media_temp (insert_date) VALUES(CURDATE());
SELECT MAX(xyz_media_temp.id) INTO t_id FROM xyz_media_temp;
SET New.id = t_id;
END IF;
END$$
DELIMITER ;
I have this trigger in MySQL this is running fine when I fire any Insert query in PHPMyadmin but when same Insert is fired from java code it is not executing. I tried changing the code made all request as Ajax with async: false and then I found out that that the earlier requests are still running in background and my later request is giving exception as no ID is returned from database yet.
What can be done to avoid this issue?
If you say requests are still running in background, then it is possible that your table is locked (MyISAM tables), and you cannot modify it.
See if table is locked using this query -
SHOW OPEN TABLES FROM database_name;
This query will show you open tables, whicj cannot be edited (INSERT, UPDATE or etc.). Then check you Java application, find code that locks tables.
I am having two different tables.I am fetching the value from a column of Table1 and then inserting it into a Column of Table2.No processing is done for that data in between, before inserting into secTable2.Both source and destination tables have same datatype - varchar(50). Example data in Column: CSC123
When I query from destination table with SQL query (after insertion is done),Only 0 appears.When I export the results to excel,I could see some special characters before that 0 (Something like squares). This issue happens only for some cases and the actual data is missing.
Application : J2EE
Framework : Hibernate
Database : Oracle
Please suggest a solution
Thanks