SQL中的慢速选择查询,其中表仅具有200万条记录

时间:2018-07-29 16:45:55

标签: sql sql-server database query-performance

如果可能的话,我需要您的宝贵建议,我有一个表跟踪历史记录(下表的脚本)。它有大约。 220万条记录。但是,当我获取所有记录时,表呈现速度太慢,并且获取所有记录大约需要4-5分钟。我认为表中的行数远远少于 220万,因此它应呈现得非常快。

如果您可以为此添加有价值的输入,那将非常有帮助。

选择记录应该非常快,并且由于表记录较少并且数据库大小仅为12 GB,因此可以在几秒钟内呈现。

100,000条记录的统计IO和时间- “(受影响的100000行) 表“ trackingsHistory”。扫描计数1,逻辑读21036,物理读0,预读0,lob逻辑读0,lob物理读0,lob预读0。

(受影响的1行)

SQL Server执行时间:  CPU时间= 2657毫秒,经过的时间= 9737毫秒。”

Client Statistics attached

**Details Below**

**Exact query** - SELECT * FROM TRACKINGSHISTORY WHERE bookedin_date BETWEEN '2018-01-01 07:03:58.700' AND '2018-12-31 07:03:58.700' ORDER BY ID DESC

**Table Structure**
/****** Object:  Table [dbo].[trackingsHistory]    Script Date: 07/25/2018 13:22:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[trackingsHistory](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [customer_id] [int] NULL,
      [master_tracking_no] [nvarchar](30) NULL,
      [carrier] [nvarchar](50) NOT NULL,
      [voided] [bit] NOT NULL,
      [origin] [nvarchar](250) NULL,
      [destination] [nvarchar](250) NOT NULL,
      [service_type] [nvarchar](50) NULL,
      [bookedin_date] [datetime] NOT NULL,
      [package_type] [nvarchar](50) NOT NULL,
      [pieces] [tinyint] NOT NULL,
      [total_weight] [decimal](18, 2) NOT NULL,
      [weight_unit] [int] NULL,
      [total_dim_weight] [decimal](18, 2) NOT NULL,
      [scheduled] [nvarchar](50) NULL,
      [quoted_base] [decimal](18, 2) NOT NULL,
      [quoted_fuel] [decimal](18, 2) NOT NULL,
      [quoted_vat] [decimal](18, 2) NOT NULL,
      [quoted_total] [decimal](18, 2) NOT NULL,
      [quoted_insurance] [decimal](18, 2) NOT NULL,
      [quoted_additionalhandlingsurcharge] [decimal](18, 2) NOT NULL,
      [quoted_administrationcharge] [decimal](18, 2) NOT NULL,
      [quoted_reseller_markup_percentage] [decimal](18, 2) NOT NULL,
      [quoted_reseller_markup] [decimal](18, 2) NOT NULL,
      [insurance] [nvarchar](50) NULL,
      [shipper_company] [nvarchar](50) NULL,
      [shipper_contact] [nvarchar](50) NULL,
      [shipper_vat_no] [nvarchar](50) NULL,
      [terms_of_trade] [nvarchar](50) NULL,
      [destination_country] [nvarchar](50) NULL,
      [receiver_company] [nvarchar](50) NULL,
      [receiver_contact] [nvarchar](50) NULL,
      [receiver_vat_no] [nvarchar](50) NULL,
      [shipment_contents] [nvarchar](2000) NULL,
      [reason_for_export] [nvarchar](50) NULL,
      [commercial_invoice] [bit] NOT NULL,
      [shipment_reference] [nvarchar](50) NULL,
      [currency_sign] [nvarchar](10) NULL,
      [dispatch_id] [int] NULL,
      [payment_transaction_id] [nvarchar](100) NULL,
      [service_id] [int] NULL,
      [importTracking_id] [int] NULL,
      [origin_email] [nvarchar](50) NULL,
      [origin_phone] [nvarchar](50) NULL,
      [dest_email] [nvarchar](50) NULL,
      [dest_phone] [nvarchar](50) NULL,
      [quoted_extareasurcharge] [decimal](18, 2) NOT NULL,
      [quoted_extarea_fuel_surcharge] [decimal](18, 2) NOT NULL,
      [payment_method_used] [nvarchar](10) NULL,
      [paymentmode] [nvarchar](10) NULL,
      [WORef] [nvarchar](50) NULL,
      [quoted_residential_charge] [decimal](18, 2) NOT NULL,
      [quoted_residential_fuel_surcharge] [decimal](18, 2) NOT NULL,
      [transportation_payer] [int] NULL,
      [subscription_type] [tinyint] NULL,
      [quoted_markup] [decimal](18, 2) NOT NULL,
      [shipment_status] [nvarchar](500) NULL,
      [shipment_status_error] [nvarchar](500) NULL,
      [is_shipment_delivered] [bit] NULL,
      [is_thermal_print] [bit] NULL,
      [watch_status] [bit] NULL,
      [account_no] [nvarchar](10) NULL,
      [quoted_largepackagesurcharge] [decimal](18, 2) NOT NULL,
      [quoted_overmax_size] [decimal](18, 2) NOT NULL,
      [quoted_overmax_weight] [decimal](18, 2) NOT NULL,
      [origin_country] [nvarchar](125) NULL,
      [quoted_largepackage_fuel_surcharge] [decimal](18, 2) NOT NULL,
      [quoted_premium_timed_charge] [decimal](18, 2) NOT NULL,
      [quoted_restricted_destination] [decimal](18, 2) NOT NULL,
      [quoted_exporter_validation] [decimal](18, 2) NOT NULL,
      [quoted_elevated_risk] [decimal](18, 2) NOT NULL,
      [shipment_type] [tinyint] NOT NULL,
      [quoted_misc_charge] [decimal](18, 2) NOT NULL,
      [last_updated_date] [datetime] NULL,
      [reseller_id] [int] NULL,
      [sales_person_id] [int] NULL,
      [multi_site_id] [int] NULL,
      [acs_group_id] [int] NULL,
      [franchise_acs_group_id] [int] NULL,
      [sk_commission] [decimal](18, 2) NULL,
CONSTRAINT [PK_tracking] PRIMARY KEY CLUSTERED 
(
      [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 100) ON [PRIMARY]
)

GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'True value will add shipment to watch list and false will remove from watch list.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'trackingsHistory', @level2type=N'COLUMN',@level2name=N'watch_status'
GO

ALTER TABLE [dbo].[trackingsHistory]  WITH CHECK ADD  CONSTRAINT [FK_trackingsHistory_customers] FOREIGN KEY([customer_id])
REFERENCES [dbo].[customers] ([id])
GO

ALTER TABLE [dbo].[trackingsHistory] CHECK CONSTRAINT [FK_trackingsHistory_customers]
GO

ALTER TABLE [dbo].[trackingsHistory]  WITH CHECK ADD  CONSTRAINT [FK_trackingsHistory_dispatches] FOREIGN KEY([dispatch_id])
REFERENCES [dbo].[dispatches] ([id])
GO

ALTER TABLE [dbo].[trackingsHistory] CHECK CONSTRAINT [FK_trackingsHistory_dispatches]
GO

ALTER TABLE [dbo].[trackingsHistory]  WITH CHECK ADD  CONSTRAINT [FK_trackingsHistory_importTrackings] FOREIGN KEY([importTracking_id])
REFERENCES [dbo].[importTrackings] ([id])
GO

ALTER TABLE [dbo].[trackingsHistory] CHECK CONSTRAINT [FK_trackingsHistory_importTrackings]
GO

ALTER TABLE [dbo].[trackingsHistory]  WITH CHECK ADD  CONSTRAINT [FK_trackingsHistory_multisite] FOREIGN KEY([multi_site_id])
REFERENCES [dbo].[customers] ([id])
GO

ALTER TABLE [dbo].[trackingsHistory] CHECK CONSTRAINT [FK_trackingsHistory_multisite]
GO

ALTER TABLE [dbo].[trackingsHistory]  WITH CHECK ADD  CONSTRAINT [FK_trackingsHistory_reseller] FOREIGN KEY([reseller_id])
REFERENCES [dbo].[customers] ([id])
GO

ALTER TABLE [dbo].[trackingsHistory] CHECK CONSTRAINT [FK_trackingsHistory_reseller]
GO

ALTER TABLE [dbo].[trackingsHistory]  WITH CHECK ADD  CONSTRAINT [FK_trackingsHistory_sales] FOREIGN KEY([sales_person_id])
REFERENCES [dbo].[customers] ([id])
GO

ALTER TABLE [dbo].[trackingsHistory] CHECK CONSTRAINT [FK_trackingsHistory_sales]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_base]  DEFAULT ((0)) FOR [quoted_base]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_fuel]  DEFAULT ((0)) FOR [quoted_fuel]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_vat]  DEFAULT ((0)) FOR [quoted_vat]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_total]  DEFAULT ((0)) FOR [quoted_total]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_insurance]  DEFAULT ((0)) FOR [quoted_insurance]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_additionalhandlingsurcharge]  DEFAULT ((0)) FOR [quoted_additionalhandlingsurcharge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_additionalhandlingsurcharge1]  DEFAULT ((0)) FOR [quoted_administrationcharge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_reseller_markup_percentage]  DEFAULT ((0)) FOR [quoted_reseller_markup_percentage]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_reseller_markup]  DEFAULT ((0)) FOR [quoted_reseller_markup]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF__trackings__quote__14270015]  DEFAULT ((0)) FOR [quoted_extareasurcharge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_extarea_fuel_surcharge]  DEFAULT ((0)) FOR [quoted_extarea_fuel_surcharge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF__trackings__quote__17036CC0]  DEFAULT ((0)) FOR [quoted_residential_charge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_residential_fuel_surcharge]  DEFAULT ((0)) FOR [quoted_residential_fuel_surcharge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_markup]  DEFAULT ((0)) FOR [quoted_markup]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF__trackings__quote__55FFB06A]  DEFAULT ((0)) FOR [quoted_largepackagesurcharge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF__trackings__quote__25E688F4]  DEFAULT ((0)) FOR [quoted_overmax_size]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF__trackings__quote__26DAAD2D]  DEFAULT ((0)) FOR [quoted_overmax_weight]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF__trackings__quote__5F3F01E1]  DEFAULT ((0)) FOR [quoted_largepackage_fuel_surcharge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_trackingsHistory_quoted_premium_timed_charge]  DEFAULT ((0)) FOR [quoted_premium_timed_charge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF__trackings__quote__22DFFF17]  DEFAULT ((0)) FOR [quoted_restricted_destination]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF__trackings__quote__23D42350]  DEFAULT ((0)) FOR [quoted_exporter_validation]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF__trackings__quote__6A669BCA]  DEFAULT ((0.00)) FOR [quoted_elevated_risk]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  CONSTRAINT [DF_Constraint]  DEFAULT ((0)) FOR [shipment_type]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  DEFAULT ((0)) FOR [quoted_misc_charge]
GO

ALTER TABLE [dbo].[trackingsHistory] ADD  DEFAULT (getdate()) FOR [last_updated_date]
GO

3 个答案:

答案 0 :(得分:3)

  

表中的行数要少得多,只有220万,因此渲染速度应该非常快。

读取220万行并不是什么大问题,但是将它们发送给客户端并“呈现”它们可能会非常昂贵,尤其是对于具有如此多列的表而言。为什么要向客户端发送这么多行和列?

无论如何,要优化查询处理,请尝试将此表存储为群集的Columnstore而不是未压缩的堆。例如:

create clustered columnstore index cci_trackingsHistory on [trackingsHistory]

这都将使表变小,从而减少了读取表的IO。但这不会使通过网络或客户端处理发送行的速度更快。

另一种更好的设计是使PK集群化,因为您以PK顺序请求行。对于非集群PK,SQL必须要么对所有行进行排序,要么对每行进行书签查找。

答案 1 :(得分:1)

两件事可以加速您的查询:

  1. Id上创建聚簇索引,因此查询不会完成排序,该表将按该列的顺序存储(因此在索引列中,排序会快很多, Id)。

  2. bookedin_date上创建非聚集索引,它将加速该列(在WHERE子句中)的过滤。

答案 2 :(得分:0)

在SQL Server中,页面大小为8 KB。即使您的排序规则是ASCII,表的每一行消耗的容量也超过该容量。这意味着即使您创建了良好的索引并提高了where子句的性能,SQL‌ Server在从叶节点提取数据方面也存在延迟。 另一方面,当表中的列过多时,您很可能需要重新设计它。
您必须采取的解决方法:
1)为您的查询创建一个索引,您需要的所有列都将其附加为“包含”列。 2)将您的桌子分成2个或更多桌子