Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 431767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:00:27+00:00 2026-05-12T20:00:27+00:00

The following simple query takes a very long time (several minutes) to execute. I

  • 0

The following simple query takes a very long time (several minutes) to execute.

I have an index:

create index IX on [fctWMAUA] (SourceSystemKey, AsAtDateKey)
SELECT MAX([t0].[AsAtDateKey]) AS [Date], [t0].[SourceSystemKey] AS [SourceSystem]
FROM [fctWMAUA] (NOLOCK) AS [t0]
WHERE SourceSystemKey in (1,2,3,4,5,6,7,8,9)
GROUP BY [t0].[SourceSystemKey]

The statistics are as follows:

  • logical reads 1827978
  • physical reads 1113
  • read aheads 1806459

Taking that exact same query and reformatting it as follows gives me these statistics:

  • logical reads 36
  • physical reads 0
  • read aheads 0

It takes 31ms to execute.

SELECT MAX([t0].[AsAtDateKey]) AS [Date], [t0].[SourceSystemKey] AS [SourceSystem]
 FROM [fctWMAUA] (NOLOCK) AS [t0]
 WHERE SourceSystemKey = 1
 GROUP BY [t0].[SourceSystemKey]
UNION
 SELECT MAX([t0].[AsAtDateKey]) AS [Date], [t0].[SourceSystemKey] AS [SourceSystem]
 FROM [fctWMAUA] (NOLOCK) AS [t0]
 WHERE SourceSystemKey = 2
 GROUP BY [t0].[SourceSystemKey]
UNION
 SELECT MAX([t0].[AsAtDateKey]) AS [Date], [t0].[SourceSystemKey] AS [SourceSystem]
 FROM [fctWMAUA] (NOLOCK) AS [t0]
 WHERE SourceSystemKey = 3
 GROUP BY [t0].[SourceSystemKey]
/* AND SO ON TO 9 */

How do I make an index that does the group by quickly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:00:27+00:00Added an answer on May 12, 2026 at 8:00 pm

    Try to tell SQL Server to use the index:

    ...
    FROM [fctWMAUA] (NOLOCK, INDEX(IX)) AS [t0]
    ...
    

    Make sure the statistics for the table are up to date:

    UPDATE STATISTICS [fctWMAUA]
    

    For better answers, turn on the showplan for both queries:

    SET SHOWPLAN_TEXT ON
    

    and add the results to your question.

    You can also write the query without a GROUP BY. For example, you can use an exclusive LEFT JOIN excluding rows with older dates:

    select cur.SourceSystemKey, cur.date
    from fctWMAUA cur
    left join fctWMAUA next
        on next.SourceSystemKey = next.SourceSystemKey
        and next.date > cur.date
    where next.SourceSystemKey is null
    and cur.SourceSystemKey in (1,2,3,4,5,6,7,8,9)
    

    This can be surprisingly fast, but I don’t think it could beat the UNION.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets suppose that I have the following simple query var q = from p
I have following rather simple query select count(*) from tbl t1, tbl t2 For
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following simple MySQL query: SELECT SQL_NO_CACHE mainID FROM tableName WHERE otherID3=19
I have a very simple query (three where conditions; two equals, one between) from
I have following simple query in ASP.NET MVC action method. IEnumerable<Company> query = unitOfWork.CompanyRepository.dbSet
I want to translate following simple sql query into Linq to NHibernate: SELECT NewsId
The following simple code pattern is very common in graphics programming. It creates an
Suppose I have the following simple struct: struct Vector3 { double x; double y;
I have a following simple code: def get(): return [lambda: i for i in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.