Solved

SQL Server Query Help

Posted on 2011-02-16
7
295 Views
Last Modified: 2012-08-13
Hi Experts,

I have a customersales table and I am using the below query to find out the maximum salesid for each customer.

SELECT MAX(SaleID),CustomerID
FROM CustomerSales
GROUP BY CustomerID

It takes a very high cpu and time. CustomerSales table has 4 million records.

Can you please guide me if there a better and efficient way to write this query.

Thanks,
0
Comment
Question by:rocky_lotus_newbie
7 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 125 total points
ID: 34910778
Do you have SaleID and CustomerID fileds indexed
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 250 total points
ID: 34910781
The query is as good as it gets.
However, you will find it runs MUCH faster, if you add an index on customerid, saleid

e.g.

create index ix_customersales_cs on CustomerSales (customerid, saleid)
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 250 total points
ID: 34910784
Not separately, mind you, but one index involving both fields.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Assisted Solution

by:spmamidi
spmamidi earned 125 total points
ID: 34910862


I hope you would be having Clustered Index if not there use the following query

CREATE UNIQUE CLUSTERED INDEX Sales_ID_Index ON CustomerSales (CustomerSalesID);
GO

Next, build three similar nonclustered indexes:
-- The first index is on a single, nonunique column
CREATE INDEX SalesOrderID_Index1 ON CustomerSales(SaleID);
GO
-- The second index is composite and explicitly adds the clustered key column
CREATE  INDEX SalesOrderID_Index2 ON CustomerSales(SaleID, CustomerID);
GO
-- The third index adds the clustered index key as an INCLUDE column
CREATE  INDEX Sales_DetailID_Index3 ON CustomerSales(SaleID);
      INCLUDE (CustomerID);
GO

Hopefully you would get good performance results.

Shiva Mamidi



Shiva Mamidi
0
 
LVL 4

Author Comment

by:rocky_lotus_newbie
ID: 34912061
Thanks for the Inputs, I will try these indexes and see if it performs better.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34912892
Shiva - you want to create 3 indexes ALL on the same [first] column on a SINGLE table??
0
 
LVL 1

Expert Comment

by:spmamidi
ID: 34916172

Yes - There is a typo for Creating third Index.

Here is the correct version - CREATE  INDEX Sales_OrderId_Index3 ON CustomerSales(SaleID) INCLUDE (CustomerID);

Shiva Mamidi
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question