Slow parameterized query speed

Source: Internet
Author: User

Slow parameterized query speed

This is the problem prototype:

 --SQL1 declare @OrderDetailID nvarchar(50) set @OrderDetailID='R1502003963580C001' select * from BillDetail where OrderDetailID=@OrderDetailID  --SQL2 select * from BillDetail where OrderDetailID='R1502003963580C001'  

PS: In addition, when SqlParameter is used each time, if it is a character type, add size as much as possible, involving the execution plan cache problem.

For example:new SqlParameter("@name", SqlDbType.Varchar, 40) { Value = name };

You still cannot get off work...

I checked again: it seems that Microsoft has encountered such a problem.

The solution is as follows: The only resolution to this problem is to use a stored querydef query instead of prepared queries.

It may be understandable (in the form of a stored procedure ):

Change the preceding parameterization to exec ('select * from BillDetail where OrderDetailID = ''' + @ OrderDetailID + '''')

This is the case for the moment! The main reason is that it cannot be reproduced and cannot be verified. If so, try it first.

The reason is that the defined string type does not match.

Declare @ OrderDetailID varchar (50) -- change from nvarchar to varchar

If you are anxious to find out the problem, just look at the following...

Summary:

The execution plan is as follows:

declare @OrderDetailID nvarchar(50)
set @OrderDetailID='R1502003963580C001'
select * from BillDetail where OrderDetailID=@OrderDetailID

There are two solutions:

1. Execution of Stored Procedures

declare @OrderDetailID1 nvarchar(20)
set @OrderDetailID1='R1502003963580C001'
exec('select * from BillDetail where OrderDetailID='''+@OrderDetailID1+'''')

2. Define the correct parameter type

declare @OrderDetailID2varchar(20)
set @OrderDetailID1='R1502003963580C001'
select * from BillDetail where OrderDetailID=@OrderDetailID2

Related Article

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.