标签:microsoft cti object 复杂 问题: lis use 参数 bin
SELECT @sql = ‘SELECT mycol FROM tbl WHERE keycol = ‘ + convert(varchar, @value) EXEC(@sql)
cmd.CommandText = "SELECT mycol FROM tbl WHERE keycol = " + value.ToString();
EXEC sp_executesql N‘SELECT mycol FROM dbo.tbl WHERE keycol = @value‘, N‘@value int‘, @value = @value
cmd.CommandText = "SELECT mycol FROM dbo.tbl WHERE keycol = @value"; cmd.Parameters.Add("@value", SqlDbType.Int); cmd.Parameters["@value"].Value = value;
SELECT * FROM Orders WHERE OrderID = 11000如果你提交的是下面这种方式,那么编译后就得到了上面这个语句:
EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderID = @p1‘, N‘@p1 int‘, @p1 = 11000
USE Northwind GO EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘20000101‘ EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘19980101‘ EXEC sp_executesql N‘select * from Orders where OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘19980101‘执行计划如下图,你会发现前两个执行语句使用同一个查询计划:
USE Northwind GO DBCC FREEPROCCACHE GO EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘20000101‘ EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘19980101‘ EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘19980101‘这一次,三个查询计划都一样了。
USE Northwind GO DBCC FREEPROCCACHE GO CREATE SCHEMA Schema2 GO CREATE USER User1 WITHOUT LOGIN WITH DEFAULT_SCHEMA = dbo CREATE USER User2 WITHOUT LOGIN WITH DEFAULT_SCHEMA = Schema2 GRANT SELECT ON Orders TO User1, User2 GRANT SHOWPLAN TO User1, User2 GO EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘20000101‘ GO EXECUTE AS USER = ‘User1‘ EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘19980101‘ REVERT GO EXECUTE AS USER = ‘User2‘ EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘19980101‘ REVERT GO DROP USER User1 DROP USER User2 DROP SCHEMA Schema2 GO
SELECT qs.plan_handle, a.attrlist FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) est CROSS APPLY (SELECT epa.attribute + ‘=‘ + convert(nvarchar(127), epa.value) + ‘ ‘ FROM sys.dm_exec_plan_attributes(qs.plan_handle) epa WHERE epa.is_cache_key = 1 ORDER BY epa.attribute FOR XML PATH(‘‘)) AS a(attrlist) WHERE est.text LIKE ‘%WHERE OrderDate > @orderdate%‘ AND est.text NOT LIKE ‘%sys.dm_exec_plan_attributes%‘
date_first=7 date_format=1 dbid=6 objectid=158662399 set_options=251 user_id=5 date_first=7 date_format=1 dbid=6 objectid=158662399 set_options=251 user_id=1
EXEC sp_executesql N‘SELECT * FROM dbo.Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘20000101‘
date_first=7 date_format=1 dbid=6 objectid=549443125 set_options=251 user_id=-2
EXECUTE AS USER = ‘appuser‘ go -- 需要执行的SQL语句 go REVERT但是,如果这个账号访问的资源不在当前库中,就会报错。这个时候,可以使用EXECUTE AS LOGIN来替代,但是这个方案需要有服务器级别的权限。
SELECT ‘<‘ + est.text + ‘>‘ FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) est WHERE est.text LIKE ‘%能标识出语句特征的SQL代码%‘注意这个要运行在文本模式,默认是网格模式,SSMS会使用空格来替换换行符。其中尖括号只是为了作为分隔作用。
EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘20000101‘即使你按这种格式来执行也无所谓:
EXEC sp_executesql N‘SELECT * FROM Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, ‘20000101‘
SELECT * FROM Orders WHERE OrderID = 11000
Seek Keys[1]: Prefix: [Northwind].[dbo].[Orders].OrderID = Scalar Operator(CONVERT_IMPLICIT(int,[@1],0))[@1]揭示了语句被自动参数化(auto-parameterised)
SELECT ... FROM dbo.Orders WHERE Status = ‘Delayed‘在Northwind中的这个表不存在stauts列,更不存在Delayed这个值,只是演示需要,当SQL Server参数化这个查询时,由于产生查询计划的要求必须可以覆盖所有参数,所以优化器并不会使用status上的列。
DECLARE @x int SELECT ... FROM dbo.Orders WHERE Status = ‘Delayed‘ AND @x IS NULL
USE Northwind GO DBCC FREEPROCCACHE GO EXEC sp_executesql N‘SELECT * FROM dbo.Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, @orderdate = ‘19960101‘ GO EXEC sp_create_plan_guide @name = N‘MyGuide‘, @stmt = N‘SELECT * FROM dbo.Orders WHERE OrderDate > @orderdate‘, @type = N‘SQL‘, @module_or_batch = NULL, @params = N‘@orderdate datetime‘, @hints = N‘OPTION (TABLE HINT (dbo.Orders , INDEX (OrderDate)))‘ GO EXEC sp_executesql N‘SELECT * FROM dbo.Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, @orderdate = ‘19980101‘ GO EXEC sp_control_plan_guide N‘DROP‘, N‘MyGuide‘
USE Northwind GO DBCC FREEPROCCACHE SET ARITHABORT ON GO EXEC sp_executesql N‘SELECT * FROM dbo.Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, @orderdate = ‘19990101‘ GO DECLARE @plan_handle VARBINARY(64), @rowc INT SELECT @plan_handle = plan_handle FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) est WHERE est.TEXT LIKE ‘%Orders WHERE OrderDate%‘ AND est.TEXT NOT LIKE ‘%dm_exec_query_stats%‘ SELECT @rowc = @@rowcount IF @rowc = 1 EXEC sp_create_plan_guide_from_handle ‘MyFrozenPlan‘, @plan_handle ELSE RAISERROR ( ‘%d plans found in plan cache. Canno create plan guide‘, 16, 1, @rowc ) GO -- Test it out! SET ARITHABORT OFF GO EXEC sp_executesql N‘SELECT * FROM dbo.Orders WHERE OrderDate > @orderdate‘, N‘@orderdate datetime‘, @orderdate = ‘19960101‘ GO SET ARITHABORT ON EXEC sp_control_plan_guide ‘DROP‘, ‘MyFrozenPlan‘
理解性能的奥秘——应用程序中慢,SSMS中快(6)——SQL Server如何编译动态SQL
标签:microsoft cti object 复杂 问题: lis use 参数 bin
原文地址:http://blog.csdn.net/dba_huangzj/article/details/53378708