有这样三个表
student(
sid int not null,
......
primary key (sid)
);course(
cid int not null ,
......
primary key(cid)
);sc(
scid int not null,
sid int not null,
cid int not null,
foreign key (sid) reference student(sid),
foreign key (cid) reference cource(cid),
primary key scid
)
假设表的数据量很大,现在要找出某个学生选的所有课的信息。用select 语句的嵌套查询的性能好点还是直接把表连接起来查询的性能好点呢?
student(
sid int not null,
......
primary key (sid)
);course(
cid int not null ,
......
primary key(cid)
);sc(
scid int not null,
sid int not null,
cid int not null,
foreign key (sid) reference student(sid),
foreign key (cid) reference cource(cid),
primary key scid
)
假设表的数据量很大,现在要找出某个学生选的所有课的信息。用select 语句的嵌套查询的性能好点还是直接把表连接起来查询的性能好点呢?
其实我认为两者效率旗鼓相当
只不过第一种建表的方式更符合建表的规范,是依次按照第一范式、第二范式、第三范式而来,对数据库来说减少了数据的冗余
而第二种查询的效率可能差不多,但综合起来对数据库的影响来说还是第一种好一些
手册中有详细解释JOIN 和SUBQUERY的性能问题。仔细看看。