I have the following query and would like to convert it to using a left outer join instead of a not in to see if it would run faster that way. It's currently taking this query about 40 seconds to run on our database. I'm not familiar enough with using outer joins for this type of thing to convert it myself.
select
c.contact_id as contact_id,
c.orgid as organization_id,
c.first_name as first_name,
c.last_name as last_name,
a.address_state as state
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
where a.address_state = 'OH'
and (c.orgid = 45 or c.orgid = 55)
and c.contact_id NOT IN (
select pc.contact_id
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
inner join cnx_contact_group_participant as gp on c.contact_id = gp.contact_id
inner join cnx_contact_participant_role as cr on gp.participant_role_uid = cr.participant_role_uid
inner join cnx_contact_group as cg on gp.group_uid = cg.group_uid
inner join cnx_contact_group_participant as pgp on cg.primary_participant_uid = pgp.participant_uid
inner join cnx_contact as pc on pgp.contact_id = pc.contact_id
where (c.orgid = 45 or c.orgid = 55)
and cr.name = 'Applicant'
);
select
c.columns
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
LEFT JOIN
(Subquery goes here) x
ON x.contact _id = c.contact_id
where a.participant_state = 'OH'
and c.orgid IN(45,55)
and x.contact_id IS NULL;
Related
First, I searched about the same problem but I didn't find an appropriate solution.
My problem is with the following code, it's returning wrong results:
SELECT FbID,FhID,
FbRef,
FbDate,
(D.AccName) AS DName,
FbQuan,
CONCAT(CategoryName,'-',ProductName) AS ProdName,
(C.AccName) AS CusName,
FhPurPrice,FbSalePrice,
(R.AccName) AS ResoName,
Curr1.CurrencyName,
Curr2.CurrencyName,
Plc1.PlaceName AS FhResoPlaceName,
Plc2.PlaceName AS FbCusPlaceName,
'linked' AS xLinkStatus,
1 AS xStatus
FROM tblfatora2 F2
INNER JOIN tblfatora1 F1 ON F1.FhRef = F2.FhRef
INNER JOIN tblproducts P ON P.ProductID = F1.FhProduct
INNER JOIN tblcategories CT ON CT.CategoryID = P.ProductCategory
INNER JOIN tblaccounts R ON R.AccID = F1.FhReso
INNER JOIN tblaccounts C ON C.AccID = F2.FbCus
INNER JOIN tblaccounts D ON D.AccID = F1.FhDriver
INNER JOIN tblcurrencies Curr1 ON C.AccCurrID = Curr1.CurrencyID
INNER JOIN tblcurrencies Curr2 ON R.AccCurrID = Curr2.CurrencyID
LEFT JOIN tblplaces Plc1 ON F1.FhResoPlace = Plc1.PlaceID
LEFT JOIN tblplaces Plc2 ON F2.FbCusPlace = Plc2.PlaceID
WHERE FIND_IN_SET(`FhID`, '18313,18314')
ORDER BY FbDate, FbID
the results that it gives me are: enter image description here
note: I use FIND_IN_SET here because I can't use (IN) where I use that SQL statement in a procedure inside vb.net code:
Public Sub MySql_GetLinked()
xDtAll = New DataTable()
Dim xPar(0) As MySqlParameter
xPar(0) = New MySqlParameter("#FhID", MySqlDbType.String) With {
.Value = LinkedFatora}
xClsMySql.GetData(xSqlLinked, xDtAll, xPar)
End Sub
So I fill the variable (LinkedFatora) by loop and I use the same SQL statement but I replace (WHERE FIND_IN_SET('FhID', '18313,18314')) with (WHERE FIND_IN_SET('FhID', #FhID)).
I looked for the error's reason but couldn't catch it.
I found the problem, it's changing (WHERE FIND_IN_SET(FhID, '18313,18314')) to (WHERE FIND_IN_SET(FbID, '18313,18314')):
SELECT FbID,FhID,
FbRef,
FbDate,
(D.AccName) AS DName,
FbQuan,
CONCAT(CategoryName,'-',ProductName) AS ProdName,
(C.AccName) AS CusName,
FhPurPrice,FbSalePrice,
(R.AccName) AS ResoName,
Curr1.CurrencyName,
Curr2.CurrencyName,
Plc1.PlaceName AS FhResoPlaceName,
Plc2.PlaceName AS FbCusPlaceName,
'linked' AS xLinkStatus,
1 AS xStatus
FROM tblfatora2 F2
INNER JOIN tblfatora1 F1 ON F1.FhRef = F2.FhRef
INNER JOIN tblproducts P ON P.ProductID = F1.FhProduct
INNER JOIN tblcategories CT ON CT.CategoryID = P.ProductCategory
INNER JOIN tblaccounts R ON R.AccID = F1.FhReso
INNER JOIN tblaccounts C ON C.AccID = F2.FbCus
INNER JOIN tblaccounts D ON D.AccID = F1.FhDriver
INNER JOIN tblcurrencies Curr1 ON C.AccCurrID = Curr1.CurrencyID
INNER JOIN tblcurrencies Curr2 ON R.AccCurrID = Curr2.CurrencyID
LEFT JOIN tblplaces Plc1 ON F1.FhResoPlace = Plc1.PlaceID
LEFT JOIN tblplaces Plc2 ON F2.FbCusPlace = Plc2.PlaceID
WHERE FIND_IN_SET(`FbID`, '18313,18314')
ORDER BY FbDate, FbID
Sorry, I'm a newbee here and can't get everyhing right at one time in beautiful code lists. I'm in a prephase to (let) launch my database on a website, but before I do I need to create a (total) view with inner join. And i get a message of the infamous 1054 unknown column in 'on clause'. Here is the actual MySQL script. Can someone help me succeeding the script as a view ? Around this code in the script it stucks (between the stars in the FROM SYNTAX): INNER JOIN zorgaanbieder AS zorgaanbieder_1 ON (zorgverlener.Zorgaanbieder_ID = zorgaanbieder_1.Zorgaanbieder_ID). Please see code below
CREATE VIEW `fetch_data` AS
SELECT
zorgactiviteiten.ZA_code,
zorgactiviteiten.ZA_naam,
zorgactiviteiten.ZA_omschr_consument,
zorgprofielklasse.ZPK_oms,
behandelingen.Behandeling_criteria,
aanspraak.Aanspraak_omschr,
aanspraak.Aanspraak_machtiging,
cluster.Cluster_naam,
diagnosen.Diagnose_code,
diagnosen.Diagnose_naam,
diagnosen.Diagnose_groep,
diagnosen.Diagnose_hoofdstuk,
specialisme.Specialisme_naam,
zorgproducten.ZP_code,
zorgproducten.ZP_naam,
zorgproducten.ZP_omschr_consument,
zorgproductgroepen.ZPG_naam,
declaratie.Declaratie_code,
declaratie.Declaratie_zorg,
contractueel.Contractueel_uitleg,
contract.Contract_zorg,
contract.Contract_verzekerd,
contract.Contract_uitsluiting,
zorgverzekeraar.Zorgverzekeraar_verzekeraar,
zorgverzekeraar.Zorgverzekering_verzekering,
zorgverzekeraar.Zorgverzekering_soort,
zorgverzekeraar.Zorgverzekering_zorgkeuze,
concern.Concern_inkoop,
concern.Concern_label,
zorgverlener.Zorgverlener_plaats,
zorgverlener.Zorgverlener_regio,
zorgverlener.Zorgverlener_soort,
zorgaanbieder.Zorgaanbieder_informatie,
zorgaanbieder.Zorgaanbieder_website,
zorgaanbieder.Zorgaanbieder_reviews,
zorgaanbieder.Zorgaanbieder_rapport,
zorgaanbieder.Zorgaanbieder_oordeel,
zorgaanbieder.Zorgaanbieder_prijslijst,
prijslijst.Prijslijst_tarief,
prijslijst.Prijslijst_actie,
prijslijst.Prijslijst_soort,
prijslijst.Prijslijst_jaar
FROM
concern_zorgaanbieder_prijslijst
INNER JOIN
prijslijst ON (concern_zorgaanbieder_prijslijst.Prijslijst_ID = prijslijst.Prijslijst_ID)
INNER JOIN
concern ON (concern_zorgaanbieder_prijslijst.Concern_ID = concern.Concern_ID)
INNER JOIN
zorgaanbieder ON (concern_zorgaanbieder_prijslijst.Zorgaanbieder_ID = zorgaanbieder.Zorgaanbieder_ID)
INNER JOIN
zorgaanbieder AS zorgaanbieder_1 ON (zorgverlener.Zorgaanbieder_ID = zorgaanbieder_1.Zorgaanbieder_ID)
INNER JOIN
zorgverlener ON (contract.Zorgverlener_koppel = zorgverlener.Zorgverlener_ID)
INNER JOIN
concern AS concern_1 ON (zorgverzekeraar.Concern_ID = concern_1.Concern_ID)
INNER JOIN
zorgverzekeraar ON (contract.Zorgverzekeraar_koppel = zorgverzekeraar.Zorgverzekeraar_ID)
INNER JOIN
contract ON (contractueel_contract.Contract_ID = contract.Contract_ID)
INNER JOIN
contractueel_contract ON (contractueel.Contractueel_ID = contractueel_contract.Contract_ID)
INNER JOIN
contractueel ON (contractueel_decaratie.Contractueel_ID = contractueel.Contractueel_ID)
INNER JOIN
contractueel_decaratie ON (declaratie.Declaratie_code = contractueel_decaratie.Declaratie_code)
INNER JOIN
declaratie ON (zorgproducten_declaratie.Declaratie_code = declaratie.Declaratie_code)
INNER JOIN
zorgproducten_declaratie ON (zorgproducten.ZP_code = zorgproducten_declaratie.ZP_code)
INNER JOIN
zorgproductgroepen ON (zorgproducten.ZPG_code = zorgproductgroepen.ZPG_code)
INNER JOIN
zorgproducten ON (dbc.ZP_code = zorgproducten.ZP_code)
INNER JOIN
specialisme ON (diagnosen.Specialisme_code = specialisme.Specialisme_code)
INNER JOIN
diagnosen ON (diagnosebereik.Diagnose_ID = diagnosen.Diagnose_ID)
INNER JOIN
diagnosebereik ON (dbc_diagnosebereik.Diagnosebereik_naam = diagnosebereik.Diagnosebereik_naam)
INNER JOIN
dbc_diagnosebereik ON (diagnosebereik.Diagnosebereik_naam = dbc_diagnosebereik.Diagnosebereik_naam)
INNER JOIN
dbc ON (dbc_diagnosebereik.DBC_koppel = dbc.DBC_koppel)
INNER JOIN
behandelingen ON (dbc.DBC_koppel = behandelingen.DBC_koppel)
INNER JOIN
cluster ON (behandelingen.Cluster_ID = cluster.Cluster_ID)
INNER JOIN
aanspraak ON (behandelingen.Aanspraak_code = aanspraak.Aanspraak_code)
INNER JOIN
zorgactiviteit_behandelingen ON (behandelingen.Behandeling_naam = zorgactiviteit_behandelingen.Behandeling_naam)
INNER JOIN
zorgactiviteiten ON (zorgactiviteit_behandelingen.ZA_code = zorgactiviteiten.ZA_code)
INNER JOIN
zorgprofielklasse ON (zorgactiviteiten.ZPK_code = zorgprofielklasse.ZPK_code)
Debugging advice: Start small, test, then by grow by small increments and re-test.
I believe you problem is INCORRECT SEQUENCE of joins, NOT incorrect column references. e.g.
## this will fail
select a.* from a
join c on b.id = c.id ## c attempts to join to b = incorrect sequence of joins
join b on a.id = b.id
Below, table zorgverlener attempts to join to contract but it has not yet been joined.
SELECT
czp.*
FROM concern_zorgaanbieder_prijslijst as czp
...
INNER JOIN zorgverlener ON (contract.Zorgverlener_koppel = zorgverlener.Zorgverlener_ID)
...
INNER JOIN contract ON (contractueel_contract.Contract_ID = contract.Contract_ID)
Other tips:
Do use table aliases
Don't use unnecessary parentheses
To correct that issue change the order of the joins:
SELECT
czp.*
FROM concern_zorgaanbieder_prijslijst as czp
...
INNER JOIN contract AS C ON contractueel_contract.Contract_ID = contract.Contract_ID
...
INNER JOIN zorgverlener ON c.Zorgverlener_koppel = zorgverlener.Zorgverlener_ID
but now we see that contractueel_contract needs to go above contract and so on it goes.
I believe a more correct sequence of jois is as follows, but there are 2 joins I cannot solve:
SELECT
czp.*
FROM concern_zorgaanbieder_prijslijst AS czp
INNER JOIN prijslijst ON czp.Prijslijst_ID = prijslijst.Prijslijst_ID
INNER JOIN concern ON czp.Concern_ID = concern.Concern_ID
## problems in the next 2 lines, seem to be recursve
INNER JOIN diagnosebereik ON dbc_diagnosebereik.Diagnosebereik_naam = diagnosebereik.Diagnosebereik_naam
INNER JOIN dbc_diagnosebereik ON diagnosebereik.Diagnosebereik_naam = dbc_diagnosebereik.Diagnosebereik_naam
INNER JOIN dbc ON dbc_diagnosebereik.DBC_koppel = dbc.DBC_koppel
INNER JOIN zorgproducten ON dbc.ZP_code = zorgproducten.ZP_code
INNER JOIN zorgproducten_declaratie ON zorgproducten.ZP_code = zorgproducten_declaratie.ZP_code
INNER JOIN declaratie ON zorgproducten_declaratie.Declaratie_code = declaratie.Declaratie_code
INNER JOIN contractueel_decaratie ON declaratie.Declaratie_code = contractueel_decaratie.Declaratie_code
INNER JOIN contractueel ON contractueel_decaratie.Contractueel_ID = contractueel.Contractueel_ID
INNER JOIN contractueel_contract ON contractueel.Contractueel_ID = contractueel_contract.Contract_ID
INNER JOIN contract ON contractueel_contract.Contract_ID = contract.Contract_ID
INNER JOIN zorgverlener ON contract.Zorgverlener_koppel = zorgverlener.Zorgverlener_ID
INNER JOIN zorgaanbieder ON czp.Zorgaanbieder_ID = zorgaanbieder.Zorgaanbieder_ID
INNER JOIN zorgaanbieder AS zorgaanbieder_1 ON zorgverlener.Zorgaanbieder_ID = zorgaanbieder_1.Zorgaanbieder_ID
INNER JOIN zorgverzekeraar ON contract.Zorgverzekeraar_koppel = zorgverzekeraar.Zorgverzekeraar_ID
INNER JOIN concern AS concern_1 ON zorgverzekeraar.Concern_ID = concern_1.Concern_ID
INNER JOIN zorgproducten_declaratie ON zorgproducten.ZP_code = zorgproducten_declaratie.ZP_code
INNER JOIN zorgproductgroepen ON zorgproducten.ZPG_code = zorgproductgroepen.ZPG_code
INNER JOIN diagnosen ON diagnosebereik.Diagnose_ID = diagnosen.Diagnose_ID
INNER JOIN specialisme ON diagnosen.Specialisme_code = specialisme.Specialisme_code
INNER JOIN behandelingen ON dbc.DBC_koppel = behandelingen.DBC_koppel
INNER JOIN cluster ON behandelingen.Cluster_ID = cluster.Cluster_ID
INNER JOIN aanspraak ON behandelingen.Aanspraak_code = aanspraak.Aanspraak_code
INNER JOIN zorgactiviteit_behandelingen ON behandelingen.Behandeling_naam = zorgactiviteit_behandelingen.Behandeling_naam
INNER JOIN zorgactiviteiten ON zorgactiviteit_behandelingen.ZA_code = zorgactiviteiten.ZA_code
INNER JOIN zorgprofielklasse ON zorgactiviteiten.ZPK_code = zorgprofielklasse.ZPK_code
Why the sql code below ignores
WHERE r.tt_schedules_id = '105'
in
SELECT DISTINCT r.load_date, u.url
FROM tt_results r
INNER JOIN url_lp u
ON u.lp_id <> r.lp_id
INNER JOIN tt_schedules s
ON s.tt_schedules_id = r.tt_schedules_id
WHERE r.tt_schedules_id = '105'
AND (DATE(NOW()) - DATE(r.load_date) <= 7)
?
Basically, it returns everything with any
tt_schedules_id
There are 5 tables:
url_lp, tt_results (table having foreign keys in url_lp, tt_schedules and
tt_tags), tt_schedules, tt_tags(not used) and tt_schedules_url_lp_hub
If you draw the scheme on the paper you should be clear.
I fixed it:
SELECT u.url FROM url_lp u
INNER JOIN tt_schedules_url_lp_hub hub
ON hub.lp_id = u.lp_id
INNER JOIN tt_schedules s
ON hub.tt_schedules_id = s.tt_schedules_id
WHERE s.tt_schedules_id = '105'
AND u.lp_id NOT IN
(SELECT r.lp_id FROM tt_results r WHERE r.tt_schedules_id = '105')
Cheers.
Please am having difficulty in optimizing this query. What am trying to achieve is to join about 8 tables, of which only about 3 of the tables contains large data (1.5m records). This query returns expected records but is taking 1min to run which is bad.
I know it can be optimized to perform far better, pls i need assistance from you experts. I have index on the fields used for join already.
SELECT topic_id,
topic_title,
unit_name_abbrev,
sch_name_abbrev,
picture_small_url AS thumbnail,
profile_pix_upload_path,
first_name,
last_name,
topic_poster,
topic_replies,
topic_views,
topic_last_post_time AS topic_post_time,
sch_sub_forum_id
FROM (_sch_forum_topics
INNER JOIN _users
ON ( _users.userid = _sch_forum_topics.topic_poster )
INNER JOIN _profile
ON _profile.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _level
ON _class.level_id = _level.id
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON _faculty.schid = _university.schid)
WHERE _sch_forum_topics.sch_sub_forum_id = 4
ORDER BY _sch_forum_topics.topic_last_post_time DESC
LIMIT 0, 15
Try to filter before making JOIN's.
SELECT topic_id,
topic_title,
unit_name_abbrev,
sch_name_abbrev,
picture_small_url AS thumbnail,
profile_pix_upload_path,
first_name,
last_name,
topic_poster,
topic_replies,
topic_views,
topic_last_post_time AS topic_post_time,
sch_sub_forum_id
FROM
( select * FROM sch_forum_topics WHERE sch_sub_forum_id = 4
ORDER BY topic_last_post_time DESC
LIMIT 0, 15 ) main
INNER JOIN _users
ON ( _users.userid = main.topic_poster )
INNER JOIN _profile
ON _profile.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _level
ON _class.level_id = _level.id
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON _faculty.schid = _university.schid);
I've got the query below that's pulling data from a number of tables to create an update:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
It works fine but when there are plenty of records in papr_down then it takes ages to process. Any ideas about how it can be optimized?
What I think is the join between the emailAddress is the issue here, you can try out with the join with the Id's.
If you provide us the screen shot of the below query ::
EXPLAIN Select * from
en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
WHERE sd.fieldid = 33
As I know we should use joins only for the columns which are preset in SELECT clause and for other joins we should implement using WHERE clause
Please try following query:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls
on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down AS pd1
INNER JOIN email.papr_data AS pd2 on pd1.paper_id = pd2.id
WHERE
email.papr_exam.id in (select exam from email.papr_data where exam = 1)
AND
email.papr_levl.id in (select level from email.papr_data where level = 4 )
AND
email.papr_subj.id in (select subject from email.papr_data)
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
I can not execute this at my machine since i don't have the schema