Solved

SQL Server Query Help

Posted on 2011-02-16
7
297 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:
Ephraim Wangoya 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

685 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