How to Suppress SELECT Output in MySQL CLI for Better Terminal Performance


9 views

When running large SELECT queries in MySQL CLI, we often face terminal performance issues as thousands of rows get printed. This is particularly problematic during:

  • Database warm-up scripts
  • Benchmark testing
  • ETL processes
  • Data migration tasks

Here are several effective methods to suppress output while keeping the query execution:

Method 1: Using --silent (-s) flag

mysql -u username -p -s -e "SELECT * FROM large_table" database_name

Method 2: Redirecting to /dev/null

mysql -u username -p -e "SELECT * FROM large_table" database_name > /dev/null

Method 3: Using --batch (-B) mode

mysql -u username -p -B -e "SELECT * FROM large_table" database_name

You can still get execution metrics while suppressing output:

mysql -u username -p -s -e "SELECT SQL_NO_CACHE * FROM large_table" database_name
SHOW STATUS LIKE 'Handler%';

Add this to your ~/.my.cnf file:

[client]
silent
# or
batch
Method Best Use Case Notes
-s/--silent Script execution Only suppresses output
-B/--batch Data processing Also changes format
Redirect Complete silence Loses error messages

Here's how I structure my DB warm-up scripts:

#!/bin/bash
mysql -u app_user -p$PASSWORD -s -e "
  SELECT COUNT(*) FROM users;
  SELECT COUNT(*) FROM transactions;
  ANALYZE TABLE important_data;
" my_database

# Check if queries succeeded without seeing output
if [ $? -eq 0 ]; then
  echo "Database warmed up successfully"
fi

We've all been there - you're running a large SELECT query in MySQL through the terminal, and suddenly your screen gets flooded with thousands of rows of data. Not only does this make the terminal unusable, but it also significantly slows down the query execution due to the I/O overhead of printing all that data.

Here are several effective ways to prevent MySQL from printing query results to your terminal:


-- Method 1: Redirect output to /dev/null (Unix/Linux)
mysql -u username -p db_name -e "SELECT * FROM large_table" > /dev/null

-- Method 2: Use the --silent (-s) flag
mysql -u username -p db_name -s -e "SELECT * FROM large_table"

-- Method 3: Combine with --skip-column-names (-N)
mysql -u username -p db_name -sN -e "SELECT * FROM large_table"

-- Method 4: For stored procedures or scripts
DELIMITER //
CREATE PROCEDURE warm_up_db()
BEGIN
    -- Your warm-up queries here
    SELECT * FROM large_table LIMIT 0;
END //
DELIMITER ;

When warming up your database, you might want to execute queries without seeing the output but still verify they ran successfully:


-- Check query execution time without output
mysql -u username -p db_name -e "SELECT SQL_NO_CACHE * FROM large_table" > /dev/null

-- Warm up indexes quietly
mysql -u username -p db_name -sN -e "ANALYZE TABLE large_table"

-- Verify query worked without seeing results
mysql -u username -p db_name -s -e "SELECT COUNT(*) FROM large_table" | grep -v '^[0-9]'

If you always want to work in quiet mode, add this to your ~/.my.cnf file:


[client]
silent
skip-auto-rehash

When writing scripts that process large result sets, it's better to:


#!/bin/bash
# Process data without displaying it
result=$(mysql -sN -u username -p db_name -e "SELECT id FROM large_table")
for id in $result; do
    # Process each ID
    echo "Processing ID: $id"
done