Worth PostgreSQL. It has a table with a little more than 300 thousand lines.
If I execute such a request in pgAdmin, then it is executed in a fraction of a second (~ 300ms):
SELECT * FROM public."Movies" WHERE vote_count > 100 ORDER BY vote_average DESC LIMIT 10; If you perform a similar request in an ASP.Net Core application, the page is loaded for almost a minute (~ 50c), but if you do not use the database, but simply give the finished collection ala new Movie[] { ... } , then the download is almost instant.
//в классе контроллера private IMovieRepository repository; public IActionResult Index() { return View(repository.Movies .Where(m => m.VoteCount > 100) .OrderByDescending(m => m.VoteAverage) .Take(10)); } //класс для работы с данными public class EFMovieRepository : IMovieRepository { private TMDbContext context; public EFMovieRepository(TMDbContext context) { this.context = context; } public IEnumerable<Movie> Movies => context.Movies; } //класс контекста public class TMDbContext : DbContext { public TMDbContext() : base() { } public TMDbContext(DbContextOptions<TMDbContext> options) : base(options) { } public DbSet<Movie> Movies { get; set; } } //конфигурация в классе Startup public Startup(IHostingEnvironment environment) { Configuration = new ConfigurationBuilder() .SetBasePath(environment.ContentRootPath) .AddJsonFile("appsettings.json") .Build(); } public void ConfigureServices(IServiceCollection services) { services.AddDbContext<TMDbContext>(option => option.UseNpgsql( Configuration["Data:Movies:ConnectionString"])); services.AddTransient<IMovieRepository, EFMovieRepository>(); services.AddMvc(); } I use Npgsql.EntityFrameworkCore.PostgreSQL
What is the problem? Maybe I have something wrong with the configuration or maybe the request should be made more correctly? Why is the SQL query in pgAdmin so many times faster than the query through the provider?
UPD: Changed method code to:
public string Index() { var startTime = DateTime.Now; var movies = repository.Movies .Where(m => m.VoteCount > 100) .OrderByDescending(m => m.VoteAverage) .Take(10).ToList(); return (DateTime.Now - startTime).TotalSeconds.ToString(); } Returns an average of 17 seconds.
SELECT "m"."id", <...>, "m"."vote_average", "m"."vote_count" FROM "Movies" AS "m"lineSELECT "m"."id", <...>, "m"."vote_average", "m"."vote_count" FROM "Movies" AS "m"is it at every request the whole database asks for it? - MrModest