Unidac
Unidac
Unidac
held @Sort VARCHAR ( 255 ), -- sort fields and rules, without adding
order by
AS
/* 查詢總記錄數*/
OVER(ORDER BY ' + @Sort + ') AS rownum, ' + @strFld + ' FROM ' +
@tab + ' where ' + @strWhere + ') AS Dwhere
exec (@strSql)
HOME
A universal stored procedure for millions of data
query paging operations
2018-04-20 / VIEWS: 29
I. Introduction
Recently, I have been busy with work, so I rarely write anything. The MongoDB series
of articles has to be delayed. There is no way, work first, no food, no food. It’s just a little bit of
time today, I’ve made some things about me recently and recorded them.
In the software industry, a slightly larger company, the amount of relevant data storage
may be very large, when we do the system, we will use the stored procedure for page display, as
to why the page display, I do not have to say more. Recently, I am working on a system. In order
to let the system support large data volume, I have done a lot of tests, mainly to test the pros and
cons of various paging algorithms. Finally, after many tests, I determined this stored
procedure. The test environment is:
hardware environment: CPU is 12 cores, memory is 8G size,
software environment: Visual Studio 2015, database is Sql Server
2008 R2, data volume is about 30 million
ORM environment: IBatis.Net
The test results are not mapped. If you want to test, you can operate it yourself. First of
all, if you want to query the ID primary key, you can display the paging data for up to 300-400
milliseconds. The first page or the last page shows that the time is similar. In addition, if it is a
query paging for other fields, an index must be created for the field, and the query speed can also
be completed in milliseconds. After many rounds of testing, the final stored procedure style is
determined. Of course, if there is no index page for the paged field of the query, the time will be
longer. 6 minutes, 10 minutes is possible. Since only the stored procedure is discussed today, the
others will not be discussed, such as sub-library, sub-table, horizontal split or vertical split.
Of course, this stored procedure also refers to the information of many seniors, only to
finalize. Modified its original bug, and some errors in the grammar hill. Other details have also
been fine-tuned to form this final version. If you have a better way of writing, you are welcome
to leave a message. Not much nonsense, the code is sent out today, the stored procedure has been
tested, no problem at all, you can rest assured to use, the code is as follows:
1 USE [XXXXXXXX .BbbbbbbbCcccccccc.Dataware ]
2 GO
5 GO
6 SET QUOTED_IDENTIFIER ON
7 GO
8 -- ============================================= =========
9 -- Author: <PatrickLiu>
12 --=============================================
14 (
17 @PageSize int = 20 , -- --
23 @EnabledDistinct bit = 0 , -- --
Whether to add the DISTINCT of the query field Default 0 does not add
/1 Add
26 )
27 AS
28 SET NOCOUNT ON
38
41 is
42 is IF @EnabledDistinct = 0
43 is the begin
46 end
47 else
48 begin
51 end
52
53 if @EnabledSort = 0
54 begin
57 end
58 else
59 begin
62 end
63
67 the begin
72 else
73 begin
77 End
78
81
83 if @RecordCount = 0
84 set @TemporaryCount = 1
85 else
87
88 -- Get the total number of pages
90
94
98
103 else
105
106 -- //*** Show pagination
110 begin
111 if @PageNumber = 1
113 else
114 begin
115 if @EnabledSort = 1
116 begin
120 end
121 else
122 begin
126 end
127 end
128 end
129 else
130 begin
135 else
136 if @EnabledSort = 1
137 begin
142 end
143 else
144 begin
150 End
151 End
152
156 begin
157 if @PageNumber = 1
161 begin
165 end
166 else
167 begin
171 end
172 end
173 else
174 begin
183 else
187 end
188 end
189
190 -- ----Return result-----
The above is the stored procedure, the code is very simple, there are comments, no
explanation.
. 5 {
. 6 /// <Summary>
8 /// </summary>
11 /// <summary>
13 /// </summary>
15
16 /// <summary>
18 /// </summary>
20
23 /// </summary>
25
26 /// <summary>
28 /// </summary>
29 public string SortFields { get ; set ; }
30
31 /// <summary>
35
36 /// <Summary>
40
41 /// <summary>
43 /// </summary>
45
46 /// <summary>
47 /// Whether to add the DISTINCT of the query field Default
0 does not add /1 Add
48 /// </summary>
50
51 /// <summary>
55
60 }
The above is the code of the entity class, very simple, it will not be explained.
Fourth, Sql Server call code
Here, I only demonstrate how to call this stored procedure in Sql Server, which is very
simple, everyone should know. Ok, that's it, the code is very simple, I want to call the stored
procedure, the call instance is as follows:
1 USE [ Xxxxxxx.Bbbbbb.Dataware ] GO //The name is commented out
8 @FieldList = N '
[ID],[Title],[Authors],[PeriodicalName],[PublicationDate],[VolumeNumbe
r] ' ,
9 @PageSize = 30 ,
10 @PageNumber = 50 ,
11 @SortFields = [ ID ] ,
15 @EnabledDistinct = 1 ,
20
22
23 GO
After the query, you can see the results of the first off, I will not map.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- /*-----Storage process paging processing Sun Wei 2005-03-28 created
-------*/
AS
SETNOCOUNT ON
if @Dist = 0
begin
end
else
begin
end
if @Sort=0
begin
end
else
begin
end
the begin
End
the else
the begin
SET @sqlTmp = + @fldName + From @tblName + where
@strCondition
end
if @Counts = 0
set @tmpCounts = 1
else
if @lastcount > 0
else
the begin
Begin
if @page = 1
else
begin
if @Sort=1
begin
end
else
begin
end
end
end
else
begin
else
if @Sort=1
begin
else
begin
than
than
than
begin
begin
if @page = 1
else if(@Sort=1)
begin
end
else
begin
end
end
else
begin
else if(@Sort=1)
else
end
end
-- print @strTmp
HOME
EXEC msdb.dbo.sp_add_job
@sql = N ' in the current database -- the job step content '
-- the general definition is Jobs processed using TSQL, here define
the Transact-SQL statement to be executed
EXEC msdb.dbo.sp_add_jobstep
@job_id = @jobid ,
@database_name = @dbname ,
@command = @sql
EXEC msdb..sp_add_jobschedule
@job_id = @jobid ,
EXEC msdb.dbo.sp_add_jobserver
@job_id = @jobid ,
EXEC msdb..sp_add_jobschedule
@job_id = @jobid ,
-- */
EXEC msdb.dbo.sp_add_jobschedule
@job_id = @jobid ,
@freq_type = 8 , -- weekly
-- */
EXEC msdb.dbo.sp_add_jobschedule
@job_id = @jobid ,
-- */
EXEC msdb.dbo.sp_add_jobschedule
@job_id = @jobid ,
the @name = N ' schedule name ' ,
@freq_type = 32 , - monthly
-- */
-- * /
EXEC msdb..sp_add_jobschedule
@job_id = @jobid ,
-- */
A generic stored procedure written by yourself:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
@freqInterval INT = 1 ,
- specify the number of days happen every time, here is one day.
month , here is the first day - at the time of the month, 1~7 =
Sunday to Saturday, 8 = day, 9 = Workday, 10=weekend
-- Daily frequency
- duration
AS
BEGIN
--定義創建作業
EXEC msdb.dbo.sp_add_job
@job_name = @jobname,
EXEC msdb.dbo.sp_add_jobstep
@job_id = @jobid ,
@step_name = N ' performed formula job, generating cost
breakdown ' ,
@database_name = @dbname ,
@command = @sql
IF @freqtype = 4
BEGIN
EXEC msdb.dbo.sp_add_jobschedule
@job_id = @jobid ,
END
IF @freqtype = 8
BEGIN
EXEC msdb.dbo.sp_add_jobschedule
@job_id = @jobid ,
END
IF @freqtype = 16
BEGIN
EXEC msdb.dbo.sp_add_jobschedule
@job_id = @jobid ,
END
IF @freqtype = 32
BEGIN
EXEC msdb.dbo.sp_add_jobschedule
@job_id = @jobid ,
@freq_relative_interval = @freq_relativeInterval , --
executed at the first relative time, the allowed values are 1, 2, 4, 8
represents the 1~4 relative time , 16 indicates the last relative time
END
EXEC msdb.dbo.sp_add_jobserver
@job_id = @jobid ,
END