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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:17:55+00:00 2026-05-25T13:17:55+00:00

I have to count how many times a number from table2 occurs between the

  • 0

I have to count how many times a number from table2 occurs between the number in range table2.a and table2.b

i.e. we wanna know how many times we have this : a < start < b

I ran the following query :

UPDATE table2
 SET occurrence =  
       (SELECT COUNT(*) FROM table1 WHERE start BETWEEN table2.a AND table2.b);



table2 
ID   a   b    occurrence
1    1   10
2    1   20
3    1   25
4    2   30


table1 
ID start col1 col2 col3
1   1
2   7
3  10
4  21
5  25
6  27
7  30

table2 as

  • 3 indexes on a, b and occurrence
  • 1567 rows (so we will SELECT COUNT(*) over table2 1567 times..)
  • ID column as PK

table1 as

  • 1 index on start
  • 42,000,000 rows
  • Column start was “ordered by column start”
  • ID column as PK

==> it took 2.5hours to do 2/3 of it. I need to speed this up… any suggestions ? 🙂

  • 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-25T13:17:56+00:00Added an answer on May 25, 2026 at 1:17 pm

    You could try to add the id column to the index on table 1:

    CREATE INDEX start_index ON table1 (start,id);
    

    And rewrite the query to

    UPDATE table2
     SET occurrence =  
           (SELECT COUNT(id) FROM table1 WHERE start BETWEEN table2.a AND table2.b);
    

    This is called “covering index”: http://www.simple-talk.com/sql/learn-sql-server/using-covering-indexes-to-improve-query-performance/

    -> The whole query on table 1 can be served through the data in the index -> no additional page lookup for the actual record.

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

Sidebar

Related Questions

I want to count how many times i built on VS.NET . I have
I have a few text files and I'd like to count how many times
i have this SELECT COUNT(1) cnt, a.auther_id FROM `posts` a LEFT JOIN users u
This is a particular problem that I have come across many times, but I
I have read many article about this one. I want to hear from you.
I have a function (frequency) which that counts how many times each distinct value
I have 3 tables namely: Town; Village; Farmers...and I need to COUNT how many
I have variable @count of datatype int.I am setting values to this @count. I
I have to count the number of overlapping dimers (AA, AG, AC, AT, GA,
For example I have an app that counts how many time Camera button was

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.