-1

Given tables setup like this

    tableA {
    id = 1
    name = 'bob'
    id = 2
    name = 'sally'
    id = 3
    name = 'sue'
    }
    tableB {
    id = 1
    name = 'bob'
    }

If I run this command, it returns ID's 2 and 3, as wanted:

select id 
from tableA a 
left join tableB b using (id) 
where id not in (select id from tableB) 
order by id 
limit 10;

But it is a very large table so I need to use offset. When I try the following command, no results are returned.

select id 
from tableA a 
left join tableB b using (id) 
where id not in (select id from tableB) 
order by id 
limit 10 offset 10;

Is there a way to use offset when a join is being used?

11
  • You can use OFFSET with a JOIN, this is not the issue. The reason why you're getting no results after adding OFFSET 10 in the specific query you provided is because of how filtering is done after the JOIN and WHERE clause, especially combined with NOT IN and the effect of your dataset size. Commented Apr 3 at 15:22
  • I probably don't understand your answer but I tried moving the limit and offset to the subquery and it failed with "This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'". Can you provide an example of how it can work? Commented Apr 3 at 15:35
  • How many rows returned in case "very large table" without offset and limit? Commented Apr 3 at 15:53
  • About 67,000. A lot of processing is done on the data so the script times out. Commented Apr 3 at 16:12
  • Do you have indexes on tableA(id) and tableB(id)? Commented Apr 3 at 16:47

1 Answer 1

1

The process environment is not visible, but I will suggest this approach. You process rows by batches (limit 10).

With parameter @startId=0

select id 
from tableA a 
where id>@startId
  and   id not in (select id from tableB) 
order by id 
limit 10;

After processing batch, take @startId as max(id) in processed batch and put as parameter value no next query - without offset.

select id 
from tableA a 
where id>@startId
  and   id not in (select id from tableB) 
order by id 
limit 10;

Example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.