MySql JOIN query with OR clause very slow - mysql

I have the following query:
SELECT a.id, b.from, b.to FROM a INNER JOIN b ON a.id = b.from OR a.id = b.to;
which is extremely slow.
If I remove the OR clause and run each query separately then the both queries execute under 1 second.
SELECT a.id, b.from, b.to FROM a INNER JOIN b ON a.id = b.from;
SELECT a.id, b.from, b.to FROM a INNER JOIN b ON a.id = b.to;
How can I speed up the original query (set up indexes) or redesign the query itself?

What about using union?
SELECT a.id, b.from, b.to FROM a INNER JOIN b ON a.id=b.from
UNION
SELECT a.id, b.from, b.to FROM a INNER JOIN b ON a.id=b.to

How about the following instead. Just join to b twice:
SELECT a.id, b.from, b2.to
FROM a
INNER JOIN b ON a.id = b.from
INNER JOIN b b2 ON a.id = b2.to;
You may have to use LEFT JOIN instead if you don't always have a record for both cases.

Related

Group by Use Query if SQL LEFT JOIN is performed multiple times

I think it's impossible, but I'm asking if there's a good way.
There are A table / B table / C table.
The table was joined LEFT JOIN based on table A with FK called id of each table.
At this time, I would like to output the count(*) as B table rows and C table rows based on b.id(B_CNT) c.id(C_CNT)
SELECT
*
FROM
A
LEFT JOIN B ON A.ID = B.ID
LEFT JOIN C ON A.ID = C.ID (base query)
how could I count group by b.id and c.id?
You could try:
SELECT
COUNT(DISTINCT B.ID), COUNT(DISTINCT C.ID)
FROM A
LEFT JOIN B
ON A.ID = B.ID
LEFT JOIN C
ON A.ID = C.ID
(I couldn't quite understand from your question, but I'm making an assumption that you want the distinct count of "ID" from each table)
You can use a couple of scalar subqueries. For example:
select id,
(select count(*) from b where b.id = a.id) as b,
(select count(*) from c where c.id = a.id) as c
from a

How to count an inner join in MySQL

How could I count an inner join output, thanks a lot
-- Quantity A = 981
SELECT COUNT(DISTINCT ID) FROM A;
-- Quantity B = 673
SELECT COUNT(DISTINCT ID) FROM B;
How can i count an inner join
SELECT * FROM A
INNER JOIN B
ON A.ID = B.ID;
Combine your two attempts into one since you're performing an INNER JOIN, it does not matter if you use A.ID or B.ID in the DISTINCT COUNT:
SELECT COUNT(DISTINCT A.ID) AS AB_Count FROM A INNER JOIN B ON A.ID = B.ID;
Fiddle for reference.

Join Three Tables On Two Columns

I'm using this to join 3 tables
FROM TABLE_A LEFT JOIN TABLE_B ON A.Name = B.Name
LEFT JOIN TABLE_C ON A.Name = C.Name
Whenever I try something like
FROM TABLE_A LEFT JOIN TABLE_B ON A.Name = B.Name, A.Number = B.Number
LEFT JOIN TABLE_C ON A.Name = C.Name, A.Number = C.Number
It tells me I can only use one column for this operation. I need to join on two different columns though so I can't leave it at the first example. Using AND didn't help me either.
Try to replace the comma between the dual tests with an operator.
such as:
FROM TABLE_A LEFT JOIN TABLE_B ON A.Name = B.Name AND A.Number = B.Number
LEFT JOIN TABLE_C ON A.Name = C.Name AND A.Number = C.Number

Avoiding subqueries

I have a query like this:
SELECT a.number, a.name, b.name1 FROM a,b
WHERE a.number NOT IN (SELECT number FROM c)
AND a.name = b.name1
This query takes a very long time to run. I think it is because of the subquery.
Is it possible to avoid using subquery in this case?
You can use this code as an alternative to get your results.
SELECT a.number, a.name, b.name1
FROM a
JOIN b ON a.name = b.name1
LEFT JOIN c ON a.number = c.number
WHERE c.number is NULL;
You must however check to make sure you have properly defined indexes on your table unless the query will do full table scans on each execution. This will not be noticeable when the data in your table is small, it will start to get slower as your table grows.
You can do an explain on the query to check its execution plan, and optimise accordingly.
EXPLAIN SELECT a.number, a.name, b.name1
FROM a
JOIN b ON a.name = b.name1
LEFT JOIN c ON a.number = c.number
WHERE c.number is NULL;
Try using join:
SELECT a.number, a.name, b.name1
FROM a join b on a.name = b.name1
left join c on a.number = c.number
WHERE c.number is null

Can not join more than two table

I have 3 tables:
table1
fields : id, title, cat1_id
table2
fields : id, title, cat2_id
table3
fields : id, title
My SQL :
SELECT a.id, a.title, b.title, c.title
FROM table1 AS a
INNER JOIN table2 AS b ON b.id = a.cat1_id
AND INNER JOIN table3 AS c ON c.id = b.cat2_id
ORDER BY a.id DESC
it's not working.
I try this SQL, it's working:
SELECT a.id, a.title, b.title, c.title
FROM table1 AS a
INNER JOIN table2 AS b ON b.id = a.cat1_id
ORDER BY a.id DESC
but double INNER JOIN does not work.
You do not need to use AND before JOIN to join more that two tables.
Your query should be
SELECT a.id, a.title, b.title, c.title
FROM table1 AS a
INNER JOIN table2 AS b ON b.id = a.cat1_id
INNER JOIN table3 AS c ON c.id = b.cat2_id
ORDER BY a.id DESC
Have a look at MySQL JOIN Syntax