SQL Server Stored Procedures and advanced applications

Source: Internet
Author: User
SQL Server Stored Procedures and advanced applications today mainly discuss the application and Optimization Solutions of SQL Server Stored Procedures. Stored Procedure: as I have summarized the stored procedure in my previous article, I will not go into details here. Today, let's take a look at the advanced applications of stored procedures. First, let's take a look at what the stored procedure can contain:

SQL Server Stored Procedures and advanced applications today mainly discuss the application and Optimization Solutions of SQL Server Stored Procedures. Stored Procedure: as I have summarized the stored procedure in my previous article, I will not go into details here. Today, let's take a look at the advanced applications of stored procedures. First, let's take a look at what the stored procedure can contain:

SQL Server Stored Procedures and advanced applications

Today, we will mainly discuss the application and Optimization Solutions of SQL Server Stored Procedures.

Stored Procedure: as I have summarized the stored procedure in my previous article, I will not go into details here. Today, let's take a look at the advanced applications of stored procedures.

First, let's take a look at what the stored procedure can contain:

It can be seen that the Stored Procedure contains not only a single select statement, but also a select statement block and a Hong Kong server lease, such as Case When, but also logical control statements, such as if-else.

Note: The stored procedure can also contain insert, update, and delete statements.

Next, let's take a look at the usage of the Extended Stored Procedure xp_mongoshell.

Case: Enable xp_cmdshell

Enable the Extended Stored Procedure in SQL Server2000:

Use master

Exec sp_addextendedproc xp_cmdshell, 'loglog70. dll'

Go

Enable xp_cmdshell in SQL Server 2005 or SQL Server 2008

Sp_configure 'xp _ Your shell', 1

Go

Reconfigure -- make sp_configure take effect immediately

Go

-- Enable advanced options

Sp_configure 'show advanced options', 1

Go

Reconfigure

Go

Note: There are a total of 36 server options. By default, only 10 of the sp_configure stored procedures are displayed. The results do not contain advanced options. Hong Kong servers, all new SQL Server configuration options will not appear in this streamlined list. However, you can use the show advanced options command parameter to display all options on SQL Server.

Case: Disable/delete xp_cmdshell

SQL server 2000 Delete Extended Stored Procedures

-- Delete the Extended Stored Procedure

Use master

Exec sp_dropextendedproc 'xp _ export shell'

Go

How to Improve the Performance of Stored Procedures

01, use set nocount on

Syntax
Set nocount {ON | OFF}

Note
When set nocount is ON, no count is returned (indicating the number of rows affected by the Transact-SQL statement ). When set nocount is OFF, return the count.

When set nocount is ON, the DONE_IN_PROC information of each statement in the stored procedure is not sent to the client. When you use a utility provided by Microsoft SQL Server to execute a query) "nn rows affected" is not displayed in the query results at the end ".

02. Reduce optional parameters.

03. Optimize SQL statements

001. Avoid frequent access to the same or multiple tables.

002. Avoid a large number of transaction operations as much as possible

003. Avoid using cursors whenever possible

004. Note the where statement syntax.

The order of statements must be considered. The order before and after condition clauses should be determined based on the index order and range, and the field order should be consistent with the index order as much as possible, with the range from large to small.

005. Use exists instead of select count () to determine whether a record exists. The count function is used only when all the rows in the statistical table are used, and count (1) is more efficient than count.

006. Note the data types connected between tables.

007, write DDL first, then DML

008, rational use of Indexes

Pay attention to index maintenance, Hong Kong Space, re-indexing regularly, and re-compiling the storage process.

, Reasonable use of tempdb system tables

I. Avoid using distinct, order by, groupby, having, and join statements whenever possible.

II. Avoid frequent creation and deletion of temporary tables

III. The data inserted in the temporary table is too large. You can use select into instead of create table.

IV. When a temporary table is used, it must be explicitly deleted at the end of the stored procedure.

V. Avoid using large temporary tables and other big data tables for link query and modification.

Use of Sqlprofiler

Sqlprofiler can detect the execution of any SQL server statement and help us analyze the final execution process of the SQL statement.

Usage: Open the tool → SQL Server Profiler as shown in Figure

Click Connect. The page is displayed.

Click to run

This interface allows you to view SQL Server trace information.

I have discussed this for the time being. I hope it will help you!

Posted on

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.