I have a database similar to the simplified one below. I need to retrieve columns: col8, col9, col10, col11, col12 (the ones I've circled
Currently, I;m using a left join to join each table but this results in the query taking a very long (there are lots of records). Upon profiling, the biggest impact is writing to the tmp table.
I'm limiting the result to 24, but because of the left joins it's still copying thousands of records to the tmp table.
http://oberto.co.nz/demo/assets/db2.jpeg
Can this be optimised to still retrieve the circled column from each table using joining by the pk?
Thanks.
SELECT a.col12, b1.col8, c1.col9, d1.col10, e1.col11
FROM a
INNER JOIN (SELECT b.col8, b.col2, b.col3 FROM b
WHERE b.col2 = a.col2 GROUP BY b.col8) b1
ON (b1.col2 = a.col2)
INNER JOIN (SELECT c.col9, c.col3, c.col4 FROM c
WHERE c.col3 = b1.col3 GROUP BY c.col9) c1
ON (c1.col3 = b1.col3)
INNER JOIN (SELECT d.col10, d.col4 FROM d
WHERE d.col4 = c1.col4 GROUP BY d.col10) d1
ON (d1.col4 = c1.col4)
INNER JOIN (SELECT e.col11, e.col6 FROM e
WHERE e.col6 = a.col6 GROUP BY e.col11) e1
ON (e1.col6 = a.col6)
Now you will have no more duplicate rows.
You may have to experiment with LEFT instead of INNER joins.
And if you don't need a subselect you should eliminate it, because it slows things down.
in its current and simplest form, I would have the query as...
SELECT STRAIGHT_JOIN
a.col1,
a.col12,
b.col8,
c.col9,
d.col10,
e.col11
FROM a
left join b
on a.col2 = b.col2
left join c
on b.col3 = c.col3
left join d
on c.col4 = d.col4
left join e
on a.col6 = e.col6
However, will adjust once I read back on other criteria, filters, conditions, left/inner join "requirements" that may help optimize further.
Related
I am having an SQL query as follows -
select
*
from A
inner join AA on A.id = AA.aid
inner join AAA on
(
select B.bid, B.bname
from B
inner join C on B.id = C.bid
where C.aaid = AA.id
) as B1 on A.id = B1.aid
Which gives an error
Unknown column 'AA.id' in 'where clause'
It will be very helpful if someone can tell me the reason and provide me with a possible solution.
I believe that you need something like this
select
*
from A
inner join AA on A.id = AA.aid
inner join AAA on AA.id = AAA.aid
inner join
(
select B.bid, B.bname, C.aaid
from B
inner join C on B.id = C.bid
) as B1 on A.id = B1.aid and AA.id = B1.aaid
It is not possible to reference outer aliases in join subqueries in MySQL. In PostgreSQL you could use CROSS JOIN LATERAL and in SQL Server CROSS APPLY, however, there is no such thing in MySQL.
From my understanding,
select
*
from #a1 a1 --A
inner join #a2 a2 on a1.i = A2.i --AA
inner join #a3 a3 on a1.i = a3.i --AAA
inner join
(
select B.i, a2.i ai
from #b b --B
inner join #c c on B.i = C.i --C
inner join #a2 a2 on a2.i = C.i --AA
) as B1
on A1.i = B1.i
and B1.ai = a2.i
Lack of sample data, we help as assumption.
Revert me, if query needs updates.
This is too long for a comment, so ...
Let's say you didn't write the subquery in the FROM clause as an ad-hoc view, but make it an explicit view B1 (with CREATE VIEW). Then your query would read
select *
from A
inner join AA on A.id = AA.aid
inner join AAA on B1 on A.id = B1.aid
which illustrates that your syntax is off.
Moreover the B1 select doesn't contain an aid, only a bid and a bname.
At last the ON clause for AAA doesn't contain any reference to the table AAA, so this is not really a join criteria for that table.
This is the answer to what's wrong with your query. I cannot show you the correct query, though, because it's completely unclear what you are trying to achieve. Please elaborate. It's probably quite simple and you're over-complicating things :-)
I have the following schema.
I can run two queries fairly simply
select * from booking_model_assignment
join booking_model on booking_model_assignment.booking_model_id = booking_model.id
left outer join axis_channel_mappings on bmi_id = axis_channel_mappings.assignment_id
left outer join axis_revenue_stream_mappings on bmi_id = axis_revenue_stream_mappings.assignment_id
which will give me all of the combinations of channel mappings and 'revenue_stream_mappings' which fit a booking model, with Null if there is one which only matches in one of the tables.
The other query
select * from axis_channel join axis_revenue_stream
Gives all of the possible combinations of channels and revenue streams.
What I would like is a query which will give all of the combinations, and the booking_model if that combination matches.
Any time I try to join or subquery I seem to get too many, or too few results. I think the issue is that I want the assignment_id to match across outer joins but only if there is an outer join.
The schema is laid out like this so it will be possible to add new axis and fit models to combinations, so if there is an easier way to achieve this I would be open to changing the schema.
EDIT
I have a partial solution based on Eggyal's answer but it is not extendable.
SELECT c.*, r.*, GROUP_CONCAT(a.bmi_id), GROUP_CONCAT(b.name) AS booking_models
FROM axis_channel c
CROSS JOIN axis_revenue_stream r
LEFT JOIN axis_channel_mappings cm ON cm.channel_id = c.id
LEFT JOIN axis_revenue_stream_mappings rm ON rm.revenue_stream_id = r.id
LEFT JOIN booking_model_assignment a ON (a.bmi_id = cm.assignment_id
AND a.bmi_id = rm.assignment_id)
OR (a.bmi_id = cm.assignment_id
AND rm.assignment_id IS NULL)
OR (cm.assignment_id IS NULL
AND a.bmi_id = cm.assignment_id)
LEFT JOIN booking_model b ON b.id = a.booking_model_id
GROUP BY c.id, r.id
But if I were to add more axes this query would grow way to cumbersome.
SELECT c.*, r.*, GROUP_CONCAT(b.name) AS booking_models
FROM axis_channel c
CROSS JOIN axis_revenue_stream r
LEFT JOIN axis_channel_mappings cm ON cm.channel_id = c.id
LEFT JOIN axis_revenue_stream_mappings rm ON rm.revenue_stream_id = r.id
LEFT JOIN booking_model_assignment a ON a.bmi_id = cm.assignment_id
AND a.bmi_id = rm.assignment_id
LEFT JOIN booking_model b ON b.id = a.booking_model_id
GROUP BY c.id, r.id
Can anyone help me with this query?
I have three tables (A;B;C)
A <--1....N---> B <--1....N---> C
I want all A rows having C.dates (the greatest)
SELECT A.*, MAX(C.dates)
FROM A
JOIN B ON B.A_fk = A.id
JOIN C ON C.B_fk = B.id
GROUP BY A.id
This JOIN will exclude results which wont have LEFT join. That is, if any row from A wont have B row or any row from B wont have any C row, then the row wont show. To overcome this you can use LEFT JOIN instead of JOIN.
SELECT A.*, MAX(C.dates)
FROM A
LEFT JOIN B ON B.A_fk = A.id
LEFT JOIN C ON C.B_fk = B.id
GROUP BY A.id
EDIT: Sorry didnt noticed that you needed the greatest value of C.data. There you have it. You have to use MAX function in SELECT and GROUP BY A.id
I'm coming across this situation alot, I'll have a query that will have one table needed in a join condition that may have no entries therefore requiring me to use a LEFT JOIN. I can't wrap my head around the syntax when it's used with more than 1 join.
I'll have:
SELECT A.*, B.*, C.*
FROM A, B, C
WHERE A.id = C.id
AND C.aid = A.id
AND B.cid = C.id
Along comes D with the possibility of being empty and I have to rewrite the query and run into problems.
How can I simply join D to any one of these tables?
You're much better off explicitly specifying all of your JOINs. That should make things much clearer.
SELECT A.*, B.*, C.*, D.*
FROM A
INNER JOIN C
ON C.aid = A.id
INNER JOIN B
ON B.cid = C.id
LEFT JOIN D
ON C.did = d.id
My advice is to never specify more than one column on FROM clause.
For clarity, it's better to always:
Use JOIN clause
Use aliases
Specify columns of joined tables on left side of equal sign
Example:
SELECT a.*, b.*, c.*
FROM ATable a
INNER JOIN BTable b
ON b.id = a.id
INNER JOIN CTable c
ON c.id = a.id
WHERE a.someColumn = 'something'
Not sure about MySQL, but in some other SQL flavors, you can use the same on UPDATES and DELETES, like:
DELETE FROM a
FROM ATable a
INNER JOIN BTable b
ON b.id = a.id
INNER JOIN CTable c
ON c.id = a.id
WHERE a.someColumn = 'something'
or
UPDATE a
SET something = newValue
FROM ATable a
INNER JOIN BTable b
ON b.id = a.id
INNER JOIN CTable c
ON c.id = a.id
WHERE a.someColumn = 'something'
The syntax below should help you. The basic premise is whatever table is listed LEFT is the required.. the table (or alias) on the right is optional. I understand you don't quite get it, and your syntax sample shows that (not meant to criticize) as you are joining from A -> C and C back to A on a different field. If this is the case where two fields are in the "C" table that BOTH point to A, you would re-join to A as a second alias...
select
Want.*,
Maybe.*,
SecondA.*,
B.*
From
A as Want
LEFT JOIN C as Maybe
on Want.ID = Maybe.ID
JOIN A as SecondA
on Maybe.AID = SecondA.ID
JOIN B
on Maybe.ID = B.cID
So, this query is stating I want everything from Table A (alias Want -- left side/first table in the list) Regardless of there being a match in Table C (alias Maybe) where the ID keys match.
Notice the next joins going down from "C" back to the second instance of "A" and table B. I have those as just joins... So the relationship between the "Maybe" alias, and that of second instance of "A" and "B" are JOIN (required).
Hopefully this gives some better clarification on HOW it works.
Now, for your real-life query. If you can describe what you are looking for, and your sample table structures / result expections, listing that could offer more explicit solution to your needs.
Hope this will help
SELECT
A.*, B.*, C.*
FROM A
inner join C on(A.id = C.id)
inner join B on(B.cid = C.id)
This is the query I'm performing (without some Joins that are not relevant):
SELECT a.*, c.id
FROM a
LEFT OUTER JOIN b ON a.id = b.id_anunciante
LEFT OUTER JOIN c ON c.id = b.id_rubro
GROUP BY a.id
Each row of "a" is linked with 1 to 5 rows in "b".
The problem is that GROUP BY has performance issues (it takes 10x or more using GROUP BY than not using it). I need to retrieve only one row of each member in "a".
How can I make this faster?
edit: I need to be able to filter by a.id AND/OR c.id. The resultset I should be getting is only 1 row per "valid" member of "a", meaning the rows that match the constraints. Rows that don't match the filters shouldn't be returned.
In my original query, this would be done this way:
SELECT a.*, c.id
FROM a
LEFT OUTER JOIN b ON a.id = b.id_anunciante
LEFT OUTER JOIN c ON c.id = b.id_rubro
WHERE c.id = 1
OR a.id = 1
GROUP BY a.id
a.id, b.id_anunciante, b.id_rubro, c.id are all indexes.
SELECT a.*,
(
SELECT c.id
FROM b
JOIN с
ON c.id = b.id_rubro
WHERE b.id_anunciante = a.id
-- add the ORDER BY condition to define which row will be selected.
LIMIT 1
)
FROM a
Create the index on b (id_anunciante) for this to work faster.
Update:
You don't need the OUTER JOINs here.
Rewrite your query as this:
SELECT a.*, c.id
FROM a
JOIN b
ON b.id_anunciante = a.id
JOIN c
ON c.id = b.id_rubro
WHERE a.id = 1
UNION ALL
SELECT a.*, 1
FROM a
WHERE EXISTS
(
SELECT NULL
FROM c
JOIN b
ON b.id_rubro = c.id
WHERE c.id = 1
AND b.id_anunciante = a.id
)
Add ORDER BY NULL to avoid the implicit sorting MySQL does when doing a group by.
I suppose you have indexes/PKs on a.id, b.id_anunciante, b.id_rubro and c.id ? I guess you could try adding a composite index on (b.id_anunciante, b.id_rubro) if your mysql version is not able to do an index merge.