简体   繁体   English

Postgresql npgsql第一次连接花费的时间太长

[英]Postgresql npgsql first connection takes too long

I am new to Postgresql. 我是Postgresql的新手。 I am tring to use Postgresql with entity framework6, using npgsql. 我正尝试使用npgsql将Postgresql与实体framework6一起使用。

I already have a database. 我已经有一个数据库。 I am using "Code First form database" option. 我正在使用“代码优先表单数据库”选项。

The problem is that the first time a query is executed, it takes me lots of time to execute it. 问题是第一次执行查询时,我要花很多时间来执行它。 I think that is when the connection is opened for the first time. 我认为那是第一次打开连接时。

I created this simple example with the problem (TestJSONB is DbContext): 我用问题创建了这个简单的示例(TestJSONB是DbContext):

class Program
{
    static void Main(string[] args)
    {
        TestQuery();
        TestQuery();
    }

    private static void TestQuery()
    {
        using (DALJSONB.TestJSONB dataModel = new DALJSONB.TestJSONB())
        {
            var query1 =
                dataModel.Database.SqlQuery<int>(
                    @"

                    SELECT 1;
                ");

            query1.ToList();

            var query2 =
                dataModel.Database.SqlQuery<int>(
                    @"

                    SELECT 1;
                ");

            query2.ToList();
        }
    }
 }

The first execution times of TestQuery() are something like: TestQuery()的首次执行时间类似于:

  • query1.ToList() - 2348ms query1.ToList()-2348ms
  • query2.ToList() - 2ms query2.ToList()-2毫秒

The second execution times of TestQuery() are something like: TestQuery()的第二次执行时间类似于:

  • query1.ToList() - 19ms query1.ToList()-19毫秒
  • query2.ToList() - 2ms query2.ToList()-2毫秒

My app.config is: 我的app.config是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <connectionStrings>
        <add name="TestJSONB" connectionString="Host=x.x.x.x;Username=x;Password=x;Persist Security Info=True;Database=TestJSONB" providerName="Npgsql" />
    </connectionStrings>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
        </providers>
    </entityFramework>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-3.2.1.0" newVersion="3.2.1.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

Is it possible to decrease this time? 可以减少这个时间吗?

Thanks in advance. 提前致谢。

This very likely has nothing to do with PostgreSQL or Npgsql, but rather with Entitt Framework itself starting up, building your model etc. try to connect to your database without EF (Ie just creating an NpgsqlConnection and opening it) and you should see it runs pretty quickly. 这很可能与PostgreSQL或Npgsql无关,而是与Entitt Framework本身启动,构建模型等有关。尝试在没有EF的情况下连接到数据库(即仅创建一个NpgsqlConnection并打开它),并且应该看到它正在运行很快。

It's common for EF applications to send sa sort of dummy warm-up query before actually servicing user calls, precisely to avoid this significant first-time delay. EF应用程序通常在实际服务用户调用之前先发送某种虚拟的预热查询,以精确地避免这种严重的首次延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM