简体   繁体   English

SQL花费太多时间来执行查询

[英]SQL taking too much time for executing a query

i am using below query to generate some information of around 7k lines.我正在使用下面的查询来生成大约 7k 行的一些信息。 but the issue is that the query is taking too much time to complete just 7k line.但问题是查询需要太多时间才能完成 7k 行。 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.我使用 SQL Profiler 自动创建微调查询所需的索引。

  • Enter in SSMS (SQL Server Management Studio) -> Tools -> SQL Server Profiler.进入 SSMS (SQL Server Management Studio) -> 工具 -> 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).打开后点击Connect,在Workload中选择file,点击右侧第一个按钮(双筒望远镜)。 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)与(nolock)一起使用

mo.GroupNumber from Person p with (nolock) mo.GroupNumber 来自 Person p with (nolock)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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