关于asp.net项目的数据库设计

hornbills 2009-05-17 01:38:05
目的:针对每个客户按产品品牌区分价格,价格分为三个级别

现状:
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表,但工程浩大。
...全文
188 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
微工程 2009-05-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 ai_li7758521 的回复:]
TRY:

SQL codeSELECT 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
[/Quote]

也可以:
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
hornbills 2009-05-28
  • 打赏
  • 举报
回复
还有没有新思路?
jueyingfd 2009-05-28
  • 打赏
  • 举报
回复
select ID,BrandID,ProductName, 
(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

--或在存储过程里通过游标遍历添加数据到临时表看看

可行!!!!!!!!!!
tabbycat 2009-05-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hornbills 的回复:]
引用 6 楼 zzxap 的回复:
SQL code

select Product.ID,Product.BrandID,ProductName,
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


这只不过是分了两步,性能会有提高吗?
[/Quote]

当然有提高啊, 第二步查询#table的集合小很多很多啊
  • 打赏
  • 举报
回复
错误使用“left”的问题。

跟存储过程有何关系?说存储过程能够“提高运行速度”,相对实际遇到的性能问题来说根本不值一提,根本解决不了设计问题。
jueyingfd 2009-05-18
  • 打赏
  • 举报
回复
用存储过程好一点,因为他快啊
一方晴空 2009-05-18
  • 打赏
  • 举报
回复
用Left join似乎不太好,这可能不是性能上的问题,但是我觉得不是很好,应该写一个存储过程。看了14楼的,觉得不错
shangwg 2009-05-18
  • 打赏
  • 举报
回复
BrandID是关键
用存储过程,
用BrandID来约束left join 的数据量。

表1 left join 表2
表1和表2是经过约束的。

不知道是否理解?

www.datasonar.com
Yeriklove 2009-05-18
  • 打赏
  • 举报
回复
利用存储过程
springbell 2009-05-18
  • 打赏
  • 举报
回复
起来起来
liuenhai211 2009-05-18
  • 打赏
  • 举报
回复
UPUPUP
hornbills 2009-05-18
  • 打赏
  • 举报
回复
还有没有新的意见?
ai_li7758521 2009-05-17
  • 打赏
  • 举报
回复
TRY:
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
ai_li7758521 2009-05-17
  • 打赏
  • 举报
回复
语句可以试试这个:
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

wangbin1986 2009-05-17
  • 打赏
  • 举报
回复
存储过程顶!
xslqingfeng 2009-05-17
  • 打赏
  • 举报
回复
关注,帮顶
zzxap 2009-05-17
  • 打赏
  • 举报
回复
优化你的表结构吧,要不就是临时表 视图 建立索引 建立表分区之类的了
或者把结果整理分表好,把表join 起来,不要那么多判断。
wuyq11 2009-05-17
  • 打赏
  • 举报
回复
select ID,BrandID,ProductName,
(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

或在存储过程里通过游标遍历添加数据到临时表看看
hornbills 2009-05-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zzxap 的回复:]
SQL code

select Product.ID,Product.BrandID,ProductName,
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
[/Quote]

这只不过是分了两步,性能会有提高吗?
-无-为- 2009-05-17
  • 打赏
  • 举报
回复
支持一下 顺便找数据库开发的同事问问啊。。。。

[Quote=引用 2 楼 shizhen_zhang 的回复:]
写个存储过程不就行了吗
[/Quote]
加载更多回复(6)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧