NHibernate与SQLite插入性能问题

时间:2010-12-17 12:54:10

标签: c# .net performance nhibernate sqlite

我正在使用NHibernate 3和SQLite 3(更准确地说 - SQLCipher,但在这种情况下没有意义)。

我已经像这样配置了NHibernate:

<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">Test.NHibernate.MySqliteDriver, nhibernate_test</property>     
        <property name="connection.connection_string">Data Source=embedded</property>
        <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
        <property name="query.substitutions">true=1;false=0</property>  
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu, Version=3.0.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4</property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

此处,Test.NHibernate.MySqliteDriver, nhibernate_test是一个自定义驱动程序,可以处理Data Source=embedded之类的连接字符串。除了将SQLiteConnection创建到内存DataSource并使用ATTACH指令将一个数据库附加到它之外,它不执行任何特定操作。它也没有意义。

我有一张桌子:

public class Product
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Category { get; set; }
    public virtual bool Discontinued { get; set; }
}

映射到:

CREATE TABLE Prosucts (
    Id INTEGER PRIMARY KEY,
    Name VARCHAR(250),
    Category VARCHAR(250),
    Discontinued INTEGER
);

使用映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   assembly="nhibernate_test" 
                   namespace="Test.NHibernate.Domain">
    <class name="Product" table="Products">
        <id name="Id">
            <generator class="native" />
        </id>
        <property name="Name" />
            <property name="Category" />
            <property name="Discontinued" />
    </class>
</hibernate-mapping>

在代码中,我执行以下操作:

var configuration = new Configuration();
configuration.Configure(typeof(Program).Assembly, "hibernate.cfg.xml");         
configuration.AddAssembly(typeof(Program).Assembly);

var sessionFactory = configuration.BuildSessionFactory();

var product = new Product
{
    Name = "Product",
    Category = "Products",
    Discontinued = true
};

var product2 = new Product
{
    Name = "Product 12",
    Category = "Bad products",
    Discontinued = true
};

using (var session = sessionFactory.OpenSession())
{               
    using(var transaction = session.BeginTransaction())
    {
        var sw = new Stopwatch();
        sw.Start();         

        session.Save(product);
        Console.WriteLine("saved 1st in {0}", sw.Elapsed);
        sw.Reset();
        sw.Start();

        session.Save(product2);
        Console.WriteLine("saved 2nd in {0}", sw.Elapsed);
        sw.Stop();

        transaction.Commit();
    }
}

获得令人失望的输出:

NHibernate:INSERT INTO产品 (名称,类别,已停产)VALUES(@ p0,@ p1,@ p2); select last_insert_rowid(); @ p0 ='Product'[Type:String(0)],@ p1 ='Products'[Type:String(0)],@ p2 = True [Type:Boolean(0)]

在00:00:01.3444869保存第一

NHibernate:INSERT INTO产品(名称,类别,已停产)VALUES(@ p0,@ p1,@ p2); select last_insert_rowid(); @ p0 ='Product'[Type:String(0)],@ p1 ='Products'[Type:String(0)],@ p2 = True [Type:Boolean(0)]

在00:00:00.0044215保存第二

那么为什么插入第一条记录需要这么长时间??? (持续1.3秒)

1 个答案:

答案 0 :(得分:2)

我看到两个可能的来源:

  1. 在为第一个要跟踪的实体提供会话缓存时初始化会话缓存,
  2. SQLite启动其内部以生成第一个本机标识值