简体   繁体   中英

SQL taking too much time for executing a query

i am using below query to generate some information of around 7k lines. but the issue is that the query is taking too much time to complete just 7k line. i need to fine tune it.

select mo.CardUId,mo.CarParkUId,mo.PersonUId,mo.CardType,mo.CardNr,pk.LastName            company_name,ab.Name carpark_name,
mo.ValidFrom,mo.ValidUntil,mo.LastTccTime,p.LastName    customerName,mo.GroupName, mo.Name,p.Type,
mo.LastTccNr,mo.DisplayableCardCarrierNrId serialnr,mo.BlacklistEntry,

mo.GroupNumber from Person p


LEFT outer join vwAllCards mo on p.PersonUId=mo.PersonUId
left outer join Person pk on pk.PersonUId=p.CompanyDepartmentUId
left outer join CarPark ab on ab.CarParkUId=mo.CarParkUId
where p.Type=0 and mo.Name is not null
order by p.LastName

I use the SQL Profiler to auto create the indexes needed for a fine tune query.

  • Enter in SSMS (SQL Server Management Studio) -> Tools -> SQL Server Profiler.

  • Click Connect, and then click Run, to start a new trace.

  • Execute your slow query now, so the profiler can capture and record it.

  • Stop the query and File -> Save it.

  • In the menu, click Tools -> Database Engine Tuning advisor.

  • Click Connect when it opens, and in Workload, select file, and click the first button on the right (binoculars one). Then select the file you have just saved.

  • Select now the checkbox of your database, and click the "Start Analysis" button (below the "View" and "Action" menu).

  • Once it finish, open "Action" menu, and click in "Apply Recommendations".

Now you should have a fast and fine tuned query.

use with (nolock)

mo.GroupNumber from Person p with (nolock)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM