proper indexing for fast retrieval in mysql - mysql

SELECT A.*
FROM
(SELECT *
FROM
(SELECT os.connectioninfoid,
os.schemaname,
con.name AS connectionname,
wiki.wikitext,
ot.oeschemaid,
ot.tablename,
ot.tabledescription,
ot.rowcount,
oc.*
FROM oecolumn oc
JOIN oetable ot ON (oc.oetableid = ot.oetableid)
JOIN oeschema os ON os.oeschemaid = ot.oeschemaid
JOIN connectioninfo con ON os.connectioninfoid = con.connectioninfoid
JOIN acl_object_identity oi ON oi.object_id_identity = ot.oetableid
JOIN acl_class CLASS ON (oi.object_id_class = class.id)
JOIN acl_entry entry ON (entry.acl_object_identity = oi.id)
JOIN acl_sid sid ON (entry.sid = sid.id)
JOIN ROLES ROLE ON (sid.sid = role.authority)
JOIN user_role usr ON (usr.roleid = role.roleid)
LEFT OUTER JOIN wiki ON oc.oecolumnid = wiki.wikiobjectid
AND wikiobject = 'oecolumn'
AND wiki.wikitext LIKE '%'
WHERE ot.type='DB'
AND class.class = 'com.ovaledge.oasis.domain.OeTable'
AND entry.mask = 1
AND usr.userid = 'admin' ) hts
WHERE tablename LIKE '%'
AND columnname LIKE '%'
AND columndescription LIKE '%' )A
I have this query what should be indexed for fast retrieval.

Related

Error near Group and order by while converting ORACLE query to Aurora DB MYSQL query

iam trying to convert following ORACLE query into Aurora DB MYSQL query.
Changed few oracle functions to mysql functions.
SELECT MIN(TP.TP_ID) TP_ID,
TC_GRP.TC_GRP_NB TC_GRP_NB,
MIN(TC.TC_ID) TC_ID,
MIN(TC.FAC_ID) FAC_ID,
LISTAGG(TC_GST.TXN_IDVL_PTY_ID, ',') WITHIN GROUP (ORDER BY TC_GST.TXN_IDVL_PTY_ID) AS TXN_IDVL_PTY_ID
FROM RES_MGMT.TP
INNER JOIN RES_MGMT.TPS
ON TPS.TP_ID = TP.TP_ID
INNER JOIN RES_MGMT.TC_GRP
ON TC_GRP.TPS_ID = TPS.TPS_ID
INNER JOIN RES_MGMT.TC
ON (TC.TC_GRP_NB = TC_GRP.TC_GRP_NB AND
TC.TC_TYP_NM = 'AccommodationComponent' AND
TC.TRVL_STS_NM = 'Booked' AND TC.UPGRD_TC_ID IS NULL)
INNER JOIN RES_MGMT.TC_GST
ON TC_GST.TC_ID = TC.TC_ID
WHERE TC_GRP.ADV_INTERNT_CHKIN_IN = 'Y'
AND TPS.SRC_ACCT_CTR_ID = 2
AND TC.FAC_ID in (80010383)
AND TRUNC(TC.TC_STRT_DTS) = TO_DATE('20210930', 'yyyyMMdd')
AND EXISTS (SELECT *
FROM RES_MGMT.TC TC1
WHERE TC1.TC_GRP_NB = TC.TC_GRP_NB
AND TC1.TC_TYP_NM = 'AdmissionComponent'
AND TC1.TRVL_STS_NM = 'Booked')
GROUP BY TC_GRP.TC_GRP_NB;
MySQLQuery has modified as follows:
SELECT MIN(TP.TP_ID) TP_ID,
TC_GRP.TC_GRP_NB TC_GRP_NB,
MIN(TC.TC_ID) TC_ID,
MIN(TC.FAC_ID) FAC_ID,
LISTAGG(TC_GST.TXN_IDVL_PTY_ID, ',') WITHIN GROUP (ORDER BY TC_GST.TXN_IDVL_PTY_ID) AS TXN_IDVL_PTY_ID
FROM RES_MGMT.TP
INNER JOIN RES_MGMT.TPS
ON TPS.TP_ID = TP.TP_ID
INNER JOIN RES_MGMT.TC_GRP
ON TC_GRP.TPS_ID = TPS.TPS_ID
INNER JOIN RES_MGMT.TC
ON (TC.TC_GRP_NB = TC_GRP.TC_GRP_NB AND
TC.TC_TYP_NM = 'AccommodationComponent' AND
TC.TRVL_STS_NM = 'Booked' AND TC.UPGRD_TC_ID IS NULL)
INNER JOIN RES_MGMT.TC_GST
ON TC_GST.TC_ID = TC.TC_ID
WHERE TC_GRP.ADV_INTERNT_CHKIN_IN = 'Y'
AND TPS.SRC_ACCT_CTR_ID = 2
AND TC.FAC_ID in (80010385)
AND DATE(TC.TC_STRT_DTS) = STR_TO_DATE('20210930', '%Y-%m-%d')
AND EXISTS (SELECT *
FROM RES_MGMT.TC TC1
WHERE TC1.TC_GRP_NB = TC.TC_GRP_NB
AND TC1.TC_TYP_NM = 'AdmissionComponent'
AND TC1.TRVL_STS_NM = 'Booked')
GROUP BY TC_GRP.TC_GRP_NB;
Iam getting mysql syntax error near WITHIN GROUP.
Kindly suggest what need to be changed.

how to join 10 table by using inner joins and group by condition

i want to convert this query into joins , this is a task which i got from my manger that Please convert the query below into Join format.
SELECT
capder.cder_id,
capman.cman_name AS Manufacturer,
caprange.cran_name AS Range1,
capmod.cmod_name AS Model,
capmod.cmod_code AS ModelCode,
capder.cder_doors AS Doors,
nvdbodystyle.bs_description AS BodyStyle,
captrim.ctrim_name AS Trim1,
capder.cder_name as Trim2,
capder.cder_drivetrain AS DriveTrain,
nvdmodelyear.MY_ref AS DerivativeModelYear,
group_concat(distinct(nvddictonaryoption.DO_Description) order by nvddictonaryoption.DO_Description SEPARATOR '|') AS ExteriorColour
FROM capman,
caprange,
capmod,
nvdbodystyle,
capder,
nvdmodelyear,
captrim,
nvdoptions,
nvddictonaryoption,
nvddictionarycategory
WHERE capman.cman_code = caprange.cran_mantextcode
AND caprange.cran_code = capmod.cmod_rancode
AND nvdbodystyle.bc_code = capmod.cmod_bodystyle
AND capmod.cmod_code = capder.cder_modcode
AND capder.cder_id = nvdmodelyear.my_id
AND captrim.ctrim_code = capder.cder_trimcode
AND nvdoptions.OPT_Id = capder.cder_id
AND nvdoptions.opt_optioncode = nvddictonaryoption.DO_OptionCode
AND nvddictionarycategory.dc_catcode = nvddictonaryoption.do_catcode
AND nvddictionarycategory.dc_cth_type = 'C'
AND SUBSTR(nvdmodelyear.MY_ref,1,4) > 2006
group by cder_id,SUBSTR(nvdmodelyear.MY_ref,1,4),captrim.ctrim_name
MySQL workbench 6.3 CE.
i tried this way
SELECT
cd.cder_id,
cm.cman_name AS Manufacturer,
cr.cran_name AS Range1,
cmd.cmod_name AS Model,
cmd.cmod_code AS ModelCode,
cd.cder_doors AS Doors,
nb.bs_description AS BodyStyle,
ct.ctrim_name AS Trim1,
cd.cder_name as Trim2,
cd.cder_drivetrain AS DriveTrain,
nmy.MY_ref AS DerivativeModelYear,
group_concat(distinct(ndo.DO_Description) order by ndo.DO_Description SEPARATOR '|') AS ExteriorColour
FROM capman cm
inner join caprange cr on cm.cman_code = cr.cran_mantextcode
inner join capmod cmd on cr.cran_code = cmd.cmod_rancode
inner join nvdbodystyle nb on nb.bc_code = cmd.cmod_bodystyle
inner join capder cd on cmd.cmod_code = cd.cder_modcode
inner join nvdmodelyear nmy on cd.cder_id = nmy.my_id
inner join captrim ct on ct.ctrim_code = cd.cder_trimcode
inner join nvdoptions nop on nop.OPT_Id = cd.cder_id
inner join nvddictonaryoption ndo on nop.opt_optioncode = ndo.DO_OptionCode
inner join nvddictionarycategory ndc on ndc.dc_catcode = ndo.do_catcode AND ndc.dc_cth_type = 'C' AND SUBSTR(nmy.MY_ref,1,4) > 2006
group by cder_id,SUBSTR(nmy.MY_ref,1,4),ct.ctrim_name;
am i did correct? it is giving correct result but it is taking too long to execute and get results. is there any better way to execute this query only with joins syntax and i don't have access to put index to the table

using mysql what will be the fastest way to join tables

i have two tables "User" And "UsersSettings" .
and i want to select User's where the users settings is something
what query will be more fast ?
SELECT u.*, us.someSettings FROM User u,UserSettings us
WHERE u.id = us.user_id
AND us.somesettings = somevalue
AND u.someProperty = someOtherValue
AND u.someProperty1 = someOtherValue1
AND u.someProperty2 = someOtherValue2
AND u.someProperty3 = someOtherValue3
AND u.someProperty4 = someOtherValue4
AND us.someUSProperty = someUSvalue
OR
SELECT u.*, us.someSettings FROM User u
LEFT JOIN UserSettings us ON us.user_id = u.id
WHERE us.somesettings = somevalue
AND u.someProperty = someOtherValue
AND u.someProperty1 = someOtherValue1
AND u.someProperty2 = someOtherValue2
AND u.someProperty3 = someOtherValue3
AND u.someProperty4 = someOtherValue4
AND us.someUSProperty = someUSvalue
can you help me please
thank you
The correct query is an inner join:
SELECT u.*, us.someSettings
FROM User u INNER JOIN
UserSettings us
ON us.user_id = u.id
WHERE us.somesettings = somevalue
AND u.someProperty = someOtherValue
AND u.someProperty1 = someOtherValue1
AND u.someProperty2 = someOtherValue2
AND u.someProperty3 = someOtherValue3
AND u.someProperty4 = someOtherValue4
AND us.someUSProperty = someUSvalue;
You have a filtering condition on UserSettings, so the outer join is turned to an inner join anyway. You should avoid implicit join syntax and put the join conditions explicitly in an on clause. This is for readability and maintainability. Both versions should have the same performance.

Mysql query with joined subqueries gives no result

i have a huge query:
SELECT stories.*
FROM stories stories
JOIN (SELECT tstories.uid
FROM stories tstories
JOIN tours_stories_mm mmt
ON tstories.uid = mmt.uid_foreign
JOIN tours tours
ON tours.uid = mmt.uid_local
JOIN tours_countries_rel tourcountryrel
ON tours.uid = tourcountryrel.tourid
JOIN countries tcountries
ON tcountries.uid = tourcountryrel.countryid
WHERE tcountries.uid = ?
AND tours.deleted = 0
AND tours.hidden = 0
AND tstories.deleted = 0
AND tstories.hidden = 0) as tourstories
JOIN (SELECT cstories.uid
FROM stories cstories
JOIN individualtourcomponents_stories_mm mmc
ON (cstories.uid = mmc.uid_foreign)
JOIN individualtourscomponents components
ON (components.uid = mmc.uid_local)
JOIN individualtourcomponents_countries_rel componentcountryrel
ON (components.uid = componentcountryrel.componentid)
JOIN countries ccountries
ON (ccountries.uid = componentcountryrel.countryid)
WHERE ccountries.uid = ?
AND components.deleted = 0
AND components.hidden = 0
AND cstories.deleted = 0
AND cstories.hidden = 0) as componentstories
WHERE stories.uid = componentstories.uid
OR stories.uid = tourstories.uid
GROUP BY stories.uid`
each of the subqueries work for themselves and if both subqueries have the same result, i get an result on the whole query. If only one subquery has a result i get nothing. Simplyfied it could be written like that:
SELECT stories.*
FROM stories
JOIN (SELECT * FROM table_a WHERE x=5) as table_a
JOIN (SELECT * form table_b where x=5) as table_b
WHERE stories.uid = table_a.uid OR stories.uid = table_b.uid
GROUP BY stories.uid
What am i doing wrong?
It seems like you should be using an OUTER JOIN instead of INNER JOIN to join your source tables.
Using the simplified query example, it might look like this:
SELECT stories.*
FROM stories
LEFT OUTER JOIN (SELECT * FROM table_a WHERE x=5) as table_a
ON stories.uid = table_a.uid
LEFT OUTER JOIN (SELECT * form table_b where x=5) as table_b
ON stories.uid = table_b.uid
GROUP BY stories.uid
UPDATE
Based on your comment it sounds like you actually want to get the distinct union of the the two sub queries.
If that's the case, you could modify your original query like this:
(SELECT tstories.*
FROM stories tstories
JOIN tours_stories_mm mmt
ON tstories.uid = mmt.uid_foreign
JOIN tours tours
ON tours.uid = mmt.uid_local
JOIN tours_countries_rel tourcountryrel
ON tours.uid = tourcountryrel.tourid
JOIN countries tcountries
ON tcountries.uid = tourcountryrel.countryid
WHERE tcountries.uid = ?
AND tours.deleted = 0
AND tours.hidden = 0
AND tstories.deleted = 0
AND tstories.hidden = 0
)
UNION DISTINCT
(SELECT cstories.*
FROM stories cstories
JOIN individualtourcomponents_stories_mm mmc
ON (cstories.uid = mmc.uid_foreign)
JOIN individualtourscomponents components
ON (components.uid = mmc.uid_local)
JOIN individualtourcomponents_countries_rel componentcountryrel
ON (components.uid = componentcountryrel.componentid)
JOIN countries ccountries
ON (ccountries.uid = componentcountryrel.countryid)
WHERE ccountries.uid = ?
AND components.deleted = 0
AND components.hidden = 0
AND cstories.deleted = 0
AND cstories.hidden = 0
)

Converter sql query to Linq

I need to convert this sql query to linq to sql and the result returns a IEnumerable:
select VisualAidName, v.VisualAidID, vs.VisualAidStatusName,
br.BrandName, v.IsEnabled, v.VisualAidCode, v.DateApproved,
br.BrandID, type, UserFirstName+ ' ' + UserLastName as name, AreaID
from VisualAids v inner join VisualAidStatus vs
on v.VisualAidStatusId = vs.VisualAidStatusId
inner join brands br
on v.BrandID = br.BrandId
inner join VisualAids_Areas_Link vareas
on v.VisualAidID = vareas.VisualAidID
left join users us
on v.Owner = us.UserID
where
AreaID IN (
select areaid
from Users inner join Users_Area_Link
on Users.UserID = Users_Area_Link.UserID
where Users.UserID= 3
)
I did this:
IEnumerable<Visual_Aid> visualAll = from v in Context.VisualAids
join vs in Context.VisualAidStatus on v.VisualAidStatusId equals vs.VisualAidStatusId
join br in Context.Brands on v.BrandID equals br.BrandId
join us in Context.Users on v.Owner equals us.UserID into vadis
from x in vadis.DefaultIfEmpty()
select new Visual_Aid()
{
VisualAid_Name = v.VisualAidName,
VisualAid_Id = v.VisualAidID,
VisualAid_StatusName = vs.VisualAidStatusName,
VisualAid_BrandsName = br.BrandName,
VisualAid_IsEnabled = bool.Parse(v.IsEnabled.ToString()),
VisualAid_Code = v.VisualAidCode,
VisualAid_DateApp = v.DateApproved.ToString() ?? "",
VisualAid_BrandId = int.Parse(v.BrandID.ToString()),
VisualAid_Type = v.Type,
VisualAid_Owner = x.UserID == null ? "" : x.UserFirstName + " " + x.UserLastName
};
but I need to do the part of the subquery, ie, I need to include this:
where AreaID IN (
select areaid from Users inner join Users_Area_Link
on Users.UserID = Users_Area_Link.UserID where Users.UserID= 3
)
Anybody know how? thank you very much in advance
You can add this as a where statement:
.......
join us in Context.Users on v.Owner equals us.UserID into vadis
from x in vadis.DefaultIfEmpty()
where (
from user in Context.Users
join userArea in Users_Area_Link
on user.UserID equals userArea.UserID
where user.UserID==3
select userArea.areaid
).Contains(????.AreaID)
select new Visual_Aid()
{
.......