MySql convert nested SELECT IN queries into JOINs - mysql

I read that using IN in MySql queries causes a slowdown in performance, unlike in Oracle and that a JOIN should be used instead. I am able to convert simple queries, but I am struggling with more complex ones that contain nested SELECTs. For example:
update Records set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and LastUpdated!=&
and Pk in (select RecordPk from GroupRecordLink
where GroupPk in (select Pk from Groups where ActiveStatus!=5 and
DateTimeStamp>&))
I had a go rewriting it by following this post , but I am not sure my result is correct.
update Records r join (select distinct RecordPk from GroupRecordLink grl join
Groups g on grl.Pk = g.Pk where g.ActiveStatus!=5 and g.DateTimeStamp>&) s
using (Pk) set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and DateTimeStamp>&
Thanks

Try this:
UPDATE Records
INNER JOIN GroupRecordLink ON Records.Pk = GroupRecordLink.RecordPk
INNER JOIN Groups ON GroupRecordLink.GroupPk = Groups.Pk AND Groups.ActiveStatus != 5
SET Records.ActiveStatus = 0,
Records.TransactionStatus = 0,
Records.LastUpdated = &
WHERE Records.ActiveStatus != 5
AND Records.LastUpdated != &;

In Mysql you could use an explicit join for update (you must change & witha proper value )
update Records
INNER JOIN GroupRecordLink on GroupRecordLink.RecordPk = Records.PK
INNER JOIN Groups on ( GroupRecordLink.GroupPk = Groups.Pk
and Groups.ActiveStatus!=5
and Groups.DateTimeStamp>&)
set ActiveStatus=0,
TransactionStatus=0,
LastUpdated=&

Related

How to optimize a query with inner join

My mysql query is too slow and i don't know how to optimize it. My webapp cant load this query because take too much time to run and the webserver have a limit time to get the result.
SELECT rc.trial_id,
rc.created,
rc.date_registration,
rc.agemin_value,
rc.agemin_unit,
rc.agemax_value,
rc.agemax_unit,
rc.exclusion_criteria,
rc.study_design,
rc.expanded_access_program,
rc.number_of_arms,
rc.enrollment_start_actual,
rc.target_sample_size,
(select name from repository_institution where id = rc.primary_sponsor_id) as
primary_sponsor,
(select label from vocabulary_studytype where id = rc.study_type_id) as study_type,
(select label from vocabulary_interventionassigment where id =
rc.intervention_assignment_id) as intervention_assignment,
(select label from vocabulary_studypurpose where id = rc.purpose_id) as study_purpose,
(select label from vocabulary_studymasking where id = rc.masking_id) as study_mask,
(select label from vocabulary_studyallocation where id = rc.allocation_id) as
study_allocation,
(select label from vocabulary_studyphase where id = rc.phase_id) as phase,
(select label from vocabulary_recruitmentstatus where id = rc.recruitment_status_id) as
recruitment_status,
GROUP_CONCAT(vi.label)
FROM
repository_clinicaltrial rc
inner JOIN repository_clinicaltrial_i_code rcic ON rcic.clinicaltrial_id = rc.id JOIN
vocabulary_interventioncode vi ON vi.id = rcic.interventioncode_id
GROUP BY rc.id;
Using inner join instead join could be a solution?
Changing to JOINs vs continuous selects per every row will definitely improve. Also, since you are using MySQL, using the keyword "STRAIGHT_JOIN" tells MySQL to do the query in the order I provided. Since your "rc" table is the primary and all the others are lookups, this will make MySQL use it in that context rather than hoping some other lookup table be the basis of the rest of the joins.
SELECT STRAIGHT_JOIN
rc.trial_id,
rc.created,
rc.date_registration,
rc.agemin_value,
rc.agemin_unit,
rc.agemax_value,
rc.agemax_unit,
rc.exclusion_criteria,
rc.study_design,
rc.expanded_access_program,
rc.number_of_arms,
rc.enrollment_start_actual,
rc.target_sample_size,
ri.name primary_sponsor,
st.label study_type,
via.label intervention_assignment,
vsp.label study_purpose,
vsm.label study_mask,
vsa.label study_allocation,
vsph.label phase,
vrs.label recruitment_status,
GROUP_CONCAT(vi.label)
FROM
repository_clinicaltrial rc
JOIN repository_clinicaltrial_i_code rcic
ON rc.id = rcic.clinicaltrial_id
JOIN vocabulary_interventioncode vi
ON rcic.interventioncode_id = vi.id
JOIN repository_institution ri
on rc.primary_sponsor_id = ri.id
JOIN vocabulary_studytype st
on rc.study_type_id = st.id
JOIN vocabulary_interventionassigment via
on rc.intervention_assignment_id = via.id
JOIN vocabulary_studypurpose vsp
ON rc.purpose_id = vsp.id
JOIN vocabulary_studymasking vsm
ON rc.masking_id = vsm.id
JOIN vocabulary_studyallocation vsa
ON rc.allocation_id = vsa.id
JOIN vocabulary_studyphase vsph
ON rc.phase_id = vsph.id
JOIN vocabulary_recruitmentstatus vrs
ON rc.recruitment_status_id = vrs.id
GROUP BY
rc.id;
One final note. You are using a GROUP BY and applying to the GROUP_CONCAT() which is ok. However, proper group by says you need to group by all non-aggregate columns, which in this case is every other column in the list. You may know this, and the fact the lookups will be the same based on the "rc" associated columns, but its not good practice to do so.
Your joins and subqueries are probably not the problem. Assuming you have correct indexes on the tables, then these are fast. "Correct indexes" means that the id column is the primary key -- a very reasonable assumption.
My guess is that the GROUP BY is the performance issue. So, I would suggest structuring the query with no `GROUP BY:
select . . .
(select group_concat(vi.label)
from repository_clinicaltrial_i_code rcic
vocabulary_interventioncode vi
on vi.id = rcic.interventioncode_id
where rcic.clinicaltrial_id = rc.id
)
from repository_clinicaltrial rc ;
For this, you want indexes on:
repository_clinicaltrial_i_code(clinicaltrial_id, interventioncode_id)
vocabulary_interventioncode(id, label)

How do I add a criteria to a query on one table but keep all the records from another?

I have the following query:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
I know the joining is convoluted but the original db is built without a primary key. The above works fine and importantly pulls all the records from games_atp. I now want to add a criteria into this to pull only certain K1 records from odds_atp. I added a WHERE clause as follows:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
WHERE (((odds_atp.ID_B_O)=2));
However, this overides the left join and only pulls records from games_atp where there is a corresponding record in odds_atp with ID_B_O = 2. How do I keep the criteria and all the records in games_atp? Thanks in advance.
Your current where condition will filter your final result, hence you are only seeing id_B_O = 2.
However, you could also add the wehre condition directly into your left join.
something like this.
SELECT
games_atp.ID1_G, odds_atp.K1
FROM
games_atp
LEFT JOIN odds_atp ON
(
(odds_atp.ID_B_O =2)
AND
(
(games_atp.ID1_G = odds_atp.ID1_O)
AND (games_atp.ID2_G = odds_atp.ID2_O)
AND (games_atp.ID_T_G = odds_atp.ID_T_O)
AND (games_atp.ID_R_G = odds_atp.ID_R_O)
)
);
or you could also take advantage of sub-queries

How to format my UPDATE statement correctly? Getting not unique table/alias error

I am getting a "... not unique table/alias 'plots' ..." error when trying to run the following UPDATE statement:
UPDATE homestead.plots
INNER JOIN homestead.graphs
ON homestead.drivers.id = homestead.graphs.driver_id
INNER JOIN homestead.plots
ON homestead.plots.graph_id = homestead.graphs.id
SET homestead.plots.yAxis = homestead.plots.yAxis + 3.4
WHERE homestead.graphs.name = "DI";
Even though the below SELECT statement works fine, and returns the results I want:
SELECT homestead.graphs.driver_id, homestead.drivers.MarketingNo, homestead.graphs.name, homestead.plots.xAxis, homestead.plots.yAxis
FROM homestead.drivers
INNER JOIN homestead.graphs
ON homestead.drivers.id = homestead.graphs.driver_id
INNER JOIN homestead.plots
ON homestead.plots.graph_id = homestead.graphs.id
WHERE homestead.graphs.name = "DI";
Any ideas how to fix my UPDATE statement to work? I've done a lot of research online but cannot understand why this doesn't work.
There are several flaws with your UPDATE statement, for example:
table plots is referenced twice (in the UPDATE and in a JOIN) and not aliased (this is causing the error that you are getting)
you are referring to column id in table homestead.graphs, but this table is not part of any join
Based on your SELECT query, I would try and phrase your UPDATE as follows:
UPDATE homestead.plots p
INNER JOIN homestead.graphs g ON p.graph_id = g.id AND g.name = "DI"
INNER JOIN homestead.drivers d ON d.id = g.driver_id
SET p.yAxis = p.yAxis + 3.4

MySQL Replacing IN and EXISTS with joins in sub sub queries

So, this query is currently used in a webshop to retrieve technical data about articles.
It has served its purpose fine except the amount of products shown have increased lately resulting in unacceptable long loading times for some categories.
For one of the worst pages this (and some other queries) get requested about 80 times.
I only recently learned that MySQL does not optimize sub-queries that don't have a depending parameter to only run once.
So if someone could help me with one of the queries and explain how you can replace the in's and exists's to joins, i will probably be able to change the other ones myself.
select distinct criteria.cri_id, des_texts.tex_text, article_criteria.acr_value, article_criteria.acr_kv_des_id
from article_criteria, designations, des_texts, criteria, articles
where article_criteria.acr_cri_id = criteria.cri_id
and article_criteria.acr_art_id = articles.art_id
and articles.art_deliverystatus = 1
and criteria.cri_des_id = designations.des_id
and designations.des_lng_id = 9
and designations.des_tex_id = des_texts.tex_id
and criteria.cri_id = 328
and article_criteria.acr_art_id IN (Select distinct link_art.la_art_id
from link_art, link_la_typ
where link_art.la_id = link_la_typ.lat_la_id
and link_la_typ.lat_typ_id = 17484
and link_art.la_ga_id IN (Select distinct link_ga_str.lgs_ga_id
from link_ga_str, search_tree
where link_ga_str.lgs_str_id = search_tree.str_id
and search_tree.str_type = 1
and search_tree.str_id = 10132
and EXISTS (Select *
from link_la_typ
where link_la_typ.lat_typ_id = 17484
and link_ga_str.lgs_ga_id = link_la_typ.lat_ga_id)))
order by article_criteria.acr_value
I think this one is the main badguy with sub-sub-sub-queries
I just noticed i can remove the last exist and still get the same results but with no increase in speed, not part of the question though ;) i'll figure out myself whether i still need that part.
Any help or pointers are appreciated, if i left out some useful information tell me as well.
I think this is equivalent:
SELECT DISTINCT c.cri_id, dt.tex_text, ac.acr_value, ac.acr_kv_des_id
FROM article_criteria AS ac
JOIN criteria AS c ON ac.acr_cri_id = c.cri_id
JOIN articles AS a ON ac.acr_art_id = a.art_id
JOIN designations AS d ON c.cri_des_id = d.des_id
JOIN des_texts AS dt ON dt.tex_id = d.des_tex_id
JOIN (SELECT distinct la.la_art_id
FROM link_art AS la
JOIN link_la_typ AS llt ON la.la_id = llt.lat_la_id
JOIN (SELECT DISTINCT lgs.lgs_ga_id
FROM link_ga_str AS lgs
JOIN search_tree AS st ON lgs.lgs_str_id = st.str_id
JOIN link_la_typ AS llt ON lgs.lgs_ga_id = llt.lat_ga_id
WHERE st.str_type = 1
AND st.str_id = 10132
AND llt.lat_typ_id = 17484) AS lgs
ON la.la_ga_id = lgs.lgs_ga_id
WHERE llt.lat_typ_id = 17484) AS la
ON ac.acr_art_id = la.la_art_id
WHERE a.art_deliverystatus = 1
AND d.des_lng_id = 9
AND c.cri_id = 328
ORDER BY ac.acr_value
All the IN <subquery> clauses can be replaced with JOIN <subquery>, where you then JOIN on the column being tested equaling the column returned by the subquery. And the EXISTS test is converted to a join with the table, moving the comparison in the subquery's WHERE clause into the ON clause of the JOIN.
It's probably possible to flatten the whole thing, instead of joining with subqueries. But I suspect performance will be poor, because this won't reduce the temporary tables using DISTINCT. So you'll get combinatorial explosion in the resulting cross product, which will then have to be reduced at the end with the DISTINCT at the top.
I've converted all the implicit joins to ANSI JOIN clauses, to make the structure clearer, and added table aliases to make things more readable.
In general, you can convert a FROM tab1 WHERE ... val IN (SELECT blah) to a join like this.
FROM tab1
JOIN (
SELECT tab1_id
FROM tab2
JOIN tab3 ON whatever = whatever
WHERE whatever
) AS sub1 ON tab1.id = sub1.tab1_id
The JOIN (an inner join) will drop the rows that don't match the ON condition from your query.
If your tab1_id values can come up duplicate from your inner query, use SELECT DISTINCT. But don't use SELECT DISTINCT unless you need to; it is costly to evaluate.

Microsoft Access Update Statement with Inner Joins and Subqueries

I am having a tough time figuring out how to do this update query. Basically I need to update a table named tblOpenJobs. It needs to be updated with the dbo_WorkOrders table with the Max Install date. But there is not direct relationship between those two tables you need to have the dbo_premise table between. Here is my query, what am I doing wrong?
UPDATE tblOpenJobs
INNER JOIN (dbo_Premise INNER JOIN dbo_WorkOrders w (WHERE w.InstallDate IN
(SELECT MAX(InstallDate) FROM dbo_WorkOrders WHERE dbo_WorkOrders.PremiseKey = w.PremiseKey))
ON (dbo_Premise.PremiseKey = w.PremiseKey)
ON tblOpenJobs.ServiceOrderNum = dbo_Premise.AccountNumber
SET tblOpenJobs.InstallerID = w.InstallerID,
tblOpenJobs.InstallDate= w.InstallDate,
tblOpenJobs.New_Serial_num= w.NewSerial,
tblOpenJobs.Old_Reading= w.OldRead;
I checked this in Access 2007 query window:
Your query seems neither Transact-SQL, neither Access, as the two have different syntax.
In Access, table aliasing must use the keyword AS, while Transact-SQL does not require:
UPDATE ((tblOpenJobs
INNER JOIN dbo_Premise
ON tblOpenJobs.ServiceOrderNum = dbo_Premise.AccountNumber)
INNER JOIN dbo_WorkOrders AS w
ON dbo_Premise.PremiseKey = w.PremiseKey)
SET tblOpenJobs.InstallerID = w.InstallerID,
tblOpenJobs.InstallDate = w.InstallDate,
tblOpenJobs.New_Serial_num = w.NewSerial,
tblOpenJobs.Old_Reading = w.OldRead
WHERE (w.InstallDate IN
(SELECT MAX(InstallDate)
FROM dbo_WorkOrders
WHERE dbo_WorkOrders.PremiseKey = w.PremiseKey))
This is correct in syntax, but I'm not sure it can update your data, as multi-table linked update is not easy in Access.