[英]mysql get specific unique row count from one table in a join query
I have a requests
and a results
table. 我有一个requests
和一个results
表。 Each with an email_sha256
column. 每个都有一个email_sha256
列。
Requests may contain multiple rows with the same email, while emails are unique in the results. 请求可能包含具有相同电子邮件的多行,而电子邮件在结果中是唯一的。 Emails in the results table might not exist in the requests table. 结果表中的电子邮件可能不存在于请求表中。
I want to get 100 results that have an email that exists in the requests table: 我想获得100个结果,并且请求表中存在一封电子邮件:
SELECT results.* FROM results
INNER JOIN requests ON results.email_sha256 = requests.email_sha256
LIMIT 100
This generally works but it may return the same result multiple times if there are multiple requests with the same email. 通常这是可行的,但是如果有多个请求使用相同的电子邮件,则可能会多次返回相同的结果。 Is there some way to make sure that I get 100 unique results instead of duplicates? 有什么方法可以确保获得100个唯一的结果,而不是重复的结果?
The join seems very slow. 加入似乎很慢。 Is there some better way to get the desired result. 有没有更好的方法来获得期望的结果。 eg using EXISTS
? 例如使用EXISTS
吗?
This generally works but it may return the same result multiple times if there are multiple requests with the same email. 通常这是可行的,但是如果有多个请求使用相同的电子邮件,则可能会多次返回相同的结果。 Is there some way to make sure that I get 100 unique results instead of duplicates? 有什么方法可以确保获得100个唯一的结果,而不是重复的结果?
Use GROUP BY
Docs . 使用GROUP BY
Docs 。
SELECT results.* FROM results
INNER JOIN requests ON results.email_sha256 = requests.email_sha256
GROUP BY results.email_sha256
LIMIT 100
The join seems very slow. 加入似乎很慢。 Is there some better way to get the desired result. 有没有更好的方法来获得期望的结果。 eg using EXISTS? 例如使用EXISTS?
We can't specifically answer this without an explanation and/or information about the table(s) . 没有表格 的解释和/或信息,我们无法具体回答。 However the most probably answer is that you have not indexed the correct columns. 但是,最可能的答案是您没有索引正确的列。
You should have an index on your JOIN
ing column and your GROUP BY
column(s). 您应该在JOIN
ing列和GROUP BY
列上都有一个索引。 In this case that's the same - results.email_sha256
and requests.email_sha256
. 在这种情况下是相同的results.email_sha256
和requests.email_sha256
。
That's a good start there's also plenty of more specific Q&A on Stack Overflow on various issues of slow result return by MySQL.... 这是一个好的开始,在堆栈溢出方面,还有很多针对MySQL慢速返回结果等问题的更具体的问答。
With EXISTS: 具有EXISTS:
SELECT r.* FROM results r
WHERE EXISTS (
SELECT 1 FROM requests WHERE email_sha256 = r.email_sha256
)
LIMIT 100
This returns 100 unique rows rows since email_sha256
is unique
in results . 由于email_sha256
在结果中是unique
,因此将返回100个唯一的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.