How to optimize a MySQL query to get data from one table which is not present in another table? - mysql

I have two MySQL tables named "table1" and "table2", with "table1" containing 1 million rows of data and "table2" containing 0.76 million rows of data. I want to retrieve all the data from "table1" that does not exist in "table2". I tried using a LEFT JOIN in my query, but the query is taking a very long time to execute and has not returned any results after 117965 seconds. I was expecting to get the result much faster.
I'm now looking for suggestions on how to optimize the query or find a more efficient way to get the result. Can anyone help me with this problem?
I tried the following query to get data from table1 that is not present in table2:
SELECT t1.part, t2.company
FROM table1 t1
LEFT JOIN table2 t2 ON t1.part = t2.part AND t1.company = t2.company
WHERE t2.company IS NULL;

You can avoid the JOIN:
SELECT
t1.part,
t1.company
FROM
table t1
WHERE
t1.company IS NULL

Related

Speed up query that returns results when present in one table but not in another

I have two tables. t1 contains business info and t2 contains business hours.
I use the following query to return results where data is present in t1 and is not in t2.
SELECT DISTINCT(t1.id)
t1 LEFT JOIN t2 ON (t1.id = t2.busId)
WHERE t2.busId IS NULL
This query works, but with about 15K records in t1 and 50K rows in t2 this literally takes minutes to return results. I wonder if it's possible to speed it up.

MySQL Joins of tables with different columns

I want to create a table that needs to be a combination of selected columns from three or more tables. I don't have any sample data, just added the columns for explanation.
Table 1
A|B1|C1|D1|E1|F1|G1|H1|I1|J1
Table 2
A|B2|C2|D2|E2|F2|G2
Table 3
A|B3|C3|D3|E3|F3|G3
Resultant New table must have
A|B1|E1|F1|G1|J1|C2|D2|G2|B3|D3|F3
I'm not sure if I need to use a FULL JOIN or use UNIONS.
Each of these tables contain more than 400,000 rows. Any help here on what query needs to be included would be really helpful.
You can try the below query:
select t1.A,t1.B1,t3.E1,t1.F1,t1.G1,t1.J1,t2.C2,t2.D2,t2.G2,t3.B3,t3.D3,t3.F3
from table1 t1 join table2 t2 on t1.A = t2.A
join table3 t3 join table2 t2 on t3.A = t2.A
As Palec commented correctly in the other answer, so adding a bit of explanation to the answer.
You need to use the JOINS for this problem instead of UNION. The reason why I am saying to use JOINS over UNION is UNION combines the data/result of two or more queries into a single result set which includes all the rows which exist in the queries in your UNION. But when you are using JOINs, you can retrieve data from two or more tables based on logical relationships between the tables.
Also to add that you should add an alias name to your table so that it becomes easy to retrieve the column in the select query and also while linking the table.
Join is the correct way:
select table1.A,B1,E1,F1,G1,J1,C2,D2,G2,B3,D3,F3 from table1 join table2 on table1.A = table2.A
join table3 join table2 on table3.A = table2.A

Mysql Select records that not contain records of second table

I have 2 MySQL tables with 1 column and a lot of records. I should select records of the first table that doesn't CONTAIN rows of second table.
Can anyone help me with a query?
Table 1:
http://www.google.com
http://www.aaaa.com
http://bbbb.com
http://www.cccc.com
http://www.dddd.com
Table 2:
tttt.com
aaaa.com
google.com
rrrr.com
dddd.com
Result should to be:
http://bbbb.com
http://www.cccc.com
You can do this with a left join and like:
select t1.*
from table1 t1 left join
table2 t2
on t1.col like concat('%', t2.col, '%')
where t2.col is null;
Unfortunately, you cannot use indexes to optimize this query, so the engine will have to do a nested loop join.
Works!!
A question: why the last row "where t2.col is null" ?
Regards

Which Query is faster if we put the "Where" inside the Join Table or put it at the end?

Ok, I am using Mysql DB. I have 2 simple tables.
Table1
ID-Text
12-txt1
13-txt2
42-txt3
.....
Table2
ID-Type-Text
13- 1 - MuTxt1
42- 1 - MuTxt2
12- 2 - Xnnn
Now I want to join these 2 tables to get all data for Type=1 in table 2
SQL1:
Select * from
Table1 t1
Join
(select * from Table2 where Type=1) t2
on t1.ID=t2.ID
SQL2:
Select * from
Table1 t1
Join
Table2 t2
on t1.ID=t2.ID
where t2.Type=1
These 2 queries give the same result, but which one is faster?
I don't know how Mysql does the Join (or How the Join works in Mysql) & that why I wonder this!!
Exxtra info, Now if i don't want type=1 but want t2.text='MuTxt1', so Sql2 will become
Select * from
Table1 t1
Join
Table2 t2
on t1.ID=t2.ID
where t2.text='MuTxt1'
I feel like this query is slower??
Sometimes the MySQL query optimizer does a pretty decent job and sometimes it sucks. Having said that, there are exception to my answer where the optimizer optimizes something else better.
Sub-Queries are generally expensive as MySQL will need to execute and store results seperately. Normally if you could use a sub-query or a join, the join is faster. Especially when using sub-query as part of your where clause and don't put a limit to it.
Select *
from Table1 t1
Join Table2 t2 on t1.ID=t2.ID
where t2.Type=1
and
Select *
from Table1 t1
Join Table2 t2
where t1.ID =t2.ID AND t2.Type=1
should perform equally well, while
Select *
from Table1 t1
Join (select *
from Table2
where Type=1) t2
on t1.ID=t2.ID
most likely is a lot slower as MySQL stores the result of select * from Table2 where Type=1 into a temporary table.
Generally joins work by building a table comprised of all combinations of rows from both table and afterwards removing lines which do not match the conditions. MySQL of course will try to use indexes containing the columns compared in the on clause and specified in the where clause.
If you are interested in which indexes are used, write EXPLAIN in front of your query and execute.
As per my view 2nd query is more better than first query in terms of code readability and performance. You can include filter condition in Join clause also like
Select * from
Table1 t1
Join
Table2 t2 on t1.ID=t2.ID and t2.Type=1
You can compare execution time for all queries in SQL fiddle here :
Query 1
Query 2
My Query
I think this question is hard to answer since we don't exactly know the internals of the query parser in the database. Usually these kind of constructions are evaluated by the database in a similar way (it can see that the first and second query are identical so parses it correctly, or not).
I would write the second one since it is more clear what is happening.

Mysql join function to join around 25 tables

I have around 15 tables in my database.
Each table has a column employee id's, I wanted to join all the tables based on employee id column and at the same time select two columns from each table.
That means finally, I will have a table with 31 columns. (1 id column and 30 from 15 tables)
What would be the easier way to do this?
If I understand your problem, one possible solution could be:
SELECT t1.id, t1.f1, t1.f2, t2.f3, t2.f4, ....., t15.f30, t15.f31
FROM table1 t1 INNER JOIN table t2 ON t1.id = t2.id
INNER JOIN table3 t3 ON t1.id = t3.id....
....
INNER JOIN table15 t15 ON t1.id = t15.id
WHERE ....
Naturally IDs must have indexes (for faster querying); I'm assuming one-one relation, but this standard query could be easily changed to fullfil your needs...
First of all I would recommend you to use(whenever is possible) a left join to achieve that. In that link you can have an idea of how to optimise the process, because, of course, you will need it a lot. Anyway, depending on the amount of data you have, please consider redesigning the database. Hope that helps,