简体   繁体   English

Azure SQL Server(弹性池)连接缓慢

[英]Azure SQL Server (elastic pool) connect slow

We ar running a webapplication in Azure Web Apps using a database per customer (multiple accounts per customer). 我们使用每个客户的数据库(每个客户多个帐户)在Azure Web Apps中运行一个Web应用程序。 When logging in we connect the user to the correct customer database. 登录时,我们会将用户连接到正确的客户数据库。 This database is also hosted in azure (an elastic pool). 该数据库还托管在azure(弹性池)中。 It is hosted in the same region (West Europe) as the Web App. 它与Web App托管在同一地区(西欧)。

Once the connection is pooled, request times are fast, but the first time a user log's in, the connection still needs to be created an this takes (quiet) a long time. 连接一旦池化,请求时间就很快,但是第一次登录用户时,仍然需要创建连接,这花费了很长时间。

The connectionstring is build up using a SqlConnectionStringBuilder. 连接字符串是使用SqlConnectionStringBuilder建立的。

var csb = new System.Data.SqlClient.SqlConnectionStringBuilder();
csb.DataSource = "tcp:******.database.windows.net,1433";
csb.InitialCatalog = "***-***-***";
csb.UserID = "**-**";
csb.Password = "**********";
csb.MultipleActiveResultSets = true;
csb.Encrypt = true;
csb.PersistSecurityInfo = false;
csb.TrustServerCertificate = false;
csb.ConnectTimeout = 30;

_connectionString = csb.ConnectionString;
// Data Source=tcp:******.database.windows.net,1433;Initial Catalog=***-***-***;Persist Security Info=False;User ID=**-***;Password=******;MultipleActiveResultSets=True;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False

Am I doing anything wrong? 我做错什么了吗? Or are there some settings in azure to speed up the connect process? 还是有一些天蓝色设置来加快连接过程?

首次请求时间

The above request shows the first request to the application of a customer. 以上请求显示了对客户申请的第一个请求。 It therefor includes the EF Migration Seed resulting in the first 2 queries not actually going to the database itself and quite a lot of queries (not all shown here) to the database. 因此,它包括EF迁移种子,这导致前2个查询实际上并没有直接进入数据库本身,并且有很多查询(此处未全部显示)。

Well, I solved my problem eventualy. 好吧,我终于解决了我的问题。 Seems i was matching wrong queries within Applications Insights. 似乎我在Applications Insights中匹配了错误的查询。 I installed Stackify and this gives just the little bit more information I needed. 我安装了Stackify,这提供了我所需的更多信息。

Seem's Entity Framework does some things with the 'master' database. Seem的Entity Framework使用“ master”数据库执行某些操作。 As the user in the connectionstring did not have access to the 'master' database it throws an error. 由于connectionstring中的用户无权访问“ master”数据库,因此将引发错误。 Well, handling that error take's up quite some time on the app-service used and therefor returning slow. 好吧,在使用的应用程序服务上处理该错误需要花费相当长的时间,因此返回速度很慢。 It just doesn't fail. 它只是没有失败。

What EF tries to do is determine if the database exist by querying the master database wich is faster then connecting to a non existing database. EF尝试通过查询主数据库来确定数据库是否存在,这比连接到不存在的数据库要快。 If it fails because it can not connect to the master database, EF just tries to connect to the database itself. 如果由于无法连接到主数据库而失败,则EF只会尝试连接到数据库本身。 If connection to the database works, it continues normal execution like the seed method. 如果与数据库的连接有效,它将像种子方法一样继续正常执行。

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

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