含有10000行数据库表(PostgreSQL的):
CREATE TABLE test
(
id bigserial NOT NULL,
text text,
CONSTRAINT test_pkey PRIMARY KEY (id)
)
类别:
public class Test
{
public virtual int ID { get; set; }
public virtual string Text { get; set; }
}
public class TestMap : ClassMap<Test>
{
public TestMap()
{
Table("test");
Id(x => x.ID, "id");
Map(x => x.Text, "text");
}
}
这是我如何查询数据库:
using (ISession session = SessionBuilder.OpenSession())
{
Stopwatch s = new Stopwatch();
s.Start();
var result = session.QueryOver<Test>().Where(test => test.Text == "aaa").List();
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds); // >150ms
s.Restart();
var result2 = session.QueryOver<Test>().Where(test => test.Text == "bbb").List();
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds); // ~4ms
}
为什么第一次查询时间这么长? 有没有一种方法,以加快步伐?