查询性能:查询多个表 Vs。复合查询

Query performance: Query on multiple tables Vs. Composite query

Table A 有列 srno 和其他几列。

Table B 有列 srnoid.

我想从 B 获取 srno 给定 id,然后从 table A 获取 srno 的记录。

例如,如果id是7,那么我可以想出两种方法:

select * from A as table_a, B as table_b where table_a.srno=table_b.srno and table_b.id=7;

而且,

select * from A where srno in (select srno from B where id=7);

两者都在做同样的事情。但是当 table 中都有大量记录时,性能方面哪个更好?或者两者都有相同的性能? (我们假设这两个 tables 已经处理了适当的索引等。我只想比较这两个查询之间的性能)

您的第二个查询总是比较慢。 MySQL 中的那种动态 IN 子句从来都不是一个好方法。

我的建议是使用第一个查询,但使用 ANSI 连接语法和 select 您需要的最小列集重写它,而不是 SELECT *.

这将是一个很好的起点:

select table_a.* 
from A as table_a 
inner join B as table_b on table_a.srno=table_b.srno 
where table_b.id=7;