如何优化 SQL 查询
How SQL queries may be optimized
我有一个 SQL 服务器 table Top_Research_Areas
包含数据,例如
aid res_category_id research_area Paper_Count
---------------------------------------------------------------
2937 33 markov chain 3
2937 33 markov decision process 1
2937 1 linear system 1
11120 29 aspect oriented prog 4
11120 1 graph cut 2
11120 1 optimization problem 2
12403 2 differential equation 7
12403 1 data structure 2
12403 1 problem solving 1
35786 1 complete graphs 11
35786 1 graph cut 10
35786 NULL NULL 2
49261 3 finite automata 6
49261 3 finite element 2
49261 14 database 2
78841 5 genetic programming 6
78841 23 active learning 2
78841 28 pattern matching 1
现在我想 select pid
来自另一个 table 即 sub_aminer_paper
用于 table [=13] 中的 aid
=],而 table sub_aminer_paper
包含列,即 aid
、pid
、research_area
、res_category_id
和更多列。
此外 Top_Research_Areas
仅包含 top_3 research_area
的记录,而 table sub_aminer_paper
包含 aid
以外的记录Top_Research_Areas
中的 s。
我使用过这个查询,即
SELECT
aid, pid, research_area
FROM
sub_aminer_paper
WHERE
aid IN (2937, 11120)
AND research_area IN (SELECT
research_area
FROM
Top_Research_Areas
WHERE
aid IN (2937, 11120))
ORDER BY aid ASC
现在的问题是,当通过匹配 table 中的 research_area
从 sub_aminer_paper
中检索 pid
时,它会给我输出,例如如果我检索两个 aid
的记录,即 2937
和 11120
,它给我的输出为:
我们可以看到前 2 aid
的 Paper_Count
是 3+1+1+4+2+2
即它应该给出 13
记录,但它给出 14
因为 research_area
即 optimization problem
实际上属于 aid
即 11120
in table Top_Research_Areas
但通过使用 IN
子句进行匹配research_area
它是 research_area
和 aid
的混合,而我在输出中需要 13
记录而不是 14
.
如何处理?
请帮忙,谢谢!
"optimization problem" 上可能有一篇关于援助 2937 的论文未登录 top_research_Areas。
查看 id 这有帮助:select 来自 sub_aminer_paper 其中存在 (aid,research_area) 的组合,
SELECT
sap.aid, sap.pid, sap.research_area
FROM
sub_aminer_paper sap
WHERE
sap.AID IN (2937, 11120) --- For indexing which I'm assuming this column has
AND EXISTS (SELECT 1 FROM Top_Research_Areas tra WHERE tra.aid = sap.aid and tra.research_area = sap.research_area and tra.aid in (2937,11120))
我有一个 SQL 服务器 table Top_Research_Areas
包含数据,例如
aid res_category_id research_area Paper_Count
---------------------------------------------------------------
2937 33 markov chain 3
2937 33 markov decision process 1
2937 1 linear system 1
11120 29 aspect oriented prog 4
11120 1 graph cut 2
11120 1 optimization problem 2
12403 2 differential equation 7
12403 1 data structure 2
12403 1 problem solving 1
35786 1 complete graphs 11
35786 1 graph cut 10
35786 NULL NULL 2
49261 3 finite automata 6
49261 3 finite element 2
49261 14 database 2
78841 5 genetic programming 6
78841 23 active learning 2
78841 28 pattern matching 1
现在我想 select pid
来自另一个 table 即 sub_aminer_paper
用于 table [=13] 中的 aid
=],而 table sub_aminer_paper
包含列,即 aid
、pid
、research_area
、res_category_id
和更多列。
此外 Top_Research_Areas
仅包含 top_3 research_area
的记录,而 table sub_aminer_paper
包含 aid
以外的记录Top_Research_Areas
中的 s。
我使用过这个查询,即
SELECT
aid, pid, research_area
FROM
sub_aminer_paper
WHERE
aid IN (2937, 11120)
AND research_area IN (SELECT
research_area
FROM
Top_Research_Areas
WHERE
aid IN (2937, 11120))
ORDER BY aid ASC
现在的问题是,当通过匹配 table 中的 research_area
从 sub_aminer_paper
中检索 pid
时,它会给我输出,例如如果我检索两个 aid
的记录,即 2937
和 11120
,它给我的输出为:
我们可以看到前 2 aid
的 Paper_Count
是 3+1+1+4+2+2
即它应该给出 13
记录,但它给出 14
因为 research_area
即 optimization problem
实际上属于 aid
即 11120
in table Top_Research_Areas
但通过使用 IN
子句进行匹配research_area
它是 research_area
和 aid
的混合,而我在输出中需要 13
记录而不是 14
.
如何处理?
请帮忙,谢谢!
"optimization problem" 上可能有一篇关于援助 2937 的论文未登录 top_research_Areas。
查看 id 这有帮助:select 来自 sub_aminer_paper 其中存在 (aid,research_area) 的组合,
SELECT
sap.aid, sap.pid, sap.research_area
FROM
sub_aminer_paper sap
WHERE
sap.AID IN (2937, 11120) --- For indexing which I'm assuming this column has
AND EXISTS (SELECT 1 FROM Top_Research_Areas tra WHERE tra.aid = sap.aid and tra.research_area = sap.research_area and tra.aid in (2937,11120))