Sto facendo refactoring di una applicazone Web API.
Come DBMS usa Sql Server e come Entity Framewotk dapper.
Ho la necessità di ottimizzare il seguente spezzone di codice ;
var result = await sqlConnection.QueryAsync<ProviderAccessLog>(@"
SELECT Id, PaymentProviderType, UserId, TimeStamp, EventType, RequestType, Url, PaymentProviderTransactionId, TransactionId, Request, Response, ElapsedTime
FROM ProviderAccessLog
WHERE (0=@FilterTimeStampFrom OR TimeStamp >= @TimeStampFrom)
AND (0=@FilterTimeStampUntil OR TimeStamp <= @TimeStampUntil)
AND (0=@FilterTransactionId OR TransactionId = @TransactionId)
AND (0=@FilterPaymentProviderType OR PaymentProviderType = @PaymentProviderType)
AND (0=@FilterPaymentProviderTransactionId OR PaymentProviderTransactionId = @PaymentProviderTransactionId)
AND (0=@FilterUserId OR UserId = @UserId)
AND (0=@FilterEventType OR EventType = @EventType)
AND (0=@FilterRequestType OR RequestType = @RequestType)
AND (0=@FilterRequest OR Request LIKE CONCAT('%',@Request,'%'))
AND (0=@FilterResponse OR Response LIKE CONCAT('%',@Response,'%'))
ORDER BY TimeStamp DESC
OFFSET ((@PaginationOffset-1) * @PaginationLimit) ROWS
FETCH NEXT @PaginationLimit ROWS ONLY;", filter);
Può essere utile mostrarvi la struttura della tabella :
USE [cashier]
GO
/****** Object: Table [dbo].[ProviderAccessLog] Script Date: 12/15/2022 8:59:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProviderAccessLog](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[PaymentProviderType] [int] NULL,
[UserId] [bigint] NULL,
[TimeStamp] [datetime2](0) NULL,
[EventType] [int] NULL,
[RequestType] [int] NULL,
[Url] [nvarchar](1000) NULL,
[PaymentProviderTransactionId] [nvarchar](50) NULL,
[TransactionId] [nvarchar](100) NULL,
[Request] [nvarchar](max) NULL,
[Response] [nvarchar](max) NULL,
[ElapsedTime] [int] NULL,
CONSTRAINT [PK_ProviderAccessLog] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Man mano che la tabella “cresce” la lettura, anche se filtrata, p sempre più lenta.
Potete darmi qualchhe prezioso suggerimento?
Simone