Test the performance difference between sp_executesql and exec)

Source: Internet
Author: User
The expanded storage process of sp_executesql is similar to the Execute function of T-SQL, but it is slightly different that the execution plan executed through sp_executesql will be cached and can be reused. Test: NZ. perfectaction nzperfect@gmail.com
The following test shows the performance differences between sp_executesql and exec.
Create Database t_dbgouse t_dbgocreate table Tb (ID int identity (1, 1) primary key, name varchar (20 )) goinsert into TB select 'A' insert into TB select 'B' insert into TB select 'C' insert into TB select 'D' insert into TB select 'E' insert into TB select' f'go -- clear all the elements in the cache DBCC freeproccache -- view the select SQL as exec_ SQL cache used by the t_db database, objtype as exec_type, * from Master .. syscacheobjects where dbid = db_id ('t_db') and SQL like '% select * from TB where name %' and SQL not like '% syscacheobjects %' order by SQL results are empty,: -- Test exec, run the following SQL block declare @ SQL varchar (2000) Declare @ name varchar (20) declare @ I intset @ I = 1 while @ I <= 6 begin if @ I = 1 Set @ name = 'A' if @ I = 2 set @ name = 'B' if @ I = 3 set @ name = 'C' if @ I = 4 set @ name = 'D' if @ I = 5 set @ name = 'E' if @ I = 6 set @ name = 'F' set @ SQL = 'select * from TB where name = ''' + @ name + ''' exec (@ SQL) set @ I = @ I + 1end -- view the cache used by the t_db database select SQL as exec_ SQL, objtype as exec_type, * from master .. syscacheobjects where dbid = db_id ('t_db ') and SQL like '% select * from TB where name %' and SQL not like '% syscacheobjects %' order by SQL results have six records: for example, this indicates that SQL statements executed by SQL Server for exec use different caches even if the where field is the same but with different values. -- Test the execution of sp_executesql. Run the following SQL statement: declare @ SQL nvarchar (2000) Declare @ name nvarchar (20) declare @ I intset @ I = 1 while @ I <= 6 begin if @ I = 1 Set @ name = 'A' if @ I = 2 set @ name = 'B' if @ I = 3 set @ name = 'C' if @ I = 4 set @ name = 'D' if @ I = 5 set @ name = 'E' if @ I = 6 set @ name = 'F' set @ SQL = 'select * from TB where name = @ name' exec sp_executesql @ SQL, n' @ name nvarchar (20) ', @ name set @ I = @ I + 1end -- view the cache select SQL as exec_ SQL, Objtype as exec_type, * from Master .. syscacheobjects where dbid = db_id ('t_db ') and SQL like '% select * from TB where name %' and SQL not like '% syscacheobjects %' order by SQL results Add a record in addition to the preceding six records: for example, it indicates that SQL statements executed by SQL Server for sp_executesql do not need to be re-compiled if the where field is the same and the values are different, but the same cache plan is used for execution. -- Test completed: drop database t_dbgodrop table tbgoIn summary, the sp_executesql execution plan will be cached, but execute is not allowed. If a large number of repeated queries are performed, sp_executesql can improve the database performance better than execute.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.