查询SQL语句成本

 在有些大系统中,客户端查询非常复杂,需要显示很多字段和过滤很多条件,例如TFS中的条件过滤,或则用户希望客户端查询能像EXCEL过滤那样方便

这样可能导致查询语句非常复杂,此时的效率也比较低.

 

TFS查询条件界面:

 

 

如果可以获取查询语句的成本,当SQL语句执行成本很大(可能执行时间会很长时),系统自动提示用户是否继续或则大概需要多长时间,这样会提高用户体验.

 

下举一例:

查询Sql Server中的所有列信息表:sys.all_columns

在Sql server 2k8 management studio 中先后执行下面每行语句即可得到Sql语句的成本, 即为第二个语句执行结果中第一行中TotalSubtreeCost字段值.

set showplan_all on
select * from sys.all_columns

 

 第二个语句(select * from sys.all_columns)执行结果:

 

StmtTextTotalSubtreeCostStmtIdNodeIdParentPhysicalOpLogicalOpEstimateRowsEstimateIOEstimateCPUAvgRowSize
select * from sys.all_columns0.1736313110NULLNULL5270NULLNULLNULL
 |--Concatenation0.1736313121ConcatenationConcatenation527000.000527303
0.01454595132FilterFilter65908.44E-04303
0.01370243143Compute ScalarCompute Scalar65906.59E-05305
0.01363653154Clustered Index ScanClustered Index Scan6590.012754630.0008819179
0.15855831362Compute ScalarCompute Scalar461100.0004611303
0.152382813736Compute ScalarCompute Scalar461100.0011905164
0.151192313837Clustered Index ScanClustered Index Scan46110.13793980.0132525167

 

再执行 set showplan_all off结束查询计划分析结果.

 

此时可以应用showplan_all来获取Sql查询的成本.

如下为C#简单实现上述应用,在VS2k8中编译通过,只需正确更改Data Source即可运行:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnectionStringBuilder connectionBuilder = new SqlConnectionStringBuilder();
            connectionBuilder.Add("Data Source", "your server name or ip");
            connectionBuilder.Add("Initial Catalog", "master");
            connectionBuilder.Add("Integrated Security", "True");

            string sqlStr = "select * from sys.all_columns";
            Console.WriteLine(GetSqlCost(connectionBuilder.ToString(), sqlStr).ToString());
            Console.Read();
        }

        static double GetSqlCost(string connectStr, string strSQL)
        {
            double sqlCost;
            int timeOut;
            SqlDataAdapter adapter;
            SqlConnection connection = new SqlConnection(connectStr);
            SqlCommand command;
            DataTable table;
            timeOut = 180;
            adapter = null;

            try
            {
                connection.Open();
                command = new SqlCommand("set showplan_all on ", connection);
                command.ExecuteNonQuery();
                command.CommandText = strSQL;
                adapter = new SqlDataAdapter(strSQL, connection);
                adapter.SelectCommand.CommandTimeout = timeOut;
                table = new DataTable();
                adapter.Fill(table);
                command.CommandText = "set showplan_all off";
                command.ExecuteNonQuery();
                sqlCost = System.Convert.ToDouble(table.Rows[0]["TotalSubtreeCost"]);
            }
            finally
            {
                command = null;
                connection.Close();
            }
            return sqlCost;
        }
    }

 

}

转载于:https://www.cnblogs.com/tiwlin/archive/2010/06/25/1764967.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值