How do I make my query using left join to be faster? - mysql

I have three tables: users (id), projects (id_project, owner_id) and followers (repo_id, user_id). I would like count how many followers one project has. I would like to return all user projects, with and without followers.
Where:
id = owner_id = user_id
id_project = repo_id
I report more than 1,000 users in my query. I did this:
rs = dbSendQuery(mydb, "select p.id_project, p.owner_id, count(f.user_id) from users u left outer join projects p on p.owner_id = u.id and u.id in (123, 526, 852) left outer join followers f on p.id_project = f.repo_id group by p.id;")
The query is too slow. Could anyone give me any suggestions to make the query faster? Am I doing something wrong?
Maybe, I can break into two queries, but how do I get the result of the first (which are the projects of the users) and add in the second query (where I would have the number of followers of the projects) in R?
I am using R and mysql.
Regards,
Thaciana

Sometimes switching to a correlated subquery can speed such queries:
select p.id_project, p.owner_id, count(f.user_id)
from users u left outer join
projects p
on p.owner_id = u.id and u.id in (123, 526, 852) left outer join
followers f
on p.id_project = f.repo_id
group by p.id;
For this query, you want indexes on users(id), projects(owner_id, id_project), and followers(repo_id, user_id).
I note that you are not really using the users table. So, this should do what you want:
select p.id_project, p.owner_id, count(f.user_id)
from projects p left outer join
followers f
on p.id_project = f.repo_id
where p.owner_id in (123, 526, 852)
group by p.id;
The same indexes should work on this query, although the one on users is obviously not needed.
Next, in MySQL, correlated subqueries are sometimes faster than aggregation queries. So, you can try:
select p.id_project, p.owner_id,
(select count(*)
from followers f
where p.id_project = f.repo_id
) as num_followers
from projects p
where p.owner_id in (123, 526, 852);

Related

Making a query with several JOINS

I am stuck with some SQL query.
I have four tables. Which are connected:
user =>user_account=>acount_profile_entries=>profile_entries
From left to right they are one to many.
user_account has a user_id field as FK.
account_profile_field has user_account_id and profile_entry_id.
Profile_entries has a text field that I need to show for each user (account).
I need to write a query that will show me, all accounts for every user, and its profile entries.
I am sorry if this is confusing, I tried to make it simple
This is what I have done so far. I can show all accounts for every user and this is the point I am stuck with. Last two commented out Joins are not working properly. I believe I am close somewhat, I just need a push :)
SELECT
u.email AS Email,
u.id AS UserId,
ua.id AS UserAccountId,
ua.app_id AS Application
FROM users AS u
INNER JOIN user_accounts ua ON ua.user_id = u.id
-- INNER JOIN account_profile_entries ape ON ape.user_account_id = ua.id
-- INNER JOIN profile_entries as pe ON pe.id = ape.profile_entry_id
limit 10
Try this SQL Query with using LEFT JOIN
Description :- The MySQL LEFT JOIN joins two tables and fetches rows based on a condition, which is matching in both the tables and the unmatched rows will also be available from the table written before the JOIN clause.
SYNTAX
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
SELECT u.*,
u.id AS UserId,
ua.id AS UserAccountId,
ua.app_id AS Application,pe.* FROM `users` u
LEFT JOIN user_accounts ua ON ua.user_id = u.id
LEFT JOIN account_profile_entries ape ON ape.user_account_id = ua.id
LEFT JOIN profile_entries as pe ON pe.id = ape.profile_entry_id LIMIT 10

MYSQL: Using a join after and AND

the MYSQL query below combines a number of tables. However, as you can see, I would like to add a LEFT JOIN at the end on the receipt table. The query returns an error when I add the LEFT JOIN. Anybody know the best way to LEFT JOIN the receipt table to the rest of the query. Sorry if this is a newbie question. Thanks !!
SELECT user_name, expense_category, merchant_name, expense_cost, expense_date, expense_status, receipt_image, expense_comment
FROM users, expenses, merchants, receipts
WHERE ".$adminId." = expenses.admin_id
AND expenses.user_id = users.user_id
AND expenses.merchant_id = merchants.merchant_id
AND LEFT JOIN (receipts)
ON expenses.receipt_id = receipts.receipt_id
Here is a clean approach of doing it, note that I have added alias for the tables for better readability so you may use the alias name in the select statement to fetch the column from the proper table.
SELECT
u.user_name,
ex.expense_category,
mer.merchant_name,
ex.expense_cost,
ex.expense_date,
ex.expense_status,
re.receipt_image,
ex.expense_comment
FROM users u
JOIN expenses ex on ex.user_id = u.user_id
JOIN merchants mer on mer.merchant_id = ex.merchant_id
LEFT JOIN receipts re on re.receipt_id = ex.receipt_id
where
ex.admin_id = '$adminId'
Try this,
SELECT user_name, expense_category, merchant_name, expense_cost, expense_date, expense_status, receipt_image, expense_comment
FROM users, expenses, merchants, receipts
LEFT JOIN receipts ON expenses.receipt_id = receipts.receipt_id
WHERE ".$adminId." = expenses.admin_id
AND expenses.user_id = users.user_id
AND expenses.merchant_id = merchants.merchant_id
Use join clauses instead of where clause. I.e.
SELECT user_name, expense_category, merchant_name, expense_cost, expense_date, expense_status, receipt_image, expense_comment
FROM users
INNER JOIN expenses on users.user_id = expenses.expenses_id
INNER JOIN merchants on merchants.merchant_id = expenses.merchant_id
LEFT JOIN (receipts)
ON expenses.receipt_id = receipts.receipt_id
WHERE ".$adminId." = expenses.admin_id
Note that any columns from the receipts will be NULL in the select statement whenever there's no matching record.

How to do MySQL query to fetch other table field value, in case of existance?

Suppose we have this model:
As you see industry_id can be null. Can I fetch industry.name (if any), user.description, profile.name and project.title (all project titles) he/she has with a single MySQL query while having user.id?
Yes, JOIN the two tables:
SELECT
i.name,
u.id
FROM Industry AS i
LEFT JOIN `User` AS u ON u.industry_id = i.industry_id;
Update:
For multiple tables:
SELECT
i.Name AS InustryName,
p.Name AS UserName,
u.Description,
j.title AS ProjectTitle
FROM Industry AS i
INNER JOIN User AS u ON i.id = u.id
INNER JOIN Profile AS p ON p.user_id = u.id
INNER JOIN Project AS j ON u.id = j.user_id;
Note that: I used INNER JOIN between the tables, this will give you only the matched rows from the joined tables, you might need to use LEFT JOIN instead of innner join to include the untmatched rows, i.e., to get those industries that has no entries in the other tables. See this blog post:
A Visual Explanation of SQL Joins

SQl queries left join not giving accurate results

I have been trying to join two tables (USERS AND USERS_ROLES) based on their role id I put the left join on following query
users.id = users_roles.fk_user_id
but the output is not correct of users_roles.fk_role_id coulmun and shows NULL where it should display the id of users.id = users_roles.fk_user_id that is 4 (at most places) because on users.id = users_roles.fk_user_id the value of users_roles.fk_role_id = 4
Kindly let me know how can i fix that so my query should result the exact vlaues of ids where they match,
Thanks
SELECT users.id, users.v_first_name, users.v_last_name, user_facility.fk_facility_id,users.fk_tenant_id, marital_status.v_marital_status,
users.v_blood_type, NOW(),users_roles.fk_role_id
FROM users
LEFT JOIN (user_facility, marital_status, users_roles) ON
users.id = user_facility.fk_user_id AND users.fk_marital_status_id=marital_status.id AND users.id = users_roles.fk_user_id
Usage of AND operator when used with Left or Right join gives different result. You should be clear what you are trying to accomplish..See this
well it is what you get by first implicitly inner-joining 3 tables and then explicitly left-joining the result to a 4th table only if 3 conditions relevant to all of the 3 inner-joinded tables are matched (i.e. when 3rd condition is false, nothing is joined from either of the 2 remaining tables)
i strongly suggest not to combine implicit and explicit joins, i personally use explicit joins all the time:
if you need an outer join:
SELECT ...
FROM users
LEFT JOIN user_facility ON users.id = user_facility.fk_user_id
LEFT JOIN marital_status ON users.fk_marital_status_id=marital_status.id
LEFT JOIN users_roles ON users.id = users_roles.fk_user_id
if you need an inner join:
SELECT ...
FROM users
JOIN user_facility ON users.id = user_facility.fk_user_id
JOIN marital_status ON users.fk_marital_status_id=marital_status.id
JOIN users_roles ON users.id = users_roles.fk_user_id
or if you prefere implicit inner joins for some obscure reason:
SELECT ...
FROM users,
user_facility,
marital_status,
users_roles
WHERE users.id = user_facility.fk_user_id
AND users.fk_marital_status_id=marital_status.id
AND users.id = users_roles.fk_user_id
(implicit outer joins are getting deprecated in all RDBMS as far as i know)
When it shows NULL it means there isn't a correspondency (relation) between all tables in the JOIN clause.
If you want to show only the ones that have relations in all tables, use INNER JOIN instead.
SELECT u.id,
u.v_first_name,
u.v_last_name,
uf.fk_facility_id,
u.fk_tenant_id,
ms.v_marital_status,
u.v_blood_type,
NOW(),
ur.fk_role_id
FROM users u
INNER JOIN user_facility uf ON u.id = uf.fk_user_id
INNER JOIN marital_status ms ON u.fk_marital_status_id=ms.id
INNER JOIN users_roles ur ON u.id = ur.fk_user_id

Flattened string of fields from an associated table in the result sets as a comma separated string

I have the query below (shorted up all the fields in the select to just *'s).
SELECT u.*, e1.*, e2.*
FROM employee_db e1
JOIN employee_db e2 ON e1.manager_id = e2.id
JOIN users u ON u.id = e1.id
There are two more tables involved:
teams (need a flattened version of 'team_name' where user is assigned
to the team)
team_user_associations (team_id, user_id)
(users have many teams through team_user_associations).
What I need is 1 field added to the results that's a comma separated string of all the 'team_name'(s) a users belongs to. I'm having trouble figuring out what the approach would be here... Would it be something like the resutls of a subquery where the 'team_name' field in the subqueries record set are flattend down to a comma separated string that becomes a field in the main query?
Thanks for any help!
You could try a solution like the one below:
SELECT u.*, e1.*, e2.*,
GROUP_CONCAT(t.team_name ORDER BY t.team_name) AS team_names
FROM employee_db e
JOIN employee_db e2 ON l.manager_id = l2.id
JOIN users u ON u.id = l.id
JOIN team_user_associations ta ON ta.user_id = u.id
JOIN teams t ON ta.team_id = t.id
GROUP BY user_id
Try it and tell me if it works, it's difficult without knowing the structure and purpose of the other tables.
Anyway, the trick is to expand the rows by joining with team/user associations, and reducing them again with the GROUP BY. Having the associations now aggregated, you can use GROUP_CONCAT to retrieve the team name column.
SELECT u.*, e.*, e2.*, GROUP_CONCAT(t.team_name)
FROM employee_db e
JOIN employee_db e2
ON e2.id = e.manager_id
JOIN users u
ON u.id = e.id
JOIN team_user te
ON te.user_ud = u.id
JOIN teams t
ON t.id = te.team_id
GROUP BY
u.id