簡體   English   中英

SQL中的慢速選擇查詢,其中表僅具有200萬條記錄

[英]Slow Select query in SQL with table having 2 million records only

如果有可能,我需要您的寶貴建議,我有一個表跟蹤歷史記錄(下表的腳本)。 它有大約。 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毫秒。”

隨附客戶統計資料

**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

表中的行數要少得多,僅為220萬,因此它應該呈現得非常快。

讀取220萬行並不是什么大不了的事,但是將它們發送給客戶端並“渲染”它們可能會非常昂貴,尤其是對於具有如此多列的表而言。 為什么要向客戶端發送這么多行和列?

無論如何,要優化查詢處理,請嘗試將此表存儲為群集的Columnstore而不是未壓縮的堆。 例如:

create clustered columnstore index cci_trackingsHistory on [trackingsHistory]

這都會使表變小得多,從而減少了讀取表的IO。 但這不會使通過網絡或客戶端處理發送行的速度更快。

另一個更好的設計是使PK集群化,因為您以PK順序請求行。 對於非群集PK,SQL必須對所有行進行排序,或者對每行進行書簽查找。

兩件事可以加快您的查詢速度:

  1. Id上創建聚簇索引,因此查詢不會完成排序,該表將按該列已經存儲的順序進行存儲(因此,按索引列的排序會快很多,在本例中為Id )。

  2. bookedin_date上創建非聚集索引,它將加快按該列(在WHERE子句中)的過濾。

在SQL Server中,頁面大小為8 KB。 即使您的排序規則是ASCII,表的每一行消耗的容量也超過該容量。 這意味着即使您創建了良好的索引並提高了where子句的性能,SQL‌ Server在從葉節點提取數據方面也存在延遲。 另一方面,當表中的列過多時,您很可能需要重新設計它。
您必須采取的任何解決方案:
1)為您的查詢創建一個索引,您需要的所有列都將其附加為“包含”列。 2)將您的表分成2個或更多表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM