【问题标题】:Postgresql npgsql first connection takes too longPostgresql npgsql 第一次连接耗时太长
【发布时间】:2017-02-16 16:15:46
【问题描述】:

我是 Postgresql 的新手。我正在尝试将 Postgresql 与实体 framework6 一起使用,使用 npgsql。

我已经有一个数据库。我正在使用“代码优先表单数据库”选项。

问题是第一次执行查询时,我需要花费很多时间来执行它。我想那是第一次打开连接的时候。

我创建了这个有问题的简单示例(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();
        }
    }
 }

TestQuery() 的第一次执行时间是这样的:

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

TestQuery() 的第二次执行时间是这样的:

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

我的 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>

这个时间可以减少吗?

提前致谢。

【问题讨论】:

    标签: c# postgresql entity-framework-6 npgsql


    【解决方案1】:

    这很可能与 PostgreSQL 或 Npgsql 无关,而是与 Entitt Framework 本身启动、构建模型等有关。尝试在没有 EF 的情况下连接到您的数据库(即仅创建一个 NpgsqlConnection 并打开它),您应该看它运行得很快。

    EF 应用程序通常会在实际服务用户调用之前发送一种虚拟的预热查询,正是为了避免这种显着的首次延迟。

    【讨论】:

    • 我在我的电脑上托管的虚拟机上有 postgresql。我按照你告诉我的做了,发现了两种不同的情况。首先,在我的电脑上。当我执行 Npgsql.NpgsqlConnection connection = new Npgsql.NpgsqlConnection("...") 我需要 2 秒。其他人很快。第二种情况,在虚拟机中执行。 Npgsql.NpgsqlConnection connection = new Npgsql.NpgsqlConnection("...") 很快,第一个查询需要 1.7 秒,另一个查询很快。有什么想法吗?
    • 准备接受这个答案。认为它确实与实体框架有关。谢谢
    • 还有一个补充;自从写了这个答案后,我收到了几份关于 Npgsql 3.2.0 中新的启动时间异常缓慢的报告(3.1.x 没有导致这种情况)。显然 Npgsql 3.2.0 对性能计数器的新用法会导致此问题,并将在 3.2.2 中修复(请参阅github.com/npgsql/npgsql/issues/1435)。
    猜你喜欢
    • 2013-01-02
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多