0

Every query is taking 5 seconds to run.

I'm just trying to make simple querys to the database from my site and it's taking too long.

This is the query and how i'm measuring the time

var sw = new Stopwatch();
sw.Start();
var leg = db.Legislacaos.Where(x => x.leg_ativo).ToList();
sw.Stop();
ViewBag.sw = sw.Elapsed;

When I do the query in "phpMyadmin" SELECT * FROM 'legislacao' WHERE 'leg_ativo' =1 it takes no time at all but it takes 5 second per query in my site.

I have a page that has 4 querys that takes 20 seconds to load. The time is stable, I tried in tables with 20,100,300,1000 records(in different tables), all of them used the same amount of time. Also, tried to add "leg_ativo" as an index but there was no difference.

I don't know if it's necessary but some background code:

public class LegislacaosController: Controller
    {
        private Context db = new Context();

        public  ActionResult Create()
        {
             var sw = new Stopwatch();
             sw.Start();
             ViewBag.Leg =  db.Legislacaos.Where(x => x.leg_ativo).ToList();
             sw.Stop();
             ViewBag.sw = sw.Elapsed;
             return View();
        }
        //...
    }

Context-

public class  Context : DbContext
    {

        public Context() :base("smagnus_tester")
        {
            Configuration.ProxyCreationEnabled = false;
            Database.SetInitializer(new            
            DropCreateDatabaseIfModelChanges<Context>());
        }
        public DbSet<Legislacaos> Legislacaos{ get; set; }
        //...
    }

Model-

 public class Legislacao
    {
        [Key]
        public int leg_id { get; set; }

        [Required(ErrorMessage = "Preencha o campo Legislação")]
        [DisplayName("Legislação")]
        [StringLength(255)]
        public string leg_nome { get; set; }//name

        [Required(ErrorMessage = "Preencha o campo Resumo")]
        [DisplayName("Resumo")]
        [StringLength(255)]
        public string leg_resumo { get; set; }//resume

        [DisplayName("Anexo")]
        public string leg_anexo { get; set; }//file

        [DefaultValue(true)]
        [DisplayName("Ativar?")]
        public bool leg_ativo { get; set; }//active

        public virtual ICollection<Legislacao_Risco> Legislacao_Riscos { get; set; }
    }

Database config

Microsoft-IIS/7.5
Client version MySQL: 5.0.51a
Leon700
  • 59
  • 6
  • 3
    comment out the query and return fake static data. use site. is it still taking 5 seconds? Then bottleneck is not in MySQL. L for Logic! :) – KolA Aug 22 '19 at 19:02
  • 2
    Believe it or not the performance problems seams to be in Linq, it looks like you are using C# reflection API which can be slow.. -> [Use reflection to make dynamic LINQ statements in C#](https://stackoverflow.com/questions/23814949/use-reflection-to-make-dynamic-linq-statements-in-c-sharp) – Raymond Nijland Aug 22 '19 at 19:03
  • 2
    'legislacao' is a string. This is not the query that you executed. – Strawberry Aug 22 '19 at 19:17
  • Maybe I should be able to glean this from your code, but what library are you using for data access? – Casey Crookston Aug 22 '19 at 19:35
  • @KolA I'm sorry, could you be more specific? Didn't understand the" fake static data".You want me to create only a few records and try to get them? – Leon700 Aug 22 '19 at 20:14
  • @RaymondNijland So something like this would not be reflection? `db.Legislacaos.SqlQuery("SELECT * FROM Legislacao WHERE leg_ativo = 1").ToList();´ – Leon700 Aug 22 '19 at 20:15
  • 1
    @Strawberry Thats what was executed insed the "phpMyAdmin" and returned the right values – Leon700 Aug 22 '19 at 20:16
  • @CaseyCrookston i don't know if this is what you want but: EntityFramework 6.1.3 (sorry if i'm mistaken) – Leon700 Aug 22 '19 at 20:18
  • @Leon700, yes, that's what I was asking. I stopped using EntityFramework because of exactly these kinds of problems. It's SO SLOOOW and bloated. The very site we are using right now felt the same way and pioneered the data access layer I now use exclusively: Dapper. I know this doesn't help answer your question. Sorry. – Casey Crookston Aug 22 '19 at 20:22
  • @Leon700 I was wrong sorry - I thought that bottleneck is not isolated but now I see that it actually is (stopwatch wraps one line of LINQ code). My suggestion was to test how long it takes your site to respond with all db code commented out. One thing I can add to discussion is that DbContext implements IDisposable but is not disposed (probably not the root cause of your issue) – KolA Aug 22 '19 at 20:23
  • What is the SQL that EF is generating? Attach to the log, something like `db.Database.Log = Console.Write;` see https://learn.microsoft.com/en-us/ef/ef6/fundamentals/logging-and-interception – BurnsBA Aug 22 '19 at 20:35
  • How many records is the query expected to return? If it's a lot, adding `.AsNoTracking()` to the query (before `.ToList()`) could improve performance: https://learn.microsoft.com/en-us/ef/ef6/fundamentals/performance/perf-whitepaper#5-notracking-queries – devNull Aug 22 '19 at 20:56
  • @CaseyCrookston Well, at least is a tip for a future project, thanks :) – Leon700 Aug 22 '19 at 21:22
  • @KolA Yep, i have it implementes down in the class "override Disposable..." Thanks, anyway – Leon700 Aug 22 '19 at 21:23
  • @BurnsBA ``{SELECT `Extent1`.`leg_id`, `Extent1`.`leg_nome`, `Extent1`.`leg_resumo`, `Extent1`.`leg_anexo`, `Extent1`.`leg_ativo` FROM `Legislacao` AS `Extent1` WHERE `Extent1`.`leg_ativo` = 1}` `This is the sql – Leon700 Aug 22 '19 at 21:23
  • @devNull I'm sorry. I had already tried it but forgot to write in the question. My fault. The time didn't changed at all, in the 10,100,300,600 and 1000. Same 5s in all of them – Leon700 Aug 22 '19 at 21:25
  • 1
    Is it _only_ the first invocation that takes 5 seconds? How many records are in `Legislacaos`? Please show us the `CREATE TABLE` for `Legislacaos`. – mjwills Aug 22 '19 at 21:28
  • 1
    Is your C# code running on the same machine as your database? Are you sure the 5 seconds isn't just network latency / transit? – mjwills Aug 22 '19 at 21:28
  • @RaymondNijland Reflection has nothing to do with this. – NetMage Aug 22 '19 at 22:50
  • @Leon700 As you can see, EF is generating the same SQL as your phpMyadmin test so that isn't the problem. You have an issue elsewhere. Are you sure you are timing correctly? Replace `sw.Start()` with `sw.Restart()` and see what happens. – NetMage Aug 22 '19 at 22:53
  • try plain SQL to see whether EF is the element which is slowing it down. – michal.jakubeczy Aug 23 '19 at 13:08

0 Answers0