简体   繁体   中英

Timeout expired. The timeout period elapsed prior to completion of the operation

I an a stored procedure which selects files with pagination criteria In this problem, when run this stored procedure, get an exception

Timeout expired. The timeout period elapsed prior to completion of the operation

How to works again without above exception?

My researches and tries:

I compared same db on two database servers(prod and test). Test server can work and prod one can not.

I changed connecion string to add "connect timeout=30000" (5 mins)

I also added CommandTimeOut=30000 at file list method.

Additional information: Prod's db server and IIS server placed different server. I searced some articles and because of the slow connection or heavier traffic on db server. But I encountered this error at first time on today.

My stored procedure located below

ALTER PROCEDURE [dbo].[inwsDosyaListesiSirali]
@SiteId INT,
@KullaniciId int,
@SubeIp varchar(max),
@DepartmanId int,
@KategoriKod VARCHAR(200)= null,
@Baslik VARCHAR(100) = null,
@IlkTarih DATETIME=null,
@SonTarih DATETIME=null,
@Icerik varchar(max) = null,
@PageNumber INT=1,
@PageSize   INT=10,
@SortColumn varchar(100)=null,
@SortOrder BIT=null
AS 
BEGIN 

DECLARE @KategoriKodListe TABLE(KategoriKod VARCHAR(20)  primary key);

INSERT INTO @KategoriKodListe
SELECT Item FROM dbo.SplitString(@KategoriKod,',')

declare @SubeId int,        
        @DepartmanTurId int ,
        @SubeKategoriKod varchar(100)


 SELECT @DepartmanTurId=[DepartmanTurId]  FROM [Departman] 
 where [SiteId]=@SiteId and [Id] = @DepartmanId

 select @SubeId=ks.SubeId, @SubeKategoriKod = ksk.Kod from [dbo].[KurumsalSube] ks 
 inner join  [dbo].[KurumsalSubeKategori] ksk on ks.KategoriId = ksk.KategoriId
 where SiteId=@SiteId and SubeIp like '%'+@SubeIp+'%'         
  ;With EMP AS (
 SELECT 
         r.RaporId
        ,r.HistoryId
        ,r.Baslik
        ,r.DosyaAd
        ,r.RaporAktifTarih
        ,r.RaporGecerlilikTarih
        ,r.RaporTarih
        ,r.KategoriId
        ,h.KayitTarih
        ,r.ParentRaporId
        ,kat.Ad [KategoriAd]
        ,kat.Kod [KategoriKod]
        ,kat.Sira
        ,T.DosyaTurId
        ,T.Ad AS DosyaTurAd
        --,T.Uzanti AS DosyaUzanti
        ,T.iconName
        --,(ROW_NUMBER() OVER (PARTITION BY r.[KategoriId] ORDER BY r.[RaporTarih] desc)) AS number
        ,s.SistemDosyaId
        ,s.DosyaAdi as SistemDosyaAd
         FROM Dosya r
        INNER JOIN DosyaKategori kat ON kat.KategoriId=r.KategoriId 
        INNER JOIN DosyaHistory h   ON h.Id=r.HistoryId
        LEFT JOIN DosyaTur T ON T.DosyaTurId=r.DosyaTurId
        LEFT JOIN DosyaIcerik di ON r.SistemDosyaId = di.SistemDosyaId
        --Yetki kontrol kısmı
        left join [dbo].[DosyaYetki] dy on r.DosyaYetkiId = dy.DosyaYetkiId
        LEFT JOIN SistemDosyalari s ON s.SistemDosyaId=r.SistemDosyaId


        WHERE kat.SiteId=@SiteId AND  r.IsOnay=1 
         AND (isnull(@KategoriKod,'') = '' or exists(select 1 from @KategoriKodListe k where k.KategoriKod=kat.Kod)  )
         AND ((NULLIF(r.RaporGecerlilikTarih,'') IS NULL OR (r.RaporGecerlilikTarih >= GETDATE())))
        AND ((NULLIF(r.RaporAktifTarih,'') IS NULL OR (r.RaporAktifTarih <= GETDATE())))
     --   AND ((NULLIF(@Baslik,'') IS NULL OR (r.Baslik like '%'+@Baslik+'%' ) OR NULLIF(r.ParentRaporId,'') IS NOT NULL))
        --AND ((NULLIF(@IlkTarih,'') IS NULL OR (@IlkTarih<=CONVERT(date, DATEADD(day,0,r.RaporTarih)))))
        --AND ((NULLIF(@SonTarih,'') IS NULL OR (@SonTarih>=CONVERT(date, DATEADD(day,0,r.RaporTarih)))))
        --AND ((NULLIF(@Icerik,'') IS NULL OR (di.Icerik like '%'+@Icerik+'%') OR NULLIF(r.ParentRaporId,'') IS NOT NULL))
            --Yetki kontrol kısmı
        and (
        dy.DosyaYetkiId is null or  
        (
            dy.SiteId=@SiteId and

            ( 
                 r.DosyaYetkiId is null or  r.[HerkeseGosterilsin]=1 or -- herkese açık demek oluyor
                 (@DepartmanTurId=1 and @SubeKategoriKod='INGENELMUDURLUK' ) or -- Kategori Kod : INGENELMUDURLUK ve yönetim departmanında ise tüm dosyaları görebilsin
                 (
                    (
                        -- Isnull(dys.SubeId,'')='' or 
                        -- dys.SubeId=@SubeId  

                        exists(select 1 from [dbo].[DosyaYetkiSube] dys where dys.SubeId=@SubeId ) 
                        or not exists(select 1 from [dbo].[DosyaYetkiSube] dys where dys.DosyaYetkiId=r.DosyaYetkiId )

                    )and
                    (
                        @DepartmanTurId=1 -- @DepartmanTurId=1 ise şubenin yönetici olarak o şubede ki tüm dosyaları görebilir
                        -- or Isnull(dyd.DepartmanId,'')='' 
                        -- dyd.DepartmanId=@DepartmanId

                        or  exists(select 1 from [dbo].[DosyaYetkiDepartman] dyd where dyd.DepartmanId = @DepartmanId)  
                        or not exists(select 1 from [dbo].[DosyaYetkiDepartman] dyd where dyd.DosyaYetkiId = r.DosyaYetkiId )

                    )and
                    (
                        @DepartmanTurId=1 -- @DepartmanTurId=1 ise şubenin yönetici olarak o şubede ki tüm dosyaları görebilir
                        -- or Isnull(dyp.PersonelId,'')='' 
                        --  dyp.PersonelId=@KullaniciId 
                        or  exists(select 1 from [dbo].[DosyaYetkiPersonel] dyp where dyp.PersonelId=@KullaniciId ) 
                        or not exists(select 1 from [dbo].[DosyaYetkiPersonel] dyp where dyp.DosyaYetkiId = r.DosyaYetkiId )
                    )
                 )
            )
        )
 )
         ),
         cte AS
         (
          SELECT CAST(ROW_NUMBER() OVER(order by
  CASE WHEN (RTRIM(LTRIM(@SortColumn)) IS NULL)
                 THEN KayitTarih END DESC,
 CASE WHEN (@SortColumn = 'Baslik' AND @SortOrder=1)
              THEN Baslik  END ASC,
  CASE WHEN (@SortColumn = 'Baslik' AND @SortOrder=0)
              THEN Baslik  END DESC,
  CASE WHEN (@SortColumn = 'Tarih' AND @SortOrder=1)
              THEN KayitTarih  END ASC,
  CASE WHEN (@SortColumn = 'Tarih' AND @SortOrder=0)
              THEN KayitTarih  END DESC


          ) AS REAL) AS NUMBER, 1 AS Indent,  Parent.* FROM EMP Parent WHERE Parent.ParentRaporId IS NULL
          UNION ALL
           SELECT  c.NUMBER + (CAST(ROW_NUMBER() OVER(ORDER BY
              CASE WHEN (RTRIM(LTRIM(@SortColumn)) IS NULL)
                 THEN child.KayitTarih END DESC,
 CASE WHEN (@SortColumn = 'Baslik' AND @SortOrder=1)
              THEN child.Baslik  END ASC,
  CASE WHEN (@SortColumn = 'Baslik' AND @SortOrder=0)
              THEN child.Baslik  END DESC,
  CASE WHEN (@SortColumn = 'Tarih' AND @SortOrder=1)
              THEN child.KayitTarih  END ASC,
  CASE WHEN (@SortColumn = 'Tarih' AND @SortOrder=0)
              THEN child.KayitTarih  END DESC



           ) AS REAL)/POWER(10, c.Indent) ) AS NUMBER, c.Indent + 1 AS Indent,  child.* FROM EMP 
    child INNER JOIN cte c ON child.ParentRaporId= c.RaporId

         ),Count_CTE AS (
              SELECT COUNT(*) AS [TotalCount]
         FROM cte
           WHERE (NULLIF(@Baslik,'') IS NULL OR (cte.Baslik like '%'+@Baslik+'%' ))
        AND ((NULLIF(@IlkTarih,'') IS NULL OR (@IlkTarih<=CONVERT(date, 
       DATEADD(day,0,cte.RaporTarih)))))
        AND ((NULLIF(@SonTarih,'') IS NULL OR (@SonTarih>=CONVERT(date, 
      DATEADD(day,0,cte.RaporTarih)))))
      )         SELECT * FROM cte, Count_CTE
       WHERE (NULLIF(@Baslik,'') IS NULL OR (cte.Baslik like '%'+@Baslik+'%' ))
        AND ((NULLIF(@IlkTarih,'') IS NULL OR (@IlkTarih<=CONVERT(date, DATEADD(day,0,cte.RaporTarih)))))
        AND ((NULLIF(@SonTarih,'') IS NULL OR (@SonTarih>=CONVERT(date, DATEADD(day,0,cte.RaporTarih)))))           
        ORDER BY cte.NUMBER 
        OFFSET (@PageNumber-1)*@PageSize ROWS FETCH NEXT @PageSize ROWS ONLY        
   END

Add OPTION RECOMPILE and transform the non-SARGable where clauses. see http://sommarskog.se/dyn-search.html

eg

((NULLIF(@IlkTarih,'') IS NULL OR (@IlkTarih<=CONVERT(date, DATEADD(day,0,cte.RaporTarih)))))

should be

@IlkTarih is null or cast(@IlkTarih as date)<=cte.RaporTarih

I resolved this issue with adjust memory on SQL Server management studio because of low memory of db server.

If record is high, then query execution can take a long time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding Execute package in ssis causes Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. working on remote windows .net Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding C# entityframework core throwing Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding 'Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.' Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding with Azure SQL database ExecuteQueryin linq :Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding SQL Server Exception:Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM