0

For instance, assume both table A and B have a 'CreatedAt' datetime column. If I use offset 10 rows fetch next 10 rows only, after a 'union all' operation, i.e. Select [columns] from A union all select [columns] from B, will the database executor actually query all records from both tables and then do the paging? Or will it query part of the records based on the result of ordering by the 'CreatedAt' column, until it finds the 10 rows within the page number?

Dale K
  • 25,246
  • 15
  • 42
  • 71
Nico
  • 497
  • 1
  • 4
  • 15
  • 1
    Does this answer your question? [SQL Server use union all and paging](https://stackoverflow.com/questions/42021533/sql-server-use-union-all-and-paging) – Harsh Gundecha Jan 19 '21 at 08:50
  • 3
    Learn to love execution plans and it would be fairly easy to find out for yourself. – Jeroen Mostert Jan 19 '21 at 08:50
  • I agree - the query plan decides what to do each time and it endeavours to do it the fastest way. However if it needs the data in both tables to work out what rows 10-20 ara then you can't avoid working through two tables – Nick.Mc Jan 19 '21 at 11:30
  • @HarshGundecha Nope, I was asking about the execution logic in the backend, not how to write the SQL. – Nico Jan 20 '21 at 11:03
  • @Nick.McDermaid I'm afraid so. – Nico Jan 20 '21 at 11:08

0 Answers0