When you issue SELECT * FROM [MyTable] , and your table has 10Gb on a system with 2Gb of RAM, the database should not read all 10 Gb at once in memory. The selection will begin scanning data, starting with the first data . To do this, he only needs this page (which is 8 KB) in memory, so it reads the page and consumes 8 KB of RAM. When scanning this page, it displays the result that you see as a result set. As soon as this is done with this page, she needs the next page in order for it to be read in memory. It scans the entries in it and produces output for your result. Then the next page, then the next page. The key point is that once done with the page, it is no longer needed. As he continues to add these 8kb pages to RAM, they will eventually add up and consume all of the free RAM. At this point, SQL will free up old, unused pages in RAM and thus free up space for new ones. He will continue to do this until all 10Gb of your table has been read.
If there are two users who read a table of 5 GB each, everything works the same way. Each user request scans only one page at a time, and as they advance and continue to read the pages, they fill up RAM. When all available RAM is used, SQL will begin to discard old pages from RAM to make room for new ones.
In the real world, things are becoming more attractive due to considerations such as reading ahead .
And as a note, you should never scan 10Gb of data. Your application should always request only the data that it needs, and the data should be quickly restored using the index to avoid such a large scan, which should check the entire table.
source share