Skip to content

Feature: Optimize MySQL connection handling: avoid per-connection thread and runtime creation #19290

@yufan022

Description

@yufan022

Summary

Problem

Based on v1.2.636-rc8.6 code, if I understand correctly, every new MySQL connection creates:

  1. A dedicated OS thread

    • thread::spawn() is called for each incoming connection
    • Thread creation/destruction overhead is significant under high concurrency
  2. A dedicated Tokio runtime

    let query_executor =
    Runtime::with_worker_threads(1, Some("mysql-query-executor".to_string()))?;

    • Runtime::with_worker_threads(1, "mysql-query-executor") is created per connection
    • Each runtime has its own thread pool and scheduler overhead
    • Memory and CPU resources cannot be shared across connections

Impact (AI-generated)

  • High memory consumption under concurrent connections
  • Poor resource utilization due to isolated runtimes
  • Potential resource exhaustion with many simultaneous connections
  • Unnecessary thread context switching overhead

Suggested Solution (AI-generated)

  • Use a shared Tokio runtime (or the global runtime) for all MySQL connections
  • Replace thread::spawn() with tokio::spawn() to leverage async task scheduling
  • Consider using a connection pool or worker pool pattern for better resource management

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions