简体   繁体   English

SQL Server Compact(SqlCe 4.0)GROUP BY查询优化

[英]Sql Server Compact (SqlCe 4.0) GROUP BY Query Optimization

I have a table Invoices 我有一张表格Invoices

Structure of Invoices Invoices结构

CREATE TABLE Invoices (
  Id               int NOT NULL IDENTITY (1, 1),
  Depot            nvarchar(100) NOT NULL,
  InvoiceNo        nvarchar(100) NOT NULL,
  InvoiceDate      datetime NOT NULL,
  Licencee         nvarchar(255) NOT NULL,
  Outlet           nvarchar(1024) NOT NULL,
  SerialNo         int NOT NULL,
  ProductName      nvarchar(500) NOT NULL,
  Size             int NOT NULL,
  [Case]           int NOT NULL,
  BeverageSegment  nvarchar(100),
  /* Keys */
  PRIMARY KEY (Id)
)
GO

CREATE INDEX Invoices_BeverageSegment
  ON Invoices
  (BeverageSegment)
GO

CREATE INDEX Invoices_InvoiceId
  ON Invoices
  (InvoiceNo)
GO

I have to query for Total Sales for a particular Depot , for a Product on Date , i have approx 1,35,850 in the Table, the Query is taking 47 minute. 我必须查询特定软件Depot Total Sales,对于DateProduct ,我在表中大约有1,35,850 ,查询需要47分钟。

I do not have much expertise in database system, i have checked for Query Optimization for SqlCe but none of them helps in this condition. 我在数据库系统方面没有太多专业知识,我已经检查了SqlCe的查询优化,但是在这种情况下它们都没有帮助。

My query : 我的查询:

SELECT 
  Depot, 
  InvoiceDate, 
  ProductName, 
  Size, 
  SUM([Case]) as Cases 
FROM Invoices 
GROUP BY 
  Depot, 
  InvoiceDate, 
  ProductName, 
  Size

Creating a non clustered index on all the four columns Depot, InvoiceDate, ProductName, Size will increase the performance. 在所有四个列Depot,InvoiceDate,ProductName,Size上创建非聚集索引将提高性能。

   Create NonClustered Index Index_Name
   on 
   Invoices ( Depot, InvoiceDate,  ProductName, Size)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM