SQL简单嵌套选择查询被阻止

时间:2016-04-20 04:22:33

标签: sql sql-server pagination sqltransaction

在系统中,我使用以下查询来获取表格数据的一部分,以便实现分页。该表目前仅包含约100条记录,但稍后将增加到100万条记录。

 SELECT 
    Id AS ActualMapping_Id,
    BudgetPhase  AS ActualMapping_BudgetPhase,
    FromBH AS ActualMapping_FromBH,
    ToBH AS ActualMapping_ToBH,
    FromBI AS ActualMapping_FromBI,
    ToBI AS ActualMapping_ToBI,
    FromSI1 AS ActualMapping_FromSI1,
    ToSI1 AS ActualMapping_ToSI2,
    FromSI2 AS ActualMapping_FromSI2,
    ToSI2 AS ActualMapping_ToSI2,
    DataType AS ActualMapping_DataType,
    Status AS ActualMapping_Status,
    MappingType AS ActualMapping_MappingType,
    LastMappedBy AS ActualMapping_LastMappedBy,
    LastMappedDate AS ActualMapping_LastMappedDate
  FROM
 (
 SELECT
    Id,
    BudgetPhase,
    FromBH,
    ToBH,
    FromBI,
    ToBI,
    FromSI1,
    ToSI1,
    FromSI2,
    ToSI2,
    DataType,
    Status,
    MappingType,
    LastMappedBy,
    LastMappedDate,
    ROW_NUMBER() OVER (ORDER BY FromBH) AS RowNumber
FROM
    ActualMapping
WHERE
    DataType = 'Cost' and 
    Status = 'Active' and
    MappingType = 'Static' and 
    BudgetPhase like '%some_text%' and
    ToBH like '%some_text%' and
    ToBI like '%some_text%' and 
    ToSI1 like '%some_text%' and 
    ToSI2 like '%some_text%' and
    FromBH like '%some_text%' and 
    FROMBI like '%some_text%' and
    FROMSI1 like '%some_text%' and
    FROMSI2 like '%$some_text%'
) AS NumberedTable
WHERE
    RowNumber BETWEEN 1 AND 50

以上查询工作得很好(至少在约100条记录上),但过了一段时间它开始被阻止。我无法理解它被阻止的可能原因,但是当发生这种情况时,我无法从表中执行简单的选择查询,除非我杀死所有那些被堆积的被阻止的查询。

所以我的问题是:

  1. 为什么查询会在一段时间后被阻止?
  2. 对于大型数据集,这是在sql端实现分页的好/ ok方法吗? (可能超过100万+记录)

0 个答案:

没有答案