在SSMS中快速查询,但在C#应用程序中运行缓慢/无法运行

时间:2018-07-09 21:00:06

标签: c# sql-server reporting-services

我知道还有很多其他的帖子和文章,但是我现在一直在寻找解决方案,而对我来说没有任何效果。我有一个查询,该查询生成给定服务器上列出的所有SQL Reporting Services(SSRS)报告及其相关参数和有效值的列表:

;WITH XMLNAMESPACES (
DEFAULT 'http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition',
'http://schemas.microsoft.com/SQLServer/reporting/reportdesigner' AS rd --ReportDefinition
)
SELECT 
   NAME as Name
   , PATH as Path
   , x.value ('@Name', 'VARCHAR(100)') AS ReportParameterName
   , x.value ('DataType[1]', 'VARCHAR(100)') AS DataType
   , x.value ('AllowBlank[1]', 'VARCHAR(50)') AS AllowBlank
   , x.value ('Prompt[1]', 'VARCHAR(100)') AS Prompt
   , x.value ('Hidden[1]', 'VARCHAR(100)') AS Hidden
   , x.value ('data(DefaultValue/Values/Value)[1]', 'VARCHAR(100)') AS DefaultValue
   , y.value ('Value[1]','VARCHAR(100)') AS VaildValues
   , y.value ('Label[1]','VARCHAR(100)') AS VaildValuesLabel
FROM (
   SELECT  PATH
           , NAME
           , CAST(CAST(content AS VARBINARY(MAX)) AS XML) AS ReportXML 
   FROM Catalog 
   WHERE CONTENT IS NOT NULL AND TYPE = 2
   ) A
CROSS APPLY ReportXML.nodes('/Report/ReportParameters/ReportParameter') R(x)
OUTER APPLY R.x.nodes('ValidValues/ParameterValues/ParameterValue') P(y) 
ORDER BY NAME

此查询大约需要4分半钟才能在SSMS中运行。我想以C#DataTable的形式获取此查询的结果,以用作在不同服务器上运行的存储过程的参数。不幸的是,当通过我的C#代码运行SQL脚本时,假设它完全可以完成(3小时后放弃),则至少需要3个小时才能运行。

我知道从SSMS和应用程序运行时SET选项是不同的,因此,我尝试手动使设置与SSMS中的设置相同。这是代码段:

DataSet ds = new DataSet();
string SQL_Script = File.ReadAllText(runRdlQueryFilePath);
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
        {

            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = SQL_Script;
            da.SelectCommand = cmd;
            conn.Open();
            Console.WriteLine("setting QUOTED_IDENTIFIER on...");
            using (SqlCommand comm = new SqlCommand("SET QUOTED_IDENTIFIER ON", conn))
            {
                comm.ExecuteNonQuery();
            }
            Console.WriteLine("setting ANSI_NULLS on...");
            using (SqlCommand comm = new SqlCommand("SET ANSI_NULLS ON", conn))
            {
                comm.ExecuteNonQuery();
            }
            Console.WriteLine("setting ARITHABORT on...");
            using (SqlCommand comm = new SqlCommand("SET ARITHABORT ON", conn))
            {
                comm.ExecuteNonQuery();
            }
            //this one will not run for some reason but I think it is the default when running from C# client anyways
            //using (SqlCommand comm = new SqlCommand("SET CONCAT_NULL_YEILDS_NULL ON", conn))
            //{
            //    comm.ExecuteNonQuery();
            //}
            Console.WriteLine("setting  XACT_ABORT on...");
            using (SqlCommand comm = new SqlCommand("SET XACT_ABORT ON", conn))
            {
                comm.ExecuteNonQuery();
            }
            Console.WriteLine("setting LOCK_TIMEOUT -1...");
            using (SqlCommand comm = new SqlCommand("SET LOCK_TIMEOUT -1", conn))
            {
                comm.ExecuteNonQuery();
            }
            Console.WriteLine("Executing get rdl info query, this may take some time..."); 

            da.Fill(ds);

            //I have also tried this method of filling the DataSet...
            /*
            Server server = new Server(new ServerConnection(conn));
                //bool queryRan = true;
                //read sql file
                string script = File.ReadAllText(runRdlQueryFilePath);
            Console.WriteLine("Executing get rdl info query, this may take some time...");
            ds = server.ConnectionContext.ExecuteWithResults(script);
            */
        }

我没有看到参数嗅探在这里会成为一个问题,因为查询没有参数,但是我尝试放置一个

OPTION (RECOMPILE)

在我的查询中只是出于娱乐目的,也没有成功。不幸的是,我不是该数据库的所有者,也没有在该数据库上创建存储过程的权限,因此我没有尝试将查询转换为存储过程。

有什么想法可以阻止查询在应用程序中有效执行?

感谢您的帮助!

0 个答案:

没有答案