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 ? 🙂
You could try to add the id column to the index on table 1:
And rewrite the query to
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.