测试sql语句

------------------------- 1. 建库 ------------------------- 
02.USE [master] 
03.GO 
04.if exists(select * from sysdatabases where name=‘DB_TEST_MEMTB‘)   
05.    DROP DATABASE DB_TEST_MEMTB   
06.go   
07.CREATE DATABASE [DB_TEST_MEMTB] 
08. ON  PRIMARY 
09.( 
10.    NAME = N‘DB_TEST_MEMTB_DATA‘, 
11.    FILENAME = N‘e:\db\test\DB_TEST_MEMTB_DATA.mdf‘, 
12.    SIZE = 512000KB, 
13.    MAXSIZE = UNLIMITED, 
14.    FILEGROWTH = 1024KB 
15.),  
16.--下面的文件就是数据流文件了 
17.FILEGROUP [MEM_DIR] CONTAINS MEMORY_OPTIMIZED_DATA  DEFAULT 
18.( 
19.    NAME = N‘DB_TEST_MEMTB_DIR‘, 
20.    FILENAME =N‘e:\db\test\DB_TEST_MEMTB_DIR‘, 
21.    MAXSIZE = UNLIMITED 
22.) 
23.LOG ON  
24.( 
25.    NAME = N‘DB_TEST_MEMTB_LOG‘, 
26.    FILENAME = N‘e:\db\test\DB_TEST_MEMTB_LOG.ldf‘, 
27.    SIZE = 512000KB, 
28.    MAXSIZE = 2048GB, 
29.    FILEGROWTH = 1024KB 
30.) 
31.GO 
32.------------------------- 2. 建表和本地编译存储过程 ------------------------- 
33.USE DB_TEST_MEMTB 
34.GO 
35.-- 1. 建立普通磁盘表 
36.IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[t_disk]‘) AND type in (N‘U‘)) 
37.    DROP TABLE [dbo].[t_disk] 
38.GO 
39.create table [t_disk] 
40.( 
41.    c1 int not null primary key, 
42.    c2 nchar(48) not null 
43.) 
44.go 
45.-- 2. 建立内存优化表 (后面的测试不使用本地编译存储过程) 
46.IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[t_mem]‘) AND type in (N‘U‘)) 
47.    DROP TABLE [dbo].[t_mem] 
48.GO 
49.create table [t_mem] 
50.( 
51.    c1 int not null primary key nonclustered hash with (bucket_count=10000000), 
52.    c2 nchar(48) not null 
53.) with (memory_optimized=on, durability = schema_and_data) 
54.GO 
55.-- 3.0 建立内存优化表 (后面的测试使用本地编译存储过程 NATIVE_COMPILATION) 
56.IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[t_mem_nc]‘) AND type in (N‘U‘)) 
57.    DROP TABLE [dbo].t_mem_nc 
58.GO 
59.create table t_mem_nc 
60.( 
61.    c1 int not null primary key nonclustered hash with (bucket_count=10000000), 
62.    c2 nchar(48) not null 
63.) with (memory_optimized=on, durability = schema_and_data) 
64.GO 
65.-- 3.1 本地编译存储过程_insert 
66.IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[Proc_t_mem_nc_Insert]‘) AND type in (N‘P‘, N‘PC‘)) 
67.    DROP PROCEDURE [dbo].[Proc_t_mem_nc_Insert] 
68.GO 
69.CREATE PROCEDURE [Proc_t_mem_nc_Insert]  
70.       @rowcount int, 
71.       @c nchar(48) 
72.WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER 
73.AS  
74. BEGIN ATOMIC  
75. WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N‘us_english‘) 
76.       declare @i int = 1 
77. 
78.       while @i <= @rowcount 
79.       begin 
80.                 INSERT INTO [dbo].t_mem_nc values (@i, @c) 
81.                 set @i += 1 
82.       end 
83.END 
84.GO 
85.-- 3.2 本地编译存储过程_delete 
86.IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[Proc_t_mem_nc_delete]‘) AND type in (N‘P‘, N‘PC‘)) 
87.    DROP PROCEDURE [dbo].[Proc_t_mem_nc_delete] 
88.GO 
89.CREATE PROCEDURE [Proc_t_mem_nc_delete] 
90.    @rowcount int 
91.WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER 
92.AS  
93. BEGIN ATOMIC  
94. WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N‘us_english‘) 
95.       DECLARE @i INT = 1 
96.       while @i<[email protected] 
97.       begin 
98.                 DELETE FROM dbo.t_mem_nc WHERE [email protected] 
99.                 set @i += 1 
100.       end 
101.END 
102.GO 
103.-- 3.3 本地编译存储过程_update 
104.IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[Proc_t_mem_nc_update]‘) AND type in (N‘P‘, N‘PC‘)) 
105.    DROP PROCEDURE [dbo].[Proc_t_mem_nc_update] 
106.GO 
107.CREATE PROCEDURE [Proc_t_mem_nc_update] 
108.    @rowcount INT, 
109.    @c nchar(48) 
110.WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER 
111.AS  
112. BEGIN ATOMIC  
113. WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N‘us_english‘) 
114.       DECLARE @i INT = 1 
115.       while @i<[email protected] 
116.       begin 
117.                 UPDATE dbo.t_mem_nc SET [email protected] WHERE [email protected] 
118.                 set @i += 1 
119.       end 
120.END 
121.GO 
122.-- 3.4 本地编译存储过程_select 
123.IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N‘[dbo].[Proc_t_mem_nc_select]‘) AND type in (N‘P‘, N‘PC‘)) 
124.    DROP PROCEDURE [dbo].[Proc_t_mem_nc_select] 
125.GO 
126.CREATE PROCEDURE [Proc_t_mem_nc_select] 
127.WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER 
128.AS  
129. BEGIN ATOMIC  
130. WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N‘us_english‘) 
131.    SELECT c1,c2 FROM dbo.t_mem_nc 
132.END 
133.GO 
134.------------------------- 3. 效率评测 ------------------------- 
135.DECLARE @i INT=1,@iMax INT = 1000000      --最大一百万条记录 
136.DECLARE @v NCHAR(48)=‘123456789012345678901234567890123456789012345678‘ 
137.DECLARE @t DATETIME2 = sysdatetime() 
138.--3.1 insert  
139.-- 
140.set nocount on 
141.while @i<[email protected] 
142.begin 
143.    insert into t_disk (c1,c2) values(@i, @v) 
144.    set @i+=1 
145.end 
146.select ‘insert (t_disk): ‘+ convert(varchar(10),  datediff(ms, @t, sysdatetime())) 
147.-- 
148.set @i=1 
149.set @t=SYSDATETIME() 
150.while @i<[email protected] 
151.begin 
152.    insert into t_mem (c1,c2) values(@i, @v) 
153.    set @i+=1 
154.end 
155.select ‘insert (t_mem): ‘+ convert(varchar(10),  datediff(ms, @t, sysdatetime())) 
156.-- 
157.set @t=SYSDATETIME() 
158.exec [Proc_t_mem_nc_Insert]  
159.       @[email protected], 
160.       @[email protected] 
161.select ‘insert (t_mem_nc): ‘+ convert(varchar(10),  datediff(ms, @t, sysdatetime())) 
162.--结果: 
163.--insert (t_disk):   242111 
164.--insert (t_mem):    221358 
165.--insert (t_mem_nc):   2147 
166. 
167.--insert (t_disk):   243174 
168.--insert (t_mem):    223465 
169.--insert (t_mem_nc):   2214 
170. 
171.--3.2 update 
172.--时间较长,故分段执行另设变量 
173.DECLARE @u INT=1,@uMax INT = 1000000      --最大一百万条记录 
174.DECLARE @uv NCHAR(48)=‘1234567890123456789012345678901234567890abcdefgh‘ 
175.DECLARE @ut DATETIME2 = sysdatetime() 
176.set nocount on 
177.while @u<[email protected] 
178.begin 
179.    update t_disk set [email protected] where [email protected] 
180.    set @u+=1 
181.end 
182.select ‘update (t_disk): ‘+ convert(varchar(10),  datediff(ms, @ut, sysdatetime())) 
183.-- 
184.set @u=1 
185.set @ut=SYSDATETIME() 
186.while @u<[email protected] 
187.begin 
188.    update t_mem set [email protected] where [email protected] 
189.    set @u+=1 
190.end 
191.select ‘update (t_mem): ‘+ convert(varchar(10),  datediff(ms, @ut, sysdatetime())) 
192.-- 
193.set @ut=SYSDATETIME() 
194.exec [Proc_t_mem_nc_Update]  
195.       @[email protected], 
196.       @[email protected] 
197.select ‘update (t_mem_nc): ‘+ convert(varchar(10),  datediff(ms, @ut, sysdatetime())) 
198.--update (t_disk):    199369 
199.--update (t_mem):     368297 
200.--update (t_mem_nc):    3715 
201. 
202.--update (t_disk):    203251 
203.--update (t_mem):     355356 
204.--update (t_mem_nc):    3732 
205. 
206.--3.3 select  
207.DECLARE @st DATETIME2 = sysdatetime() 
208.set nocount on 
209.-- 
210.select c1,c2 from t_disk 
211.select ‘select (t_disk): ‘+ convert(varchar(10),  datediff(ms, @st, sysdatetime())) 
212.set @st=SYSDATETIME() 
213.select c1,c2 from t_mem 
214.select ‘select (t_mem): ‘+ convert(varchar(10),  datediff(ms, @st, sysdatetime())) 
215.set @st=SYSDATETIME() 
216.exec Proc_t_mem_nc_select 
217.select ‘select (t_mem_nc): ‘+ convert(varchar(10),  datediff(ms, @st, sysdatetime())) 
218.--select (t_disk):   8934 
219.--select (t_mem):    9278 
220.--select (t_mem_nc): 8889 
221. 
222.--select (t_disk):   8861 
223.--select (t_mem):    9978 
224.--select (t_mem_nc): 9108 
225. 
226.--3.4 delete 
227.--时间较长,故分段执行另设变量 
228.DECLARE @d INT=1,@dMax INT = 1000000      --最大一百万条记录 
229.DECLARE @dt DATETIME2 = sysdatetime() 
230.set nocount on 
231.while @d<[email protected] 
232.begin 
233.    delete from t_disk where [email protected] 
234.    set @d+=1 
235.end 
236.select ‘delete (t_disk): ‘+ convert(varchar(10),  datediff(ms, @dt, sysdatetime())) 
237.-- 
238.set @d=1 
239.set @dt=SYSDATETIME() 
240.while @d<[email protected] 
241.begin 
242.    delete from t_mem where [email protected] 
243.    set @d+=1 
244.end 
245.select ‘delete (t_mem): ‘+ convert(varchar(10),  datediff(ms, @dt, sysdatetime())) 
246.-- 
247.set @dt=SYSDATETIME() 
248.exec [dbo].[Proc_t_mem_nc_delete] @[email protected] 
249.select ‘delete (t_mem_nc): ‘+ convert(varchar(10),  datediff(ms, @dt, sysdatetime())) 
250. 
251.--delete (t_disk): 199438 
252.--delete (t_mem):  342959 
253.--delete (t_mem_nc):  928 
254. 
255.--delete (t_disk): 199637 
256.--delete (t_mem):  341771 
257.--delete (t_mem_nc):  803

时间: 2024-10-05 14:56:00

测试sql语句的相关文章

如何测试sql语句性能,提高执行效率

有时候我们经常为我们的sql语句执行效率低下发愁,反复优化后,可还是得不到提高 那么你就用这条语句找出你sql到底是在哪里慢了 示例: SET STATISTICS io ON        SET STATISTICS time ON        go         ---你要测试的sql语句         select top 100 * from TBL_Cot_RecStaticList        go        SET STATISTICS profile OFF    

查看SQL语句执行时间与测试SQL语句性能

查看SQL语句执行时间与测试SQL语句性能 写程序的人,往往需要分析所写的SQL语句是否够优化.是否能提升执行效率,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了. 通过设置STATISTICS我们可以查看执行SQL时的系统情况.选项有PROFILE,IO ,TIME.介绍如下: SET STATISTICS PROFILE ON:显示分析.编译和执行查询所需的时间(以毫秒为单位). SET STATISTICS IO ON:报告与语句内引用的每个表的扫描数

数据库之测试sql语句效率

在写sql语句的时候发现,对于想要的结果通常有好多中实现方法.当面对这些实现方法的时候要如何选择才是相对来说最优的呢?这就引出了这篇博客的主题,如何测试sql语句的效率 下面介绍几种sql语句测试效率的方法,大多数是从网上查找的,然后再一一测试了一下.最后我自己做了一个重新的整理. 1. 测试sql语句执行时间的方法,获得查询前后时间差 declare @begin_date datetime declare @end_date datetime select @begin_date = get

如何在python文件中测试sql语句

在manage.py的同级目录下新建一个run.py import os if __name__ == '__main__': #加载Django项目的配置信息 os.environ.setdefault("DJANGO_SETTINGS_MODULE","myblog.settings") #导入Django,并启动Django项目 import django django.setup() #导入相应的models from person import model

loadrunner测试sql语句性能

最初的想法是是想通过录制在SQL Server2008的操作来着的,无奈试了即便都录不到查询的sql语句,网上查资料全是关于SQL 2000的(这部分有经验的欢迎指教). 于是只能通过直接调用loadrunner本身的function来实现sql语句的性能测试.主要用到的是lr_db_connect和lr_db_executeSQLStatement两个函数,具体的用法这里不做解释,请自行查看loadrunner的帮助说明. 下面进入正题.在函数lr_db_connect的帮助说明中有句“Imp

查看sql语句执行时间/测试sql语句性能

写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了. 通过设置STATISTICS我们可以查看执行SQL时的系统情况.选项有PROFILE,IO ,TIME.介绍如下: SET STATISTICS PROFILE ON:显示分析.编译和执行查询所需的时间(以毫秒为单位). SET STATISTICS IO ON:报告与语句内引用的每个表的扫描数.逻辑读取数(在高速缓存中访问的页数)和物理读取数(访问磁

SQL: 查看语句执行时间--测试sql语句性能

SET STATISTICS PROFILE ON:显示分析.编译和执行查询所需的时间(以毫秒为单位). SET STATISTICS IO ON:报告与语句内引用的每个表的扫描数.逻辑读取数(在高速缓存中访问的页数)和物理读取数(访问磁盘的次数)有关的信息. SET STATISTICS TIME ON:显示每个查询执行后的结果集,代表查询执行的配置文件. 使用方法:打开SQL SERVER 查询分析器,输入以下语句: set statistics profile on set statist

测试sql 语句执行时间

#-*-coding:utf-8-*- import sysimport osimport timeimport tracebackimport MySQLdb as sqldbfrom log import tracelog query_list = ["select count(*) from T_FieldInfo,T_FamTableDescInfo,T_DataEleDesc;",             ] def getconn():    host = '10.1.81

sql语句的效率测试

1.测试sql语句执行时间的方法,获得时间差 declare @end_date datetime select @begin_date = getdate() --要执行的sql语句 select @end_date = getdate() select datediff(ms,@begin_date,@end_date) as '用时/毫秒' 2.下面这种方法比较全面,将执行每个语句时采取的步骤作为行集返回,通过层次结构树的形式展示出来,输出中包含的列: SET STATISTICS io