【发布时间】:2016-03-16 17:44:05
【问题描述】:
我想从表 A 中获取详细信息,其中包含列 id、dept、course、created_date 以及与存储过程中的表 B 具有外键关系的大约 35 列。
最后,我想通过检查表 A 中的“id”是否可用作“userid”列以及表“B”中具有 null 或“NO”的另一列来提取数据。
表 B 共有 220 列。
通过获取数据
查询 #1
Select *
from A
where Exists (select userid from B where charge = 'no')
and created_date >= '2015-01-01'
and created_date <= '2016-01-01'
查询 #2
Select *
from A aa
left join B bb on bb.userid = aa.id
and charge = 'no'
where
aa.created_date >= '2015-01-01'
and aa.created_date <= '2016-01-01'.
其实性能太慢了。
观察:
如果我在 SQL Server 存储过程中使用硬编码值执行日期,它会通过提取属于 6 个月的 6000 条记录来非常快地返回数据
同样,如果我在同一个存储过程中使用动态参数执行,它返回的速度非常慢(亚马逊服务器本身需要 45 秒)
我尝试使用面临同样问题的实体 linq。
我也想通过非常快速有效的方式获取数据。我正在使用 ASP.NET MVC 4 Web 应用程序并从服务方法执行存储过程以获取数据列表并将其绑定到网格。
请帮忙找一下。
【问题讨论】:
-
嗯...在 SSMS 中使用参数超级快,但作为存储过程超级慢...听起来像是参数嗅探的典型症状。 sqlinthewild.co.za/index.php/2007/11/27/parameter-sniffing
-
您能否发布您的 LINQ to Entities 查询
-
你在 created_date 列上有索引吗
标签: c# sql-server asp.net-mvc linq