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 8449649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:46:06+00:00 2026-06-10T10:46:06+00:00

I ran a query on about 1,300,000 records in one table. It takes certain

  • 0

I ran a query on about 1,300,000 records in one table. It takes certain records that meet some WHERE conditions and inserts them into another table. It first clears out the target table entirely.

The time taken to complete the query gets drastically better with each Execute:

1st: 5 minutes, 3 seconds
2nd: 2 minutes, 43 seconds
3rd: 12 seconds
4th: 3 seconds

I’m not doing anything other than just hitting Execute. My query looks like this (somewhat abbreviated for length purposes):

DELETE FROM dbo.ConsolidatedLogs --clear target table

DECLARE @ClientID int

DECLARE c CURSOR FOR SELECT ClientID FROM dbo.Clients
OPEN c

FETCH NEXT FROM c INTO @ClientID --foreach LogID
WHILE @@FETCH_STATUS = 0
BEGIN

   INSERT INTO dbo.ConsolidatedLogs
        (col1, col2)
        SELECT col1, col2
        FROM dbo.CompleteLogsRaw
        WHERE col3 = true AND
              ClientID = @ClientID

   FETCH NEXT FROM c INTO @ClientID

END
CLOSE c
DEALLOCATE c

How/why does this happen? What is SQL Server doing exactly to make this possible?

This query is going to be run as an SQL Server Agent job, once every 3 hours. Will it take the full 5 minutes every time, or will it be shorter because the job is only running this one query, even though it’s got a 3 hour delay?

  • 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-06-10T10:46:08+00:00Added an answer on June 10, 2026 at 10:46 am

    If identical queries get faster with each run, there is an outstanding chance that things are being cached. So, where can things be cached?

    • SQL Server Query Plan Cache
    • SQL Server’s Query Cache
    • Operating System IO Buffers
    • Hard Disk Controller Cache
    • Hard Disk On-Disk Cache

    You can clear the SQL Server Query Cache between runs to see what the impact of that cache is

    How can I clear the SQL Server query cache?

    SQL Server will use whatever RAM is dedicated to it to keep things that it accesses frequently in RAM rather than on disk. The more you run the same query, the more the data that would normally be found on disk is likely to reside in RAM instead.

    The OS level and hardware-level caches are easiest to reset by performing a reboot if you wish to see whether they are contributing to the improving results.

    If you publish the query plan that SQL Server is using for each execution of your query, a more detailed diagnostics would be possible.

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

Sidebar

Related Questions

I ran a query to copy 18 million records from one table to another.
I have the following query that I ran on my database to remove some
Here's the query (the largest table has about 40,000 rows) SELECT Course.CourseID, Course.Description, UserCourse.UserID,
I just ran into a SQL query with about 5 different columns in the
I recently ran into an issue where a query was causing a full table
I have a query that runs in about 2-4 minutes on production but runs
I am trying to speed up a long running query that I have (takes
I have a query that's running slow (in a loop of about 100 it
I want to pull back results from one table that match ALL specified values
I ran this query in my database : SELECT DB_NAME(dbid) as DBName, COUNT(dbid) as

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.