由于数据量比较大,而且要在一页显示出来,所以查询的语句很多,大约有3000多条,并且每条都有单价*数量这样的计算,如下:select a.quantity*a.price as total_amount1 from apple a,clother b where a.item_id=b.id and box_id=10310321
.....
.............
.........................
.....................................
select a.quantity*a.price as total_amount1 from apple a,clother b where a.item_id=b.id and box_id=10313906最终的结果导致花费了很长时间,不知道有没有办法优化呢?

解决方案 »

  1.   

    a.item_id=b.id and box_id=10310321
    这里涉及到的字段有索引吗,没有的加上试下
      

  2.   

    中间雷同的那些查询语句啊,每句只是最后的那个box_id不同啊
      

  3.   

    特别是box_id字段必须有索引
      

  4.   

    select a.quantity*a.price as total_amount1 from apple a,clother b
     where a.item_id=b.id and box_id between 10310321 and 10313906
      

  5.   

    为什么这么多语句,一个语句不行吗?select a.quantity*a.price as total_amount1,box_id from apple a,clother b where a.item_id=b.id and box_id between 10310321 and 10313906
      

  6.   

    select a.quantity*a.price as total_amount1 from apple a,clother b
    where a.item_id=b.id and box_id between 10310321 and 10313906
    order by box_id
      

  7.   

    如果知道查询的数据在一个范围内,先将其INTO #T,再查找亦可
    既然计算得多,可在DB上加一个公式,并实体存储它
      

  8.   

    那就这样select a.quantity*a.price as total_amount1 from apple a,clother b
    where a.item_id=b.id and box_id in ( 10310321 ,..., 10313906)
    order by box_id
      

  9.   

    这种属于设计的错误, 对于大量的需要计算或者统计的字段, 应该设计冗余字段( 如: Total ), 
    在插入数据时就把它计算出来, 到时要取的时候, 直接拿Total就好了. 
      

  10.   

    不是问题的问题
    1:数据库设计方法 正如yenange所说
    2:SQL语不能用得太死了
    这样的
    如:select a.quantity*a.price as total_amount1 from apple a,clother b
    where a.item_id=b.id and box_id in ( 10310321 ,..., 10313906)
    order by box_id
    又如
    select a.quantity*a.price as total_amount1 from apple a,clother b
    where a.item_id=b.id and box_id in (select box_id from apple a where 条件组合)
    order by box_id
      

  11.   

    select box_id,a.quantity*a.price as total_amount1 from apple a,clother b where a.item_id=b.id把box_id作為顯示或一次把參數一次傳完
    exec('select a.quantity*a.price as total_amount1 from apple a,clother b where a.item_id=b.id and box_id in('+@box_IDs+')'