日期:2014-05-17  浏览次数:20435 次

SQL Servier 表结构设计的问题求助。
有三个表:
产品主表,
班期主表(一个产品下面的所有班期,一个产品下面有3-5个班期),
班期从表(一个班期下面的所有日期明细,一个班期有十几条明细)

现在要根据日期区间和价格区间查询所有的产品,其中日期就是班期中的日期明细,
价格是和班期挂钩的,一个班期一种价格。
方案一: 这个价格只保存在班期主表里面,然后查询的时候使用2个子查询,查询产品信息
方案二: 这个价格同时也保存在班期从表里面,然后查询的时候使用子查询查询出产品信息

SQL语句:
方案一:
select * from Product p
where 
exists(select ID from BanMain bm 
where bm.ProductID = p.ID 
and bm.Price > 100
and 
exists( select ID from BanDetail bd 
where bd.BanID = bm.ID
and bd.Date >= '2013-01-01' and bd.Date < '2013-02-01')
))

select * from Product p
where 
exists(select ID from BanMain bm 
where bm.ProductID = p.ID 
and bm.Price > 100)
and 
exists( select ID from BanDetail bd 
where p.ID = bd.ProductID
and bd.Date >= '2013-01-01' and bd.Date < '2013-02-01')
)
方案二:
select * from Product p
where 
exists( select ID from BanDetail bd 
where p.ID = bd.ProductID
and bd.Date >= '2013-01-01' and bd.Date < '2013-02-01'
and bd.Price > 100
)

请大家帮我看看哪个好?谢谢大家了。
------解决方案--------------------
把3个语句放到一个查询界面,然后ctrl+M ,然后执行这个语句,看看3个语句的百分比,百分比越低,开销越低,意味着它的性能“可能越高”