Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent timeout in query?

SELECT C.CompanyName,
       B.BranchName, 
       E.EmployerName,
       FE.EmployeeUniqueID,
       pcr.EmployerUniqueID, 
       Case when FE.Status_id= 1 then 1 else 0 end IsUnPaid, 
       Case when re.EmployeeUniqueID IS NULL OR re.EmployeeUniqueID= '' then 0 else 1 end AS 'EmployeeRegistration',
       FE.IncomeFixedComponent,
       FE.IncomeVariableComponent, 
       Convert(varchar(11), Fe.PayStartDate, 106) as PayStartDate,
       Convert(varchar(11), Fe.PayEndDate, 106) as PayEndDate,
       S.StatusDescription, 
       FE.IsRejected, 
       FE.ID 'EdrID',   
       Convert(varchar(20), tr.TransactionDateTime, 113) as TransactionDateTime, 
       tr.BatchNo, 
       tr.IsDIFCreated, 
       Convert(varchar(20),tr.DIFFileCreationDateTime,113) as DiffDateTime
    From File_EdrEntries FE 
    JOIN PAFFiles pe ON pe.ID = FE.PAFFile_ID
    inner Join RegisteredEmployees RE
    ON RE.EmployeeUniqueID= FE.EmployeeUniqueID
    inner join File_PCREntries pcr on pe.ID=pcr.PAFFile_ID 
    JOIN Employers E ON E.EmployerID = pcr.EmployerUniqueID
    JOIN Branches B ON B.BranchID = E.Branch_ID 
    JOIN companies C ON C.COMPANYID = B.COMPANY_ID  
    JOIN Statuses S ON S.StatusID = FE.Status_ID
    JOIN Transactions tr on tr.EDRRecord_ID= fe.ID
    where  E.Branch_id=3
    AND FE.IsRejected=0 AND FE.Status_id= 3 and tr.BatchNo is not null
    AND Re.Employer_ID= re.Employer_ID;

THis query is supposed to return 10 million or more records and it usually causes timeout because of large no of records. So how can I improve its performance becauses I have done in where condition what I could.

like image 564
Covert Avatar asked Apr 10 '26 14:04

Covert


1 Answers

First of all, you need to

  1. optimize query more
  2. Add required Indexes to tables involved in query

Then,

You can use this, to increase Query Timeout:

SET LOCK_TIMEOUT 1800;  
SELECT @@LOCK_TIMEOUT AS [Lock Timeout];

Also, refer This Post

like image 96
Vikrant Avatar answered Apr 12 '26 04:04

Vikrant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!