I Am Confused about Why It is Called as A Phantom Query.. eg Assume These 3 Queries:-
Query 1
SELECT * FROM users
WHERE salary BETWEEN 10000 AND 300000;
return 2 records.
Query 2
INSERT INTO users VALUES ( 3, 'Bob', 270000 );
COMMIT;
Query 1 again
SELECT * FROM users
WHERE salary BETWEEN 10000 AND 30000;
return 3 records.
See It seems to me normal . I don’t understand why they are called ‘Phantom read’ .all transactions are happening in different time , so we are getting the latest data always. initially there were 2 records , Later on , 1 record inserted ..so when we run Query once again, we get the updated data i.e 3 records. So Why It is Called a Phantom Query.?
On a purely academic level a transaction should see a consistent state of the database based on the data when the transaction started. It should only see its own changes and nothing else.
As long as transaction 1 hasn’t finished seeing committed data is considered a phantom read because it sees rows that were not there when the transaction started.
Because this strong academic approach is not always needed, different isolation levels have been introduced. Depending on the requirements the developer can choose which level is needed based on the business rules that are implement.
Sometimes you want phantom reads then you use
READ COMMITTEDisolation level (which is the default for most DBMS), sometimes you don’t want them – then you useSERIALIZABLEisolation level