Query on freshly inserted Table Entities on Azure storage do not return all entities

Patrick Wellink 20 Reputation points
2024-10-04T21:02:14.9933333+00:00

After inserting a 1000 table entities, the first read gives Less entities. I have a very simple program

        public static async Task RunTest(TableClient client)
        {
            List<TableEntity> tableEntities = new List<TableEntity>();

            // Create 1000 table entities 
            for (int i = 0; i < 1000; i++)
            {
                TableEntity entity = new TableEntity();
                entity.PartitionKey = $"{new Random().Next(1, 10)}";
                entity.RowKey = $"{Guid.NewGuid()}";
                tableEntities.Add(entity);
            }

            Parallel.ForEach(tableEntities, async entity =>
            {
                await client.UpsertEntityAsync(entity);
            });

            for (int i = 0; i < 10; i++)
            {
                List<TableEntity> results = new();
                var asyncdata = client.QueryAsync<TableEntity>(maxPerPage: 200).ConfigureAwait(false);
                await foreach (TableEntity page in asyncdata)
                {
                    results.Add(page);
                }
                Console.WriteLine($"Read entities :{results.Count}");    
            }
        }

I would expect 1000 table entities when reading them. But i get less entities than i inserted. Below is some output from the program.

Read entities :999
Read entities :1000
Read entities :1000
Read entities :1000
Read entities :1000
Read entities :1000
Read entities :1000
Read entities :1000
Read entities :1000
Read entities :1000

The behaviour is pretty consistent, when i run it for a second time:

Read entities :1999
Read entities :2000
Read entities :2000
Read entities :2000
Read entities :2000
Read entities :2000
Read entities :2000
Read entities :2000
Read entities :2000
Read entities :2000

This time there are 1000 new entities added but again not all are read. Sometimes i miss 1 record, or 4, the number differs from time to time.

Azure Table Storage
Azure Table Storage
An Azure service that stores structured NoSQL data in the cloud.
171 questions
{count} votes

Accepted answer
  1. TP 99,291 Reputation points
    2024-10-20T08:54:55.7166667+00:00

    Hi Patrick,

    You are seeing incorrect results because you are querying the table before your code has finished inserting all of the entities. Please test using modified code below:

    public static async Task RunTest(TableClient client)
    {
        List<TableEntity> tableEntities = new List<TableEntity>();
    
        // Create 1000 table entities 
        for (int i = 0; i < 1000; i++)
        {
            TableEntity entity = new TableEntity();
            entity.PartitionKey = $"{new Random().Next(1, 10)}";
            entity.RowKey = $"{Guid.NewGuid()}";
            tableEntities.Add(entity);
        }
    
        await Parallel.ForEachAsync(tableEntities, async (entity, token) =>
            {
                await client.UpsertEntityAsync(entity);
            });
    
        for (int i = 0; i < 10; i++)
        {
            List<TableEntity> results = new();
            var asyncdata = client.QueryAsync<TableEntity>(maxPerPage: 200).ConfigureAwait(false);
            await foreach (TableEntity page in asyncdata)
            {
                results.Add(page);
            }
            Console.WriteLine($"Read entities :{results.Count}");
        }
    }
    
    

    Please click Accept Answer and upvote if the above was helpful.

    Thanks.

    -TP

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.