基本的に、複数のテーブルの結合で構成される SQL クエリでページネーションを実行しようとしています。このトピックに関するいくつかの同様の回答を読んだ後、私のクエリは次のようになります。
SET @PageSelect = '
SELECT CATALOG,
ProductID,
CreateDate,
Brand,
Model,
Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6,
ROW_NUMBER() OVER (ORDER BY CreateDate DESC, ProductID) AS RowNumber
FROM
( SELECT ''Agriculture'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product1 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''Cargo-Transport'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutKilometers AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM product6 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''Construction'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product2 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''Forestry'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product3 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''Groundscare'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product4 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''MaterialHandling'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product5 WITH (NOLOCK)
WHERE (Status = 8)) AS BasicSource
WHERE RowNumber BETWEEN ' + CAST(@inPage * @inPageSize - @inPageSize + 1 AS NVARCHAR) + ' AND ' + CAST(@inPage * @inPageSize AS NVARCHAR) + ' ORDER BY RowNumber'
、しかし、行番号でエラーが発生します:Invalid column name 'RowNumber'.
誰かが私が間違っていることを説明できますか?
最適化の問題があります。ページネーションを作成したいと思います (テーブルの結合から 25->100 項目のみを取得します)。しかし、この特定のケースでは...ユニオン(〜400k)からすべての結果を取得するため、遅くなります