I hope the title is somewhat clear.
Situation: Table with measurements, around 350.000.000 records are in here. Table with sensor id's, around 300 records are in here.
Now when I want specific measurements of a sensor, I take the sensor id and query the measurement table with that sensor id (and specific data or whatever). These queries run very fast (index on sensor id and some other columns).
However, it has been a long running project and I have no idea how much data is "old" (measurements that I cannot access, because the sensor id is not in my sensor table). Lets say:
measurements table
sensor id
1
2
3
4
5
sensor table
1
4
5
I want to select all id's from the measurement table, that are not in the sensor table. I normally do this with the following query:
SELECT sensor_id
FROM measurement_table
WHERE sensor_id NOT IN (
SELECT sensor_id
FROM sensor_table
WHERE sensor_id IS NOT NULL)
)
GROUP BY sensor_id
On a table that is 'small', say a couple thousands or even a million records, this is doable. However, when I run this query on my measurements table, it takes too long (cancelled it after nearly an hour).
Is there a way for me to find out what sensor id's are in the measurement table but not in the sensor table?
Edit:
SELECT COUNT(*)
FROM measurements_table
WHERE sensor_id IN (
SELECT sensor_id
FROM sensor_table
)
Does work (no group, count), gives:
334214244
Total in table:
337225071
So I want to find those 3010827 records by ID...