using mysql what will be the fastest way to join tables - mysql

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.

Related

MySQL Where Statement While checking value

$sql = "SELECT cc.name AS c_name, ev.name AS event_name, ev.eventModeId AS event_mode, ev.carClassHash, cc.carClassHash
FROM EVENT_DATA e
INNER JOIN PERSONA p ON e.personaId = p.ID
INNER JOIN CUSTOMCAR cc ON cc.ownedCarId = e.carId
INNER JOIN CAR_CLASSES ccs ON ccs.store_name = cc.name
INNER JOIN USER u ON u.ID = p.USERID
LEFT JOIN BAN b ON b.user_id = u.ID
INNER JOIN EVENT ev ON ev.ID = e.EVENTID
WHERE (p.name = ?
AND ev.carClassHash = cc.carClassHash)";
This query works for me except I'd also like to display any carClassHash with the value '607077938'. Is there a way I could somehow keep the above query to check if the event hash matches the car class hash but also still display values where the carClassHash (cc.carClassHash) is equal to '607077938'?
Thanks! :)
Why not use OR in the WHERE clause?
[...]
WHERE ((p.name = ? AND ev.carClassHash = cc.carClassHash) OR (cc.carClassHash = 607077938))
This should work if that value is an integer. If it's a string, use quotes around it.

proper indexing for fast retrieval in 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.

How to use JOIN in UPDATE mysql query

Hello I want to bind this to one query with JOIN.
How does this work:
Db::bind("uid", strip_tags($userid));
DB::bind("user_id", strip_tags($refer));
Db::bind("points_earn", strip_tags($points_earn));
Db::bind("points_refer", strip_tags($points_refer));
Db::query("UPDATE referrals SET `points_earn` = :points_earn WHERE new_id = :uid");
Db::query("UPDATE users SET `points` = `points` + :points_refer WHERE id = :user_id");
What I think but not work.
Db::query("UPDATE referrals r JOIN users u ON r.user_id = u.id SET `r.points_earn` = :points_earn WHERE r.new_id = :uid AND `u.points` = `u.points` + :points_refer WHERE u.id = :user_id");
Have anyone a solution?
Your syntax is right, but the back ticks are wrong and each SELECT only has one WHERE:
UPDATE referrals r JOIN
users u
ON r.user_id = u.id
SET r.points_earn = :points_earn
WHERE r.new_id = :uid AND u.points = u.points + :points_refer AND
u.id = :user_id;
The problem with backticks is that the following are quite different:
`r.points_earn`
`r`.`points_earn`
The first refers to a column called "r.points_earn". The second refers to the "points_earn" column in "r". The backticks aren't necessary so you can just remove them.

Is it possible to turn this query into a join?

This is the query:
SELECT *
FROM square_achievements_achievements
JOIN square_achievements_achievement_counters ON square_achievements_achievement_counters.SAA_ID = square_achievements_achievements.SAA_ID
JOIN square_achievements_counters ON square_achievements_counters.SAC_ID = square_achievements_achievement_counters.SAC_ID
WHERE square_achievements_counters.eventObject = 'CommentEvent'
AND square_achievements_counters.eventType = 'add'
AND square_achievements_achievements.SAA_ID NOT IN
(
SELECT square_achievements_achievements.SAA_ID
FROM square_achievements_achievements
JOIN square_achievements_user_achievements ON square_achievements_user_achievements.SAA_ID = square_achievements_achievements.SAA_ID
WHERE square_achievements_user_achievements.UID = 83
)
If it's possible, would it be more efficient to write this query as a join? If so, how would it be rewritten?
SELECT a.*
FROM achievements a
JOIN achievement_counters ac
ON ac.saac_id = a.saa_id
JOIN counters c
ON c.sac_id = ac.sac_id
WHERE c.eventObject = 'CommentEvent'
AND c.eventType = 'add'
AND a.saa_id NOT IN
(
SELECT saa_id
FROM user_achievements
WHERE uid = 83
)
This is pretty good.
If you want joins, use this:
SELECT a.*
FROM achievements a
JOIN achievement_counters ac
ON ac.saac_id = a.saa_id
JOIN counters c
ON c.sac_id = ac.sac_id
LEFT JOIN
user_achievements ua
ON ua.uid = 83
AND ua.saa_id = a.saa_id
WHERE c.eventObject = 'CommentEvent'
AND c.eventType = 'add'
AND ua.saa_id IS NULL
I prefer to use correlation names to improve the readability of the code
Try this:
SELECT *
FROM square_achievements_achievements a
JOIN square_achievements_achievement_counters b ON b.SAAC_ID = a.SAA_ID
JOIN square_achievements_counters c ON c.SAC_ID = b.SAC_ID
JOIN square_achievements_user_achievements d on d.SAA_ID = a.SAA_ID
JOIN square_achievements_user_achievements u on d.SAA_ID = u.SAA_ID AND u.UID = 83
WHERE c.eventObject = 'CommentEvent'
AND a.eventType = 'add'

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()
{
.......