目的:针对每个客户按产品品牌区分价格,价格分为三个级别现状:
ProductBrand表(ID,Brand)
Product表(ID,BrandID,ProductName,T1,T2,T3)
User表(ID,UserName)
UserBrandLevel表(UserID,BrandID,Tier) UserID,BrandID联合主键UserBrandLevel里存储某个客户在某个品牌所能看到的价格,在具体操作中出现了问题核心查询
select Product.ID,Product.BrandID,ProductName,
case UserBrandLevel.tier when 1 then T1 when 2 then T2 else tier3 end As Price from Product
Left join UserBrandLevel on Product.BrandID=UserBrandLevel.BrandID and UserID=@UserID可以解决按品牌和客户执行不同的价格,并组成一个表问题:发现此查询特别消耗内存,很快Sqlserver占用了全部的内存而导致服务器崩溃。希望各位提出好的建议!初步考虑拆分Product表为Product表和Price表,但工程浩大。
ProductBrand表(ID,Brand)
Product表(ID,BrandID,ProductName,T1,T2,T3)
User表(ID,UserName)
UserBrandLevel表(UserID,BrandID,Tier) UserID,BrandID联合主键UserBrandLevel里存储某个客户在某个品牌所能看到的价格,在具体操作中出现了问题核心查询
select Product.ID,Product.BrandID,ProductName,
case UserBrandLevel.tier when 1 then T1 when 2 then T2 else tier3 end As Price from Product
Left join UserBrandLevel on Product.BrandID=UserBrandLevel.BrandID and UserID=@UserID可以解决按品牌和客户执行不同的价格,并组成一个表问题:发现此查询特别消耗内存,很快Sqlserver占用了全部的内存而导致服务器崩溃。希望各位提出好的建议!初步考虑拆分Product表为Product表和Price表,但工程浩大。
解决方案 »
- 急急急急急急急急急急急!向日葵甘特图
- 页面执行
- 我想让隐藏的层不占有原来的位置,怎么实现啊???
- asp.net项目能否部署到weblogic中
- win 2000 中已经给 aspnet 拥护设置写权限了 为身让人不能写文件呢??提示权限不够
- 请问如何让用户浏览网页时在浏览器的地址栏中添加一个图表?
- 求助!!!!各位前辈们来帮帮忙哦!急
- <%# 在SQL的字段里面,有个"公司简介"在里面请问要用什么数据类型 %>
- dropdownlist 如何实现边输入边检索的功能
- DataGird的样子太简单了,如果要实现复杂一点的分页怎么办???
- DataList做购物车如何更改数量
- 在一个textbox值改变后给button添加return confirm('确定要更改图书财产号吗?')
·详情请访问:http://www.515dns.com
UserBrandLevel.tier into #table from Product a
Left join UserBrandLevel b on aa.BrandID=b.BrandID and UserID=@UserID select ID,BrandID,ProductName,
case tier when 1 then T1 when 2 then T2 else T3 end As Price from #table
(case tier when 1 then T1 when 2 then T2 else tier3 end) As Price from
(select Product.*,UserBrandLevel.tier from Product
Left join UserBrandLevel on Product.BrandID=UserBrandLevel.BrandID and UserID=@UserID) as P 或在存储过程里通过游标遍历添加数据到临时表看看
或者把结果整理分表好,把表join 起来,不要那么多判断。
SELECT P.ID
,P.BrandID
,P.ProductName
,Price=(case UBL.tier
when 1 then T1
when 2 then T2
else T3 end)
FROM Product P LEFT JOIN
(SELECT * FROM UserBrandLevel WHERE UserID=@UserID
) UBL ON P.BrandID=UBL.BrandID
SELECT P.ID
,P.BrandID
,P.ProductName
,Price=(
SELECT (case tier when 1 then T1 when 2 then T2 else T3 end)
FROM UserBrandLevel
WHERE UserID=@UserID AND P.BrandID=BrandID
)
FROM Product P
用存储过程,
用BrandID来约束left join 的数据量。表1 left join 表2
表1和表2是经过约束的。不知道是否理解?www.datasonar.com
也可以:
SQL codeSELECT P.ID
,P.BrandID
,P.ProductName
,Price in (
SELECT (case tier when 1 then T1 when 2 then T2 else T3 end)
FROM UserBrandLevel
WHERE UserID=@UserID AND P.BrandID=BrandID
)
FROM Product P
当然有提高啊, 第二步查询#table的集合小很多很多啊
(case tier when 1 then T1 when 2 then T2 else tier3 end) As Price from
(select Product.*,UserBrandLevel.tier from Product
Left join UserBrandLevel on Product.BrandID=UserBrandLevel.BrandID and UserID=@UserID) as P --或在存储过程里通过游标遍历添加数据到临时表看看
可行!!!!!!!!!!