首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

为什么小弟我在SQL的where条件后面加一个1 = 1 性能就变快多了,求解释

2012-03-31 
为什么我在SQL的where条件后面加一个1 1 性能就变快多了,求解释!我的SQL语句是这样写的:Dim sqlda As Sq

为什么我在SQL的where条件后面加一个1 = 1 性能就变快多了,求解释!
我的SQL语句是这样写的:
Dim sqlda As SqlDataAdapter
  Dim query As StringBuilder = New StringBuilder()
  query.Append(" SELECT STORE_ID, CUSTOMER_ID, JANCODE, ITEM_KANA, ")
  query.Append(" SALES_QUANTITY_1 - SALES_QUANTITY_2 AS SALES_QUANTITY, ")
  query.Append(" SALES_PRICE_SUM_1 - SALES_PRICE_SUM_2 - DETAIL_DISCOUNT - PROPORTIONAL_DISCOUNT AS SALES_PRICE ")
  query.Append(" FROM (SELECT SALES.STORE_ID AS STORE_ID, ")
  query.Append(" ITEM.CUSTOMER_ID AS CUSTOMER_ID, ITEM.JANCODE AS JANCODE, ")
  query.Append(" ITEM.ITEM_KANA AS ITEM_KANA, ")
  query.Append(" MAX(SALES.RETURN_DIVISION) AS RETURN_DIVISION, ")
  query.Append(" MAX(SALES.DELETE_DIVISION) AS DELETE_DIVISION, ")
  query.Append(" SUM(CASE WHEN ((SALES.RETURN_DIVISION = 0) ")
  query.Append(" AND (SALES.DELETE_DIVISION = 0)) OR ((SALES.RETURN_DIVISION = 1) ")
  query.Append(" AND (SALES.DELETE_DIVISION = 1)) THEN SALES.SALES_PRICE_SUM ")
  query.Append(" ELSE 0 END) AS SALES_PRICE_SUM_1, SUM(CASE WHEN ((SALES.RETURN_DIVISION = 0) ")
  query.Append(" AND (SALES.DELETE_DIVISION = 1)) OR ((SALES.RETURN_DIVISION = 1) ")
  query.Append(" AND (SALES.DELETE_DIVISION = 0)) THEN SALES.SALES_PRICE_SUM ")
  query.Append(" ELSE 0 END) AS SALES_PRICE_SUM_2, SUM(CASE WHEN ((SALES.RETURN_DIVISION = 0) ")
  query.Append(" AND (SALES.DELETE_DIVISION = 0)) OR ((SALES.RETURN_DIVISION = 1) ")
  query.Append(" AND (SALES.DELETE_DIVISION = 1)) THEN SALES.SALES_QUANTITY ")
  query.Append(" ELSE 0 END) AS SALES_QUANTITY_1, SUM(CASE WHEN ((SALES.RETURN_DIVISION = 0) ")
  query.Append(" AND (SALES.DELETE_DIVISION = 1)) OR ((SALES.RETURN_DIVISION = 1) ")
  query.Append(" AND (SALES.DELETE_DIVISION = 0)) THEN SALES.SALES_QUANTITY ")
  query.Append(" ELSE 0 END) AS SALES_QUANTITY_2, SUM(SALES.DETAIL_DISCOUNT) AS DETAIL_DISCOUNT, ")
  query.Append(" SUM(SALES.PROPORTIONAL_DISCOUNT) AS PROPORTIONAL_DISCOUNT ")
  query.Append(" FROM SALES INNER JOIN ITEM ON SALES.ITEM_ID = ITEM.ITEM_ID ")
  query.Append(" INNER JOIN STORE ON STORE.STORE_ID = SALES.SALES_ID ")
  query.Append(" INNER JOIN CUSTOMER ON ITEM.CUSTOMER_ID = CUSTOMER.CUSTOMER_ID ")
  query.Append(" WHERE SALES_DATE BETWEEN @SALES_DATE_START AND @SALES_DATE_END")

  If storeid = 0 Then
  If customer_id <> 0 Then
  query.Append(" AND ITEM.CUSTOMER_ID = @CUSTOMER_ID")
  Else
query.Append(" AND 1 = 1 ")
  End If

  query.Append(" GROUP BY SALES.STORE_ID, ITEM.JANCODE, ITEM.ITEM_KANA, ITEM.CUSTOMER_ID, ")
  query.Append(" RETURN_DIVISION, DELETE_DIVISION, SALES_PRICE_SUM, ")
  query.Append(" SALES_QUANTITY) AS AMOUNT ORDER BY ")

  If orderflag = 0 Then
  query.Append(" SALES_QUANTITY ")
  Else
  query.Append(" SALES_PRICE ")
  End If

  sqlda = New SqlDataAdapter(query.ToString(), Me.Connection)
  sqlda.SelectCommand.Parameters.AddWithValue("@SALES_DATE_START", sales_Date_Start)


  sqlda.SelectCommand.Parameters.AddWithValue("@SALES_DATE_END", sales_Date_End)
  If customer_id <> 0 Then
  sqlda.SelectCommand.Parameters.AddWithValue("@CUSTOMER_ID", customer_id)
  End If
  End If
  一开始运行的时候要,性能很慢很慢,但是最后添加入红色标记的部分代码后,性能一下就大增了,数据都是一刷就出来了,这是问什么呀!求高手指点。

[解决办法]
问题应该不在这里,之所以这样可能是缓存问题。
[解决办法]
对,是缓存的问题,

热点排行