Mysql – How to auto-refresh/re-run a query every few seconds

MySQLmysql-workbench

I have a script running against a database with no GUI. This simple query lets me see its progress:

select
    *,
    (select count(*) from domains) as count,
    (select 456976-count(*) from domains) as remaining
from domains
order by domain desc
limit 0, 1;

Using MySQL Workbench, how can I automatically refresh/re-run this query every few seconds?

I tried Googling, but didn't come up with anything relevant.

Aside: the magic number 456,976 is 26^4; it has to do with the script.

Best Answer

First, create a Stored Procedure that calls the query and sleeps every 10 seconds

use mydatabase
DELIMITER $$
DROP PROCEDURE IF EXISTS `MyDataStatus` $$
CREATE PROCEDURE `MyDataStatus` ()
BEGIN
    SET @SecondsToPause = 10;
    SELECT COUNT(*) INTO @CurrentCount FROM domains;
    SET @Remaining = 456976 - @CurrentCount;
    WHILE @Remaining > 0 DO
        SELECT *,@CurrentCount as `count`,@CurrentCount as `remaining`
        FROM domains ORDER BY domain DESC LIMIT 0, 1;
        SELECT SLEEP(@SecondsToPause) INTO @SleepValue;
        SELECT COUNT(*) INTO @CurrentCount FROM domains;
        SET @Remaining = 456976 - @CurrentCount;
    END $$
DELIMITER ;

Now, call the Stored Procedure from DOS Command Line

set MYSQL_USER=root
set MYSQL_PASS=password
set MYSQL_MYDB=mydatabase
set MYSQL_CONN=-u%MYSQL_USER% -p%MYSQL_PASS% -D%MYSQL_MYDB%
mysql %MYSQL_CONN% -ANe"CALL MyDataStatus()"

or you could login to mysql

mysql %MYSQL_CONN% 

then call the query at the MySQL prompt

mysql> CALL MyDataStatus();

Give it a Try !!!

Caveat

Please notice I call the COUNT once and subtract it from 456976 to speed up the count a little.