首页 新闻 会员 周边

请问以下asp.net core mysql访问代码有无优化手段?我的机器上QPS仅600多

0
悬赏园豆:100 [待解决问题]

新的空asp.net core 项目,加入下面的代码,试过连接池,效果不明显。
只输出hello world的话php 6000左右 asp.net core 10000左右,加了数据访问,php 1000左右,asp.net core就600左右了,我想asp.net core绝不会这样。

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace webapp
{
public class Startup
{

    string constructorString = "server=localhost;uid=root;pwd=123456;Database=test;";
     
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRouting(configureOptions => {
            configureOptions.LowercaseUrls = true;

        });
    }


    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGet("/", async context =>
            {
                var list = new List<Dictionary<string, string>>();
                MySqlConnection myConnnect;
                MySqlCommand sqlCmd;
                myConnnect = new MySqlConnection(constructorString);
                sqlCmd = new MySqlCommand();
                sqlCmd.Connection = myConnnect;
                sqlCmd.CommandText = "select * from location where parent_id=0";
                await myConnnect.OpenAsync();
                
                using (var reader = await sqlCmd.ExecuteReaderAsync(System.Data.CommandBehavior.CloseConnection))
                {
                    while (await reader.ReadAsync())
                    {
                        var data = new Dictionary<string, string>();
                        data["id"] = reader.GetInt32(0).ToString();
                        data["spell_first"] = reader.GetString(2);
                        data["spell_full"] = reader.GetString(3);
                        data["name"] = reader.GetString(4);
                        list.Add(data);
                    }
                }


                await context.Response.WriteAsync("Hello World!");
            });
        });
    }
}

}

泥菩萨123的主页 泥菩萨123 | 初学一级 | 园豆:11
提问于:2021-02-04 00:08
< >
分享
所有回答(3)
0
 while (await reader.ReadAsync())
这里可以根据业务做下优化,现在的代码,每次只返回一条数据。N条数据,至少经过N次TCP传输数据。
可以一次返回多条数据(分页),减少数据传输的耗时。
大志若愚 | 园豆:2138 (老鸟四级) | 2021-02-04 11:13
0

吧sql的connection 池化. 马上能上来几个量级.

czd890 | 园豆:14412 (专家六级) | 2021-02-04 17:22

试过。没效果。而且connection默认就是使用连接池的。

支持(0) 反对(0) 泥菩萨123 | 园豆:11 (初学一级) | 2021-02-04 19:57

@泥菩萨123:

  1. threadpool 的size也调大,
  2. constructorString 里面吧poolsize参数都配置上.
支持(0) 反对(0) czd890 | 园豆:14412 (专家六级) | 2021-02-04 21:50

@czd890: 试过了。没效果。难受。

支持(0) 反对(0) 泥菩萨123 | 园豆:11 (初学一级) | 2021-02-04 22:38

@泥菩萨123: 那就vs里面看看性能监控, 看看什么地方耗时.

支持(0) 反对(0) czd890 | 园豆:14412 (专家六级) | 2021-02-04 22:47

@czd890: 就是数据库访问那段

支持(0) 反对(0) 泥菩萨123 | 园豆:11 (初学一级) | 2021-02-04 23:41
0

建议先去掉一些代码 测试QPS
再加上那段代码 如果QPS上来后 就说明那一段有性能问题
然后针对那一段去采取别的办法实现

ycyzharry | 园豆:25653 (高人七级) | 2021-02-04 23:30

这就难受了。就那么一点点代码。你看我去掉哪行合适?

支持(0) 反对(0) 泥菩萨123 | 园豆:11 (初学一级) | 2021-02-04 23:41

@泥菩萨123: mysql连接那块

支持(0) 反对(0) ycyzharry | 园豆:25653 (高人七级) | 2021-02-07 16:50

@ycyzharry: ......兄弟。就是这段慢。去掉了当然快了。而且我要优化的就是这段啊。

支持(0) 反对(0) 泥菩萨123 | 园豆:11 (初学一级) | 2021-02-07 21:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册