我在SQL Server 2008中编写了一个由aspx网页调用的视图。
当我使用一个标准(例如Area = Townville
)运行它时它运行正常,但使用两个(例如Area = Townville AND County = Townshire
)它需要很长时间,尽管找到完全相同的结果(即Townville位于Townshire )。
由于我们的一个客户施加的限制,它必须像这样工作(区域和县都有,即使县不一定是必要的)。
我认为这个观点本身并没有任何问题,因为我已经将它重写为效率更高但仍然需要很长时间的观点。
编辑 - 它实际上并不是SQL(我认为),因为我通过服务器尝试了相关标准。我认为它必须是调用它的网页。这是在c# -
中调用的地方 string MySQLString;
MySQLString = "SELECT [L1_Name], [L2_Name], [L3_Name], [Spare1], [Spare2], [Spare3], [Spare4], [Spare5], [Operation_Name], [Team_Name], [LatestDate], [Issues], [Next], [Postcode], [Latitude], [Longitude], [OperationOrder], [FKID_Contract], [FKID_GeoType], [Geometry] FROM [vw_Report_Latest_v3_1] WHERE [FKID_Contract]=@Contract";
if ((string)Session["TSAreaString"] != "") { MySQLString = MySQLString + " AND [L1_Name]=@PA1"; }
if ((string)Session["TSSiteString"] != "") { MySQLString = MySQLString + " AND [L2_Name]=@PA2"; }
if ((string)Session["TSFeatureString"] != "") { MySQLString = MySQLString + " AND [L3_Name]=@PA3"; }
if ((string)Session["TSS1"] != "") { MySQLString = MySQLString + " AND [Spare1]=@S1"; }
if ((string)Session["TSS2"] != "") { MySQLString = MySQLString + " AND [Spare2]=@S2"; }
if ((string)Session["TSS3"] != "") { MySQLString = MySQLString + " AND [Spare3]=@S3"; }
if ((string)Session["TSS4"] != "") { MySQLString = MySQLString + " AND [Spare4]=@S4"; }
if ((string)Session["TSS5"] != "") { MySQLString = MySQLString + " AND [Spare5]=@S5"; }
if ((string)Session["TSTaskString"] != "") { MySQLString = MySQLString + " AND [Operation_Name]=@PA4"; }
if ((string)Session["TSTeamString"] != "") { MySQLString = MySQLString + " AND [Team_Name]=@Team"; }
//finish
MySQLString = MySQLString + " ORDER BY [OperationOrder], [L1_Name], [L2_Name], [L3_Name], [Operation_Name], [Team_Name]";
try
{
Conn.Open();
SqlCommand Cmd = new SqlCommand(MySQLString, Conn);
Cmd.Parameters.AddWithValue("@Contract", Convert.ToInt32(invCID.Text));
if (lblPA1.Text != "All") { Cmd.Parameters.AddWithValue("@PA1", (string)Session["TSAreaString"]); }
if (lblPA2.Text != "All") { Cmd.Parameters.AddWithValue("@PA2", (string)Session["TSSiteString"]); }
if (lblPA3.Text != "All") { Cmd.Parameters.AddWithValue("@PA3", (string)Session["TSFeatureString"]); }
if (lblS1V.Text != "All") { Cmd.Parameters.AddWithValue("@S1", (string)Session["TSS1"]); }
if (lblS2V.Text != "All") { Cmd.Parameters.AddWithValue("@S2", (string)Session["TSS2"]); }
if (lblS3V.Text != "All") { Cmd.Parameters.AddWithValue("@S3", (string)Session["TSS3"]); }
if (lblS4V.Text != "All") { Cmd.Parameters.AddWithValue("@S4", (string)Session["TSS4"]); }
if (lblS5V.Text != "All") { Cmd.Parameters.AddWithValue("@S5", (string)Session["TSS5"]); }
if (lblPA4.Text != "All") { Cmd.Parameters.AddWithValue("@PA4", (string)Session["TSTaskString"]); }
if (lblTeam.Text != "All") { Cmd.Parameters.AddWithValue("@Team", (string)Session["TSTeamString"]); }
SqlDataAdapter MyAdapter = new SqlDataAdapter(Cmd);
Cmd.CommandTimeout = 300;
DataTable dt = new DataTable();
MyAdapter.Fill(dt);
GridView1.DataSourceID = "";
Session["MyTable"] = dt;
GridView1.DataSource = Session["MyTable"];
GridView1.DataBind();
}
finally
{
if (Conn != null) { Conn.Close(); }
}
因此我们创建了MySQLString,它解决了vw_Report_Latest_v3_1视图。根据我们的需要添加标准。
在我的具体例子中,Session [" TSAreaString"]被设置为一个特定的城镇,这是很好的,并在大约2秒内加载(它是一个大城市)。这与L1_Name。
有关然而,会话[" TSS3"],在我们的演示案例中是我们的县,是导致这种速度减慢到爬行的那个。这与Spare3有关。
同样,在SQL Server 2008的视图中,这似乎不是问题。但它确实在执行它的页面上存在问题。