SQL Server较小的日期范围冻结服务器

时间:2014-05-08 21:07:18

标签: sql sql-server

因此,我们有一个SQL Server语句,用于在给定时间段内为特定商家选择销售额。这适用于任何较大的日期范围,整月,多个月,甚至是从2月或更早的几周或几天的短期。

然后,当我们想要查询一个小的日期范围时,例如在示例中给出的5月中的几天,服务器利用率达到100%并且只是将所有内容冻结。这种情况发生在3月,4月和5月的任何小日期范围内,但在此之前不会发生。

是否可能与索引有关,因为这些是最近的条目?

SELECT 
  CAST(tx.tx_sales_amount AS money) AS tx_sales_amount, 
  tx.LID AS loc_id, 
  tx.tx_date, 
  tx.tx_type, 
  tx.tx_clerk_number, 
  ISNULL(
    LTRIM(
      RTRIM(tx.tx_checknum)
    ), 
    'n/a'
  ) AS tx_checknum, 
  tx.card_number, 
  tx_item, 
  tx.member_id, 
  gp.gift_prg_name, 
  gpl.gift_pid, 
  l.location_name AS location 
FROM 
  tblTransactions tx 
  INNER JOIN tblLocations l ON tx.MID = l.MID 
  AND tx.LID = l.LID 
  INNER JOIN tblTerminals t ON tx.MID = t.MID 
  AND tx.LID = t.LID 
  AND tx.TID = t.TID 
  INNER JOIN tblGift_Program_Lookup gpl ON t.id_pk = gpl.TID_fk 
  INNER JOIN tblPrograms_Gift gp ON gp.gift_pid = gpl.gift_pid 
  INNER JOIN tblCards c ON tx.card_number = c.card_number 
  AND c.card_gift_pid = gp.gift_pid 
WHERE 
  tx.tx_type IN ('10', '50') 
  AND tx.MID = '0199' 
  AND tx.tx_void = 0 
  AND l.location_active = 1 
  AND LEFT(tx.card_number, 4) IN (
    SELECT 
      card_prefix 
    FROM 
      tblCard_Types 
    WHERE 
      card_type IN ('Gift', 'Multi')
  ) 
  AND tx.tx_date BETWEEN '2014-05-01 22:00:00' 
  AND '2014-05-07 20:59:59' 
ORDER BY 
  l.location_name, 
  tx.tx_type, 
  gpl.gift_pid, 
  LEFT(c.card_number, 6), 
  tx.tx_date DESC, 
  tx_clerk_number

0 个答案:

没有答案