SQL Server query optimization and transaction processing

Bowen Contents
I. index
two, view
three stored procedures
Fourth, the system stored procedure
V. trigger
six transaction
seven locks

First, the index

Index provides a pointer to point to the specified column is stored in the table of data values, and pointers are arranged according to a specified order, and then follows the pointer reaches the column containing the value.

1. What is the index

Database index is similar to the books in the catalog. In a book, without having to read the entire book, use the directory you can quickly find the information you need. In the database, index the database program without having to scan the entire table, you can find the data in them. Contents of the book is a list of words, which indicate the page numbers include various words. And the database index is set in a table or a plurality of column values, and the physical pointer indicates the logical worth of inventory data sector.

The index is an internal method SQL Server setup data, which provides a way for SQL Server to orchestrate routing the query data.

Index page is stored in the database index data pages. Index page storage address pointer keyword page to retrieve data rows and rows of data. By using the index, you can greatly improve the retrieval speed of the database and improve database performance.

2, Index Classification

1) unique index

Unique index does not allow two rows with the same index value.
If a duplicate key exists existing data, it is generally the case of most database allowed to create a unique index. When new data that the keys in a repeat, the database also refuse to accept this data. Creating a unique constraint will automatically create a unique index. Despite the unique index helps to find information, but for best performance, we recommend using the primary key constraint.

2) primary key index

Will automatically create a primary key for the primary key index table defined in the database diagram, the primary key index is a special kind of unique index.
The main construction of the index requires each primary key values are unique. When a query using the primary key index, it also allows quick access to data.

3) clustered index

In clustered index, the physical order of rows in the table with the logical (index) of the same key sequence. A table can contain only one clustered index.

4) non-clustered index

Non-clustered index based on the index page, you can find a store location records from the index in the query data.
The logic is non-clustered index key value in the order of the physical order of rows in the table do not match the data stored. Clustered indexes have faster data access speeds than non-clustered index. In SQL Server, a table can only create a clustered index, but can have multiple non-clustered indexes. Setting a primary key column, the column will default to a clustered index.

5) composite index

When creating an index, which is not only to create an index, with the same primary key is created, a combination of multiple columns may be used as an index, this index is called a composite index.
Note: only use the composite index of the first row or the entire composite index will be used to complete the index when the query data as a condition column.

6) full-text index

Full-text index is a special type of index-based functional marker, created and maintained by the SQL Server full-text engine service.
Full-text index is mainly used in large amounts of text in a search string, then the efficiency of the use of full-text index will be much more efficient than using T-SQL's LIKE keyword. Because the full-text index creation process is very different from other types of indexes.

3, creating and using indexes

There are two ways to create an index: SSMS and using T-SQL statements.

Use SSMS to create an index, as follows:

1) name of the products listed in the table to create an index

SQL Server query optimization and transaction processing

2) Right-click the name, click Indexes / Keys

SQL Server query optimization and transaction processing

3) add an index, the name for IX_name, click Close to save the table

SQL Server query optimization and transaction processing

4) Create a query using the index

select * from products with (index=IX_name) where 名称='黄瓜';

SQL Server query optimization and transaction processing
Although you can specify SQL Server for data query according to which index, but generally do not need to manually specify. SQL Server will be based on an index created automatically optimize queries.

Use the index can speed up data retrieval speed, but for each column are indexed not necessary. Because the index itself is required to maintain and take up some resources, you can select the column is indexed in accordance with the following criteria.

  • Frequently searched columns;

  • Often used to query the selected column;

  • Frequent sorting, grouping columns;

  • Columns (primary key / foreign key) is often used for connection;

Do not use the following columns to create the index:

  • It contains only a few different values ​​of the column;

  • Table contains only a few lines;

Second, the view

视图是保存在数据库中的select查询。因此,对查询指定的大多数操作也可以在视图上进行。使用视图的原因有两个,其一是处于安全考虑,用户不必看到整个数据库结构,而隐藏部分数据;其二是符合用户日常业务逻辑,使其对数据更容易理解。

1、什么是视图

视图是另一种查看数据库中一个或多个表中的数据的方法。视图是一种虚拟表,通常是作为来自一个或多个表的行或列的子集创建的。当然,视图也可以包含全部的行和列。但是,视图并不是数据库中存储的数据值的集合,它的行和列来自查询中引用的表。在执行时,视图直接显示来自表中的数据。

视图充当着查询中指定的表的筛选器。定义视图的查询可以基于一个或多个表,也可以基于其他视图、当前数据库或其他数据库。

如下图所示,以表T和表T1为例,该视图可以包含这些表中的全部列或选定的部分列。如下图所示为一个用表T的A列和B列及表T1的B1、C1和D1列创建的视图:
SQL Server query optimization and transaction processing

视图通常用来进行以下三种操作:

  • 筛选表中的行;

  • 防止未经许可的用户访问敏感的信息;

  • 将多个物理数据表抽象为一个逻辑数据表;

1)使用视图带来的好处

对最终用户的好处:

  • 结果更容易理解;
  • 获得数据更容易;

对开发人员的好处:

  • 限制数据检索更容易;
  • 维护应用程序更方便;

2、创建和使用视图

1)创建视图

在SQL Server中,创建视图的方法有两种:使用SSMS和使用T-SQL语句。

①展开数据库test,如图所示,右击“视图”,在弹出的快捷菜单中选择“新建视图”命令(自行创建多个表插入数据)
SQL Server query optimization and transaction processing

②将A、B、C三张表添加出来
SQL Server query optimization and transaction processing

③选择希望看到列:学员名称、学员年龄、成绩,然后中下方自动生成T-SQL语句,按“crtl+R”快捷键执行语句
SQL Server query optimization and transaction processing

④选择T-SQL语句按“crtl+R”快捷键执行即可
SQL Server query optimization and transaction processing

2)使用视图的注意事项

  • 每个视图可以使用多个表;

  • 与查询相似,一个视图可以嵌套另一个视图,最好不要超过三层;

  • 视图定义中的select语句不能包含以下内容:
    order by子句,除非子啊select语句的选择列表中也有一个TOP子句;
    into关键字;
    引用临时表或表变量;

三、存储过程

SQL Server使用存储过程来避免远程发送并执行SQL代码带来的安全隐患。

1、为什么需要存储过程

当今的软件大多应用于网络中,而一般应用程序所运用的数据保存在数据库中。在没有使用存储过程的数据库应用程序中,用户大多从本地极端及客户端通过网络向服务器端发送SQL代码编写的请求,服务器端对接收到SQL代码进行语法编译后执行,并经指定结果传送回客户端,再由客户端的应用软件处理后输出。如果开发者对服务器的安全性考虑不全面,就会为非法者提供盗取数据的机会。如下图所示:
SQL Server query optimization and transaction processing
未经授权的非法者在网络中截取用户想服务器发送的SQL代码,改写后的恶意SQL代码提交到服务器编译并执行,最后非法者就比较容易地获得他所需的数据。

从图中,我们可以看到应用程序执行的过程是不安全的,主要在于以下几个方面:

  • 数据不安全:网络传送SQL代码,容易被未经授权者截取;

  • 每次提交SQL代码都要经过语法编译后再执行,影响应用程序的运行性能;

  • 网络流量大。对于反复执行的相同SQL代码,将会在网络上多次传送,增加网络传输流量;

为了解决这些问题,我们可以采用存储过程把对数据库操作的SQL代码预先编译好并保存在服务器端,用户只需在本机上输入要执行的存储过程名称和必要的数据就可以直接调用执行存储过程完成行管的操作。这样。既减少了网络传输流量,又能保证应用程序的运行性能,同时也防止了未经授权者想截获SQL代码的行为。

2、什么是存储过程

存储过程是SQL语句和控制语句的预编译集合,保存在数据库中,可由应用程序调用执行,而且允许用户声明变量,逻辑控制语句及其他强大的编程功能。

存储过程可包含逻辑控制语句和数据操作语句,可以接收参数、输出参数、返回单个或多个结果集及返回值。

1)使用存储过程的优点:

存储过程的优点如下:

  • 模块化程序设计;

  • 执行速度快、效率高;

  • 减少网络流量;

  • 具有良好的安全性;

2)存储过程分为以下两类

  • 系统存储过程;
  • 用户自定义的存储过程;

四、系统存储过程

SQL Server提供系统存储过程,它们是一组预编译的T-SQL语句。系统存储过程提供了管理数据库和更新表的机制,并充当从系统表中检索信息的快捷方式。

通过配置SQL Server,可以生成对象、用户、权限的信息和定义,这些信息和定义存储在系统表中。每个数据库分别有一个包含配置信息的系统表集,用户数据库的系统表是在创建数据库时自动创建的,用户可以通过系统存储过程访问和更新系统表。

1、常用的系统存储过程

SQL Server的系统存储过程的名称以“sp-”开头,并存放在Resource数据库中。系统管理员拥有这些存储过程的使用权限。可以在任何数据库中运行系统存储过程,但执行的结果会反映在当前数据库中。
SQL Server query optimization and transaction processing

示例如下:

<!--显示数据库-->
exec sp_databases;
<!--显示某个数据库对象的信息-->
exec sp_help A;
<!--显示所有数据库的信息-->
exec sp_helpdb;
<!--更改数据库名字-->
exec sp_renamedb 'xsh','benet';
<!--显示products表的约束-->
exec sp_helpconstraint products;
<!--显示products表的索引-->
exec sp_helpindex products;

上面示例的输出结果集较多,在此不一一列举,请自行逐句运行,查看相应的输出结果。

2、常用的扩展存储过程

根据系统存储过程的不同作用,系统存储过程可以分为不同类。扩展存储过程是SQL Server提供的各类系统存储过程中的一类。

扩展存储过程允许使用其他编程语言(如 C##语言)创建外部存储过程,为数据库用户提供从SQL Server实例到外部程序的接口,以便进行各种维护活动。它通常以“xp_”开头,以DLL形式单独存在。

一个常用的扩展存储过程为xp_cmdshell,它可以完成DOS命令下的一些操作,如创建文件夹、列出文件夹列表等,其语法如下:
开启系统cmdshell功能

<!--开启系统cmdshell功能-->
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'xp_cmdshell',1
reconfigure;
<!--创建目录-->
exec xp_cmdshell 'mkdir D:\test', no_output;

SQL Server query optimization and transaction processing

<!--显示创建目录-->
exec xp_cmdshell 'dir d:\';

SQL Server query optimization and transaction processing

五、触发器

触发器是一种特殊类型的存储过程,当表中的数据发生更新时将自动调用,以响应INSERT、UPDATE或DELETE语句。

1、什么是触发器

1)触发器的概念

触发器是在对表进行插入、更新或删除操作时自动执行的存储过程。触发器通常用于强制业务规则,是一种高级约束,可以定义比用CHECK约束更为复杂的约束,可以行发杂的SQL语句(如IF/WHILE/CASE),可引用其他表中的列。触发器主要是通过事件进行触发而被执行的,而存储过程可以通过存储过程名称而被直接调用。当对某一表进行修改,,如UPDATE、INSERT和DELETE这些操作时,SQL Server会自动执行触发器所定义的SQL语句,从而确保对数据的处理必须符合由这些SQL语句所定义的规则。由此触发器可分为以下几种:

  • INSERT触发器:当向表中插入数据时触发,自动执行触发器定义的SQL语句;
  • UPDATE触发器:当更新表中某列、多列时触发,自动执行触发器所定义的SQL语句;
  • DELETE触发器:当删除表中记录时触发,自动执行触发器定义的SQL语句。

2)deleted表和insertd表

SQL Server query optimization and transaction processing

2、触发器的作用

触发器的主要作用是,实现由主键和外键所不能保证的复杂的参照完整性和数据的一致性。

3、触发器的功能

  • 强化约束;
  • 跟踪变化;
  • 级联运行;

4、如何创建触发器

使用T-SQL语句创建触发器的语法如下:

CREATE TRIGGER trigger_name
ON table_name
        [WITH ENCRYPTION]
        FOR { [DELETE, INSERT, UPDATE] }
            AS SQL语句

SQL Server query optimization and transaction processing

创建触发器时需要注意以下问题:

  • CREATE TRIGGER必须是批处理中的第一条语句,并且只能应用到一个表中;

  • 触发器只能在当前的数据库中创建,不过触发器可以引用当前数据库的外部对象;

  • 在同一条CREATE TRIGGER语句中,可以为多种用户操作(如INSERT和UPDATE)定义相同的触发器操作;

六、事务

1、什么是事务

事务是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这一组数据库命令要么都执行,要么都不执行。因此,事务是一个不可分隔的工作逻辑单元,在数据库系统上执行并发操作时,事务是作为最小的控制单元来使用的,它特别适用于多用户同时操作的数据库系统。

事务是作为单个逻辑工作单元执行的一系列操作。一个逻辑工作单元必须有四个属性,即原子性、一致性、隔离性及持久性,这些特性通常简称为ACID。

1)原子性

事务是一个完整的操作。事务的各元素是不可分的(原子的)。事务中的所有元素必须作为一个整体提交或回滚。如果事务中的任何元素失败、则整个事务将失败。

2)一致性

当事务完成时,数据必须处于一致状态。也就是说,在事务开始之前,数据库中存储的数据处于一致状态。在正在进行的事务中,数据可能处于不一致的状态,如数据可能有部分修改。然而,当事务成功完成时,数据必须再次回到已知的一致状态。通过事务对数据所做的修改不能损坏数据,或者说事务不能使数据存储处于不稳定的状态。

3)隔离性

对数据进行修改的所有并发事务使彼此隔离的,这表明事务必须是独立的,它不应以任何方式依赖于或影响其他事务。修改数据的事务可以在另一个使用相同数据的事务开始之前访问这些数据,或者在另一个使用相同的数据,则直到该事务成功提交之后,对数据的修改才能生效。

4)持久性

事务的持久性指不管系统是否发生了故障,事务处理的结果都是永久的。

2、执行事务的语法

1)开始事务语法如下:

BEGIN TRANSACTION

2)提交事务语法如下:

COMMIT TRANSACTION

3)回滚(撤销)事务语法如下:

ROLLBACK TRANSACTION

BEGIN TRANSACTION语句后面的SQL语句对数据库的更新操作都将记录在事务日志中,直至遇到ROLLBACK TRANSACTION语句或COMMIT TRANSACTION语句。如果事务中某一操作失败且执行ROLLBACK TRANSACTION语句,那么在BEGIN TRANSACTION语句之后所有更新的数据都能回滚到事务开始前的状态。如果事务中的所有操作都全部正确完成,并且使用COMMIT TRANSACTION语句向数据库提交更新数据,那么这时候的数据又处在新的一致状态。

七、锁

1、什么是锁

A plurality of users can simultaneously manipulate data in the same database, data inconsistencies can occur. That is, if there is no lock and multiple users simultaneously access a database, when transaction data simultaneously using the same problem may occur. These problems include missing, updates, dirty reads, non-repeatable read and read hallucinations. Database locking is to solve the above problems.

2, the lock mode

  • Shared lock (S locks): for reading resource locks applied;

  • Exclusive lock (X lock): not compatible with any other locks, including other exclusive lock, an exclusive lock to modify data;

  • Update lock (U lock): U lock can be seen as a combination of S and X locks locks for updating data, you first need to find the data to be updated when you update the data, then we can understand the S lock on the data being sought. When you find the need to modify the data needs to be modified on the X lock on the resource. SQL Server to avoid deadlocks by U-lock;

3, see the lock method

  • Use sys.dm_tran_locks dynamic management view;

  • Use profiler to capture the lock information;

4, formed condition Deadlock

The nature of the deadlock is a stalemate, the body is composed of a plurality of resource contention caused. It is to be understood in SQL Server deadlock can refer to the following figure:
SQL Server query optimization and transaction processing

Deadlock four necessary conditions required are as follows:

  • Mutually exclusive conditions;

  • Request and wait condition;

  • Not deprivation;

  • Loop wait condition;

5, deadlock prevention

Deadlock prevention of damage is the number one and four essential conditions, a deadlock, it can not form a common method as follows:

  • Destruction of mutually exclusive conditions;

  • Destruction request and wait conditions;

  • Destroy not deprivation;

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14156658/2463956