Tony
Tony

Reputation: 12695

Fluent Nhibernate very slow query execution (DB hosted on localhost)

I have a very simple entity:

public class Days
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
}

map class

public class DaysMap : ClassMap<Days> 
{
    public DaysMap()
    {
        Table("Days");
        Id(x => x.ID).GeneratedBy.Identity().Column("ID");
        Map(x => x.Name).Column("Name");
    }
}

and here's the test, I want to load the Day with the ID = 1. I can load that item,but the time needed to do it = 2,5s ! This is the MySQL database stored on localhost.

    private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
                        .Database(MySQLConfiguration.Standard
                        .ConnectionString("server=localhost;user id=someID;password=pass;database=someDB"))
                        .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
                        .BuildSessionFactory();
    }

    public ActionResult Index()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();

        using (var sessionFactory = CreateSessionFactory())
        {
            using (var session = sessionFactory.OpenSession())
            {
                using (session.BeginTransaction())
                {
                    var asd = session.Query<Days>().Where(x => x.ID == 1).FirstOrDefault();
                }
            }                
        }

        sw.Stop();

        var timeElapsed = sw.Elapsed.TotalMilliseconds; // returns 2504.3316 !!

        return View();
    }

Upvotes: 0

Views: 193

Answers (1)

Cole W
Cole W

Reputation: 15303

Hopefully you aren't doing the following every time:

private static ISessionFactory CreateSessionFactory()
{
    return Fluently.Configure()
                    .Database(MySQLConfiguration.Standard
                    .ConnectionString("server=localhost;user id=someID;password=pass;database=someDB"))
                    .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
                    .BuildSessionFactory();
}

Creating the SessionFactory should only be done 1 time per application. You cannot include this time when determining how long a query takes. Generally I build this up front on application startup to eliminate having an initial query take forever because I haven't built it yet. This is very common practice when using NHibernate.

Upvotes: 1

Related Questions