How We Reduced a 37‑second API Call to 1.5 s with Flask Profiling & MySQL Tweaks

This article walks through a systematic performance investigation of a Python‑Flask backend, using Chrome Network, flame‑graph profiling, and MySQL query refactoring to shrink a 37‑second endpoint down to 1.5 seconds, while sharing practical code snippets and optimization lessons.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How We Reduced a 37‑second API Call to 1.5 s with Flask Profiling & MySQL Tweaks

Background

Our business platform’s settings page was taking an absurd 36 seconds to load, which was unacceptable for users.

Investigating with Chrome Network

Using Chrome’s Network panel we observed that the total request time was 17.67 seconds, with 17.57 seconds spent in the Waiting (TTFB) state. TTFB (Time to First Byte) measures the server’s response latency.

Profiling Flame Graph & Code Analysis

The flame graph revealed that the bottleneck lay in the backend Python + Flask code. The relevant function was get_max_cpus, which spawns a thread per group ID (gid) to fetch CPU max values.

def get_max_cpus(project_code, gids):
    ...
    for gid in gids:
        t = Thread(target=get_max_cpu, args=(...))
        threads.append(t)
        t.start()
    for t in threads:
        t.join()
    return max_cpus

Two problems were identified:

Creating and destroying threads for each web‑API call incurs high overhead; a thread pool would be cheaper.

The data (maximum CPU per gid) has limited value, so loading it eagerly adds unnecessary cost.

First Wave Optimization: Redesign Interaction

We changed the UI to load the CPU max values only on user interaction, eliminating the need for multithreading. The flame graph after this change showed a much healthier distribution.

Second Wave Optimization: MySQL Query Tuning

Further profiling highlighted utils.py:get_group_profile_settings as a database hotspot. Although an index existed, the code performed a separate query for each gid, causing N + 1 queries.

for gid in gids:
    result = session.query(ProfileSetting).filter(ProfileSetting.name == f"{project_code}:{gid}").first()
    ...

We replaced the per‑gid queries with a single batch query using IN and moved the filter outside the loop:

# Batch query
query_results = session.query(ProfileSetting).filter(
    ProfileSetting.name.in_([f"{project_code}:{gid}" for gid in gids])
).all()

for result in query_results:
    if not result:
        continue
    result = result.as_dict()
    gid = result['name'].split(':')[1]
    profile_settings[gid] = {
        'tag_indexes': result.get('tag_indexes'),
        'interval': result['interval'],
        'status': result['status'],
        'profile_machines': result['profile_machines'],
        'thread_settings': result['thread_settings'],
    }

The updated flame graph confirmed a dramatic reduction in database‑related hot spots.

Results

After both optimization waves, the same endpoint’s response time dropped from 37.6 seconds to 1.47 seconds.

Takeaways

Key lessons include:

When possible, remove or defer expensive features.

Batch database queries instead of issuing many single‑row queries.

Use profiling tools to pinpoint real bottlenecks rather than guessing.

Consider the cost‑benefit ratio; further micro‑optimizations may yield diminishing returns.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPerformance OptimizationPythonmysqlProfiling
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.