Knowledge points taking sql server database as an example (3)

Knowledge points taking sql server database as an example (3)

1: The role of the index: the processing speed of data query data ---- the standard of success or failure of the application system The
most common optimization method
2: What is the index?
Index: Quickly locate the data we are looking for without having to scan the entire table.
– thus speeding up our queries. - The disadvantages of improving the performance index of the system
: Occupying the storage space, the more indexes are not as better. The index does not always improve the system performance
3: The purpose of the index: can query data more quickly and efficiently, reduce the system's response time indexes are divided
into aggregating index non -gathering index
primary keys
. At most consistent with the physical order
, there can only be no non -gathering index: noClusterEd logical order and physical order are inconsistent with the physical order
.

If you need to build an index on multiple columns, build a composite index on those columns.
Column: small data type, the access speed is very fast;
the index is invisible, but if you create an index, its advantages are obvious when querying large data;

– Default non-clustered index;
– Aggregated primary key index
create clustered index PK_UserInfos
on UserInfos(UserId)
with
(
drop_existing=off – delete the original first, create a new off without deleting the original prompt an error, the index already exists

)
– unique non-clustered index
create unique nonclustered index uq_UserInfos
on UserInfos(UserName)
with
(
pad_index=on,
fillfactor=50, – specifies the percentage of data on each index page to the size of the index page when creating the index
ignore_dup_key=on
)

– Read-write ratio: 100:1 100
– Read less than write: 50-70
– Read and write half: 80–90


– Composite index: create unique nonclustered index Index_UserInfos
on UserInfos(UserName,DeptId)
with
(
drop_existing=off
) for indexes created on multiple columns

View: The virtual table is defined by one or more tables through query and saves the query definition, but does not actually include data.

The difference with the table: where the table stores data, the view stores query statements (except for indexed views, which are materialized)

Role: Simplified query increases data confidentiality and security is guaranteed

Disadvantages: It just simplifies the query and increases the maintenance cost than improving the query speed

Classification: standard view (storage query definition, no data storage)
indexed view (reified, indexed, significantly improved query performance, query that aggregates many rows, not suitable for frequently updating basic data sets) partitioned view (distinguished data of a set of member tables horizontally connected between one or more servers
)

Create a standard view
create view view name
as
T-SQL statement
Create an index view
create view view name with schemabinding
as
select UserId,UserName,Age from dbo.UserInfos
go

Create partition view
create view view name
as
select * from Teat
union all
select * from Test3
go

Neither the standard view nor the partitioned view is allowed to modify the data in the table, which will affect the basic table
index. If the view is deleted, the corresponding basic table data will also be deleted, so use it with caution;

Stored procedure: A collection of one or a group of SQL statements to complete a specific function. After being compiled,
it is stored in the database on the server side, and stored procedures can be used to speed up the execution of SQL statements. Call the name, pass in parameters, and execute to complete specific functions.

Category: System stored procedures: In the master database, other databases can be called directly, and there is no need to add the database name in front.
When creating a database, these system stored procedures will be automatically created in the new database.

      自定义存储过程 :用户自己创建,特定的功能创建。可以传入参数,也可以有返回值。

It can be just one operation, or it can contain many.
Execution: execute/exec Stored procedure name parameter list (multiple parameters, separated by commas)
Advantages: 1. Improve the versatility and portability of the application. Call it multiple times without having to rewrite it, and maintenance personnel can modify it at any time
2. Can manage database permissions more effectively
3. Improve the speed of SQL execution
4. Reduce the burden on the server

Cons: Maintaining it exclusively, takes up database space.
Syntax:
create proc/procedure stored procedure name
@userId int,
@userName varchar(50)
as
begin
T-SQL statement
end
go

1: Script creation stored procedure syntax
create proc/procedure stored procedure name
@userId int,
@userName varchar(50)
as
begin
T-SQL statement
end
go

2: Create a simple stored procedure
– Create a parameterless stored procedure
create proc SearchUserInfo
as
begin
select UserId, UserName, Age from UserInfos
end

–Call to execute
exec SearchUserInfo
–Delete stored procedure
drop proc SearchUserInfo

– Create a stored procedure with parameters
create proc AddUserInfo
@UserName varchar(50),
@UserPwd varchar(50),
@Age int
as
begin
declare @time datetime
set @time=GETDATE()
insert into UserInfos(UserName,UserPwd,Age)
values(@UserName,@UserPwd,@Age);
select * from UserInfos
end
go

–call
exec AddUserInfo 'lingli','2344','2323'

Guess you like

Origin blog.csdn.net/m0_46454966/article/details/127467371