Performance of incremental update of SQL table - different approaches - sql

I want to make an incremental update of table1 with only new records from table2. I am looking for the fastest way. Assuming that the id is unique integer, which of these three approaches is the fastest? If you can propose any other approach, please share.
Solution 1.
INSERT INTO table1
SELECT * FROM table2
WHERE NOT EXISTS
(SELECT 1 FROM table1
WHERE table2.id = table1.id)
Solution 2.
INSERT INTO table1
SELECT table1.* FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.id
WHERE table2.id IS NULL
Solution 3.
INSERT INTO table1
SELECT * FROM table2
WHERE table2.id > (SELECT MAX([table1.id]) FROM table1)

The third solution is incomparably faster than two previous. But be careful. It may return different results than solution 1 and solution 2.
If you want to use 3rd solution be sure that new id in table2 always gets higher number than the older one. You may apply check of the records number in both tables. I also recommend indexing id in table1.

insert into table1
select table2.* from from table2 left outer join table1 on table2.id = table1.id
where table1.id is null

Related

SQL Join tables with empty values

I have 2 tables
Lets say Table1 and Table2
They both have one shared value(id)
What I'm looking for is whether there is any function to combine them both based on that key, however if table2 has more elements, i want columns of table1 to be empty, and if table1 has more elements, table 2 columns to be empty
I tried a lot of different joins, but most of the time I end up with a lot of duplicate values as it tries to fill in both sides.
Tried Full outer join, Full join, etc
You are looking for full join:
select t1.*, t2.*
from t1 full join
t2
on t1.id = t2.id;
The above code from Gordon is right. However, since you have not specified the database and its version, I will post an alternate version for MySQL, which should also work for other databases.
Without duplicates:
SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION
SELECT * FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id
With duplicates:
SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION ALL
SELECT * FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id

How to select data from the table not exist in another table sql

How to select data from the table not exist in another table sql. I've tried NOT IN and NOT EXIST methods. But it causes performance issues for large amount of data. Can anyone suggest a solution for this.?
Thanks in advance.
I’ve tried the following.
SELECT name
FROM table1
WHERE NOT EXISTS
(SELECT *
FROM table2
WHERE table1.name = table2.name)
And NOT IN Cases.
But performance issues while a for large number of data.
I think your table table1 and table2 have index on their name column, so you can try this:
SELECT name
FROM table1 t1 LEFT JOIN table2 t2 ON t1.name = t2.name
WHERE t2.id IS NULL
May be id column existed, if not, use t2.name as a replacement for t2.id
For this query:
SELECT name
FROM table1
WHERE NOT EXISTS
(SELECT *
FROM table2
WHERE table1.name = table2.name)
You want an index on table2(name).

For each loop SQL

Table1
id
Table2
id
Table3
id|table1_id|table2_id
I need the SQL (using MySQL) statement for:
for each row in table1 {
for each row in table2 {
insert in table3 values table1.id, table2.id;
}
Does an insert/select statement exists ? Or do I have to use a loop ?
You don't need to use loops at all. You should be thinking in terms of SETS when using a relational database.
Here is the correct way to achieve this. First using the CROSS JOIN to create a cartesian set of all the combinations of ids from table1 and table2. Then inserting that entire set into table3
Note: I am assuming Table3 ID is an auto-number.
INSERT INTO TABLE3 (TABLE1_ID,TABLE2_ID)
SELECT T1.ID
,T2.ID
FROM TABLE1 T1
CROSS JOIN
TABLE2 T2

SQL Select with join query works but delete does not work

I have two tables Table1 and Table2. I compared thes tables using the query below. SELECT
Select Table1.ID
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ID =Table2.ID
WHERE Table2.ID IS NULL
and got 1508 records that exist in Table1 but not in Table2.
Now I wanted to delete these records in Table2. This is the code I used below
DELETE Table1.*
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ID =Table2.ID
WHERE Table2.ID IS NULL
This is the error I am get Could not delete from specified tables. I realize something is wrong with my sql, but where. I thought this DELETE was specifying a table not tables.
Try this:
DELETE FROM Table1
WHERE ID NOT IN
(
SELECT ID
FROM table2
)

Ms Access gives rubbish when updating a join column

Updating a column that is part of your selection criteria should be no problem I think/thought.
I still have a query that gives odd results:
update table1 as t1
inner join table2 as t2 on t1.id = t2.old_id
set t1.id = t2.id
I use table2 to map id to old_id.
table1.id and table2.id are both primary keys. table2.old_id is also unique.
It follows that table1.id will still be unique after this update.
WRONG! MS Access will make rumble of this, with some duplicate table.id values.
I think however this is the correct way of updating a column that is used in a join? How could we achieve the desired result in MS Access?
Note: table2 is a mysql view accessed via ODBC.
table1.id and table2.id are both
primary keys. table2.old_id is also
unique.
It follows that table1.id will still
be unique after this update.
That ain't necessarily so.
It seems old_ID and (new) ID are of the same data type. A row in table1 that has no matching row in table2 based on the predicate (table1.ID = table2.old_ID) would not be updated. A different row could match a (new) ID value, hence get updated, with the same value as that row that didn't get updated.
Here's an example using Standard SQL (works in SQL Server 2008, not it Access/Jet) which I hope you can follow. Note I've re-written your INNER JOIN using an EXISTS construct to fit the logic of what I am trying to convey:
WITH Table1 (ID) AS
(
SELECT ID
FROM (
VALUES (1),
(2),
(3),
(4)
) AS Table2 (ID)
),
Table2 (old_ID, ID) AS
(
SELECT old_ID, ID
FROM (
VALUES (1, 55),
(2, 99),
(3, 4)
) AS Table2 (old_ID, ID)
)
-- ID of rows that will not be updated:
SELECT T1.ID
FROM Table1 AS T1
WHERE NOT EXISTS (
SELECT *
FROM Table2 AS T2
WHERE T1.ID = T2.old_ID
)
UNION ALL
-- updated IDs
SELECT T2.ID
FROM Table2 AS T2
WHERE EXISTS (
SELECT *
FROM Table1 AS T1
WHERE T1.ID = T2.old_ID
);
The resultset:
ID
---
4 --<-- duplicate
55
99
4 --<-- duplicate
In other words, even though all the following are unique:
(table1.ID)
(table2.ID)
(table2.old_ID)
...the following may contain duplicates:
table1.ID
UNION ALL
table2.ID
Using JOINs in UPDATE statements isn't consistently supported, which is why they aren't my habit to use.
UPDATE TABLE1 AS t1
SET t1.id = (SELECT t2.id
FROM TABLE2 t2
WHERE t2.old_id = t1.id)