我有一张有 350 000 行的表。当我搜索前 50 个时,我得到了 50 个最旧的行。有没有办法让 SQL 服务器从底部开始,所以我得到了 bin 插入的最后 50 行?
我们有一些性能问题,所以我不能使用任何循环遍历所有记录的语法。
如果我按 ID 添加订单,则需要 14 秒而不是 1 秒。我使用动态 SQL 来解决动态表结构的搜索功能。
ALTER Procedure [CS.Core].[spDynamicSearchInsuranceDemoCar]
@ChassisNumber AS NVARCHAR(50) = NULL,@FirstOwner AS BIT = NULL,@GroupId AS INT = NULL,@InsuranceNumber AS INT = NULL,@InsuredId AS INT = NULL,@Model AS NVARCHAR(50) = NULL,@OwnerYearOfBirth AS INT = NULL,@RegistrationNumber AS NVARCHAR(12) = NULL,@StakeholderId AS INT = NULL,@StatusId AS INT = NULL,@UserId 作为 int,@LanguageId 作为 int AS SET NOCOUNT ON;开始声明 @sql nvarchar(MAX),@paramlist nvarchar(4000) 选择 @sql ='SELECT TOP 50 Insurance.InsuranceId, ISNULL(ProductCaption.CaptionText, [CS.Core].Entity.Name) AS Product_462, Stakeholder.RefStakeholderName AS Stakeholder_1093,Insurance.ValidFrom 作为 ValidFrom_925,Insurance.InsuranceNumber,Insurance.GroupId,Insurance.StatusId,Insurance.ValidFrom,DemoCar.RegistrationNumber,DemoCar.Year,DemoCar.ChassisNumber,DemoCar.FirstOwner,DemoCar。
0 SELECT @sql = @sql + ' AND ([CS.Entity].[DemoCar].[RegistrationNumber] Like @RegistrationNumber)' ELSE SELECT @sql = @sql + ' AND ([CS.Entity].[DemoCar]. [RegistrationNumber] = @RegistrationNumber)' IF @StakeholderId 不为 NULL SELECT @sql = @sql + ' AND ([CS.Entity].[Insurance].[StakeholderId] = @StakeholderId)' IF @StatusId 不为 NULL SELECT @ sql = @sql + ' AND ([CS.Entity].[Insurance].[StatusId] = @StatusId)' SELECT @sql = @sql + ''
SELECT @paramlist = '@ChassisNumber AS NVARCHAR(50) = NULL,@FirstOwner AS BIT = NULL,@GroupId AS INT = NULL,@InsuranceNumber AS INT = NULL,@InsuredId AS INT = NULL,@Model AS NVARCHAR(50) = NULL,@OwnerYearOfBirth AS INT = NULL,@RegistrationNumber AS NVARCHAR(12) = NULL,@StakeholderId AS INT = NULL,@StatusId AS INT = NULL,@UserId 作为 int,@LanguageId 作为 int' EXEC sp_executesql @sql,@参数列表,@ChassisNumber,@FirstOwner,@GroupId,@InsuranceNumber,@InsuredId,@Model,@OwnerYearOfBirth,@RegistrationNumber,@StakeholderId,@StatusId,@UserId,@LanguageId END