目的:针对每个客户按产品品牌区分价格,价格分为三个级别现状:
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表,但工程浩大。

解决方案 »

  1.   

    ·双线主机 100M/35元/年,免费送数据库(自选MY/MSSQL) 
    ·详情请访问:http://www.515dns.com 
      

  2.   

    left join好像比inner join效率低了点,连接字段建索引?
      

  3.   

    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 
      

  4.   

    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 或在存储过程里通过游标遍历添加数据到临时表看看
      

  5.   

    优化你的表结构吧,要不就是临时表 视图 建立索引 建立表分区之类的了
    或者把结果整理分表好,把表join 起来,不要那么多判断。
      

  6.   

    语句可以试试这个:
    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  
      

  7.   

    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
      

  8.   

    BrandID是关键
    用存储过程,
    用BrandID来约束left join 的数据量。表1 left join 表2
    表1和表2是经过约束的。不知道是否理解?www.datasonar.com
      

  9.   

    用Left join似乎不太好,这可能不是性能上的问题,但是我觉得不是很好,应该写一个存储过程。看了14楼的,觉得不错
      

  10.   


    也可以:
    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
      

  11.   

    错误使用“left”的问题。跟存储过程有何关系?说存储过程能够“提高运行速度”,相对实际遇到的性能问题来说根本不值一提,根本解决不了设计问题。
      

  12.   


    当然有提高啊, 第二步查询#table的集合小很多很多啊
      

  13.   

    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 --或在存储过程里通过游标遍历添加数据到临时表看看
    可行!!!!!!!!!!