0

I have this query using EntityFramework Core:

                if (_context.Users.Where(u => u.EmailAddress == user.EmailAddress).Count() > 0)
                       ...

After not accessing the db for a few minutes, it takes more than 14 seconds.

Right after this, I save a new row to the table, this takes 1.5 seconds.

Does it make sense when using db.t3.small?

The table has about 10 rows.

Rony Tesler
  • 1,207
  • 15
  • 25
  • 1
    I would say that for starters instead of `Count` you should use `Any`. Though in case of 10 records it should not be a big difference. – Guru Stron Apr 14 '21 at 18:09
  • Not sure that Any is faster: https://stackoverflow.com/questions/305092/which-method-performs-better-any-vs-count-0 – Rony Tesler Apr 14 '21 at 18:18
  • 1
    It will actually depend on generated SQL and amount of data, but from my experience for small datasets it does not matter and for large ones usually it is faster to use `Any` then filter whole dataset. – Guru Stron Apr 14 '21 at 18:23
  • Also where is the db instance located compared to the code? Few times I had performance issues on my local machine when it was too far from the database. Also usually it has not affected such "small" queries. – Guru Stron Apr 14 '21 at 18:34
  • @GuruStron The server is lambda functions, in the same region as the db.. querying the db from my localhost using SSMS is always fast. – Rony Tesler Apr 14 '21 at 18:41
  • Make sure auto_close isn't set for the database. By default databases created with some versions of SQL Server Express have it set to true. – Brian Apr 14 '21 at 19:47
  • @Brian SELECT DATABASEPROPERTY('myDB','IsAutoClose') returned 0. – Rony Tesler Apr 14 '21 at 19:54

0 Answers0