Skip to content

Instantly share code, notes, and snippets.

@jpjuliao
Last active March 22, 2025 19:25
Show Gist options
  • Save jpjuliao/243cac948de086c8279130c5056c6e80 to your computer and use it in GitHub Desktop.
Save jpjuliao/243cac948de086c8279130c5056c6e80 to your computer and use it in GitHub Desktop.
MySQL Optimization Techniques

1. How would you identify if a query is properly indexed

I'd use EXPLAIN in MySQL to analyze a query. It provides insight into how the database executes the query, including whether indexes are being utilized.

Example:

EXPLAIN SELECT * FROM users WHERE email = '[email protected]';

The output will show if an index is being used for the query then I will look for the key column in the result; if it’s populated with the name of an index, then the query is indexed properly.

I'd also analyze slow query logs to identify unoptimized queries and check if indexing could help.

2. How would you estimate the run time of a query

I will perform these operations:

  • I'd use the EXPLAIN command to review which operations are expensive.
  • Benchmark using system tools: For example, use tools like mysqlslap to simulate concurrent queries and measure performance.
  • Simulate the query with sample data by running the query on a smaller dataset to estimate scaling.
  • Track database metrics: Use monitoring tools (e.g., Grafana or New Relic) to observe query performance over time.

3. How would you rotate database credentials

I'd automate rotation with tools like AWS Secrets Manager or HashiCorp Vault to schedule and enforce regular credential rotation.

4. If the MySQL results are too large to send to the front end, how would you handle it? Please demonstrate with example code

For large result sets, I will paginate data to limit what is sent to the front end.

Back end code with PHP/MySQL:

<?php
$mysqli = new mysqli("localhost", "username", "password", "database");

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;

$query = "SELECT * FROM your_table LIMIT $limit OFFSET $offset";
$result = $mysqli->query($query);

$data = [];
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

header('Content-Type: application/json');
echo json_encode([
    'data' => $data,
    'page' => $page,
    'total_pages' => ceil($mysqli->query("SELECT COUNT(*) AS count FROM your_table")->fetch_assoc()['count'] / $limit)
]);
?>

Front end code:

const fetchPage = async (page) => {
    const response = await fetch(`server_script.php?page=${page}`);
    const result = await response.json();
    console.log('Data:', result.data);
    console.log('Current Page:', result.page);
    console.log('Total Pages:', result.total_pages);
};

fetchPage(1);

5. If a query is not indexed, do you have a way to still run it efficiently without adding a new index? Please demonstrate with example code

I'd try to optimize the query by using techniques like query restructuring, temporary tables or fetching only required columns to reduce the workload on the database.

Example using Temporary Tables to Reduce Workload:

Query without Optimization:

SELECT * 
FROM orders 
WHERE customer_id IN (
    SELECT customer_id 
    FROM customers 
    WHERE region = 'North America'
);

Optimized:

-- Create a temporary table to store intermediate results
CREATE TEMPORARY TABLE TempCustomers AS
SELECT customer_id 
FROM customers 
WHERE region = 'North America';

-- Use the temporary table to improve performance
SELECT * 
FROM orders 
WHERE customer_id IN (SELECT customer_id FROM TempCustomers);

-- Drop the temporary table after use to freeing up resources
DROP TEMPORARY TABLE TempCustomers;

The CREATE TEMPORARY TABLE step isolates the smaller subset of customers from customers table who belong to the specific region.

6. If a long-running query has to be executed, how would you prevent it from locking up other processes?

  • Divide the large query into smaller chunks to avoid overloading the database and reduce lock contention.
  • To ensure the query doesn’t monopolize resources indefinitely, enforce a timeout at the database or application level.
  • Long-running queries can be executed asynchronously using tools like background workers or job queues. This decouples query execution from the main application flow, ensuring it doesn’t block other processes.
  • If the query involves row locking, use options like NOWAIT or SKIP LOCKED to avoid blocking other processes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment