我刚刚执行了一个非常快的查询,SQL Profiler 说它使用了 30.000 次读取。现在我正在查看此页面:https ://cloud.google.com/compute/docs/disks/performance并看到标准永久性磁盘具有 3000 次读取 IOPS。
这是否意味着我的查询在那里运行时需要 10 秒?或者说SQL reads
和read IOPS
意味着完全不同的事情?
我刚刚执行了一个非常快的查询,SQL Profiler 说它使用了 30.000 次读取。现在我正在查看此页面:https ://cloud.google.com/compute/docs/disks/performance并看到标准永久性磁盘具有 3000 次读取 IOPS。
这是否意味着我的查询在那里运行时需要 10 秒?或者说SQL reads
和read IOPS
意味着完全不同的事情?
正如我所读到的,大多数时候索引搜索比索引扫描更受欢迎,我正在尝试一些东西。
我有一个查询在使用索引扫描时进行 993 次读取(使用 SQL Profiler 检查)。使用索引查找时,它需要 44.347 次读取。感觉有些不对劲,或者我不明白。
这是索引扫描的查询:
select t5.Id as t5Id
from table1 t1
left join table2 t2 on t2.Table1Id = t1.Id
left join table3 t3 on t3.Table2Id = t2.Id
left join table4 t4 on t4.Table3Id = t3.Id
left join table5 t5 on t5.Table4Id = t4.Id
这是索引查找的查询:
select t5.Id as t5Id
from table1 t1
left join table2 t2 on t2.Table1Id = t1.Id
left join table3 t3 on t3.Table2Id = t2.Id
left join table4 t4 on t4.Table3Id = t3.Id
left join table5 t5 WITH (FORCESEEK) on t5.Table4Id = t4.Id
这些表格简单明了。最后,我用一些虚拟数据填充它们,因此可以轻松复制。
CREATE TABLE [dbo].[table1](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_table1] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[table2](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Table1Id] [bigint] NOT NULL,
CONSTRAINT [PK_table2] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[table2] WITH CHECK
ADD CONSTRAINT [FK_table2_table1Id] FOREIGN KEY([table1Id])
REFERENCES [dbo].[table1] ([Id])
GO
ALTER TABLE [dbo].[table2]
CHECK CONSTRAINT [FK_table2_table1Id]
GO
CREATE NONCLUSTERED INDEX [IdxTable2_FKTable1Id] ON [dbo].[table2]
(
[Table1Id] ASC
)
INCLUDE ( [Id]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE TABLE [dbo].[table3](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Table2Id] [bigint] NOT NULL,
CONSTRAINT [PK_table3] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[table3] WITH CHECK
ADD CONSTRAINT [FK_table3_table2Id] FOREIGN KEY([table2Id])
REFERENCES [dbo].[table2] ([Id])
GO
ALTER TABLE [dbo].[table3]
CHECK CONSTRAINT [FK_table3_table2Id]
GO
CREATE NONCLUSTERED INDEX [IdxTable3_FKTable2Id] ON [dbo].[table3]
(
[Table2Id] ASC
)
INCLUDE ( [Id]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE TABLE [dbo].[table4](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Table3Id] [bigint] NOT NULL,
CONSTRAINT [PK_table4] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[table4] WITH CHECK
ADD CONSTRAINT [FK_table4_table3Id] FOREIGN KEY([table3Id])
REFERENCES [dbo].[table4] ([Id])
GO
ALTER TABLE [dbo].[table4]
CHECK CONSTRAINT [FK_table4_table3Id]
GO
CREATE NONCLUSTERED INDEX [IdxTable4_FKTable3Id] ON [dbo].[table4]
(
[Table3Id] ASC
)
INCLUDE ( [Id]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE TABLE [dbo].[table5](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Table4Id] [bigint] NOT NULL,
[Description] [nvarchar](2000) NOT NULL,
CONSTRAINT [PK_table5] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[table5] WITH CHECK
ADD CONSTRAINT [FK_table5_table4Id] FOREIGN KEY([table4Id])
REFERENCES [dbo].[table5] ([Id])
GO
ALTER TABLE [dbo].[table5]
CHECK CONSTRAINT [FK_table5_table4Id]
GO
CREATE NONCLUSTERED INDEX [IdxTable5_FKTable4Id] ON [dbo].[table5]
(
[Table4Id] ASC
)
INCLUDE ( [Id], [Description]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
set nocount on
DECLARE @i INT = 0;
DECLARE @j INT = 0;
DECLARE @k INT = 0;
DECLARE @l INT = 10;
DECLARE @m INT = 0;
declare @table1Id bigint
declare @table2Id bigint
declare @table3Id bigint
declare @table4Id bigint
begin tran
WHILE @i < 10
BEGIN
INSERT INTO [dbo].[table1] ([Name]) VALUES (cast(@i as nvarchar(10)))
SELECT @table1Id = SCOPE_IDENTITY()
WHILE @j < 10
BEGIN
INSERT INTO [dbo].[table2] ([Table1Id]) VALUES (@table1Id)
SELECT @table2Id = SCOPE_IDENTITY()
WHILE @k < 10
BEGIN
INSERT INTO [dbo].[table3] ([Table2Id]) VALUES (@table2Id)
SELECT @table3Id = SCOPE_IDENTITY()
WHILE @l > 0
BEGIN
INSERT INTO [dbo].[table4] ([Table3Id]) VALUES (@table3Id)
SELECT @table4Id = SCOPE_IDENTITY()
WHILE @m < 10
BEGIN
INSERT INTO [dbo].[table5] ([Table4Id], [Description]) VALUES (@table4Id, 'Not so long description')
SET @m = @m + 1;
END;
SET @m = 0;
SET @l = @l - 1;
END;
SET @l = 10;
SET @k = @k + 1;
END;
SET @k = 0;
SET @j = @j + 1;
END;
SET @j = 0;
SET @i = @i + 1;
END;
commit
SQL Server 是否并行执行查询?换句话说,如果我运行一个需要 10 秒执行的繁重查询,同时启动另一个需要 10 秒的繁重查询,那么第二个查询实际上会在 10 秒后启动,还是它们会同时启动?