我在IIS上运行Laravel 4,SQL Server作为数据库。我在Laravel中使用雄辩来查询数据库。除了一个查询之外的所有查询都使用此方法非常快速未运行的一个查询在30秒后超时。当我从SQL事件探查器中获取原始SQL并从命令行运行它时,它会在1秒内完成。我试图找出为什么从Web应用程序运行SQL和从命令行运行它(SQL Server Management Studio)之间存在这样的时间差异。我们在运行每个查询之前清除了缓存,结果是一样的。
我的Laravel Eloquent代码(超时):
$checks = SQLServerHostLastCheck::where('organization_id', '=', Session::get('currentIssueTrakOrganizationID'))
->where('hostname', '=', $hostname)
->orderBy('check_level')
->orderBy('instance_name')
->orderBy('check_description')
->get();
我也尝试过这种方式(超时):
$checks = DB::table('sql_server_host_last_check_all_v')
->where('organization_id', '=', Session::get('currentIssueTrakOrganizationID'))
->where('hostname', '=', $hostname)
->get();
这是SQL事件探查器的原始SQL(在1秒内完成):
select * from [sql_server_host_last_check_all_v] where [organization_id] = 1 and [hostname] = 'PSMWISSUE01' order by [check_level] asc, [instance_name] asc, [check_description] asc;
此查询返回66行,总共有15列。每个字段中只有少量数据。
答案 0 :(得分:0)
我通过创建存储过程而不是构建视图的复杂SQL查询/连接的视图来解决这个问题。结果现在立即返回。