Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server Query Help

Posted on 2011-02-16
7
Medium Priority
?
306 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 500 total points
ID: 34910778
Do you have SaleID and CustomerID fileds indexed
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 1000 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 1000 total points
ID: 34910784
Not separately, mind you, but one index involving both fields.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 1

Assisted Solution

by:spmamidi
spmamidi earned 500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

926 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