Archive for the ‘MySQL’ Category

MySQL Group By

Many people are caught out by MySQL’s implementation of GROUP BY. By default MySQL does not require that you GROUP BY all non-aggregated columns. For example the following is an illegal query in SQL Server (as well as rather nonsensical); ?View Code MYSQLSELECT * FROM INFORMATION_SCHEMA.TABLES GROUP BY TABLE_SCHEMA; This will generate the following error; [...]

MySQL Database Maintenance Stored Procedure

Here’s a very simple stored procedure I use to run some maintenance on MySQL tables. It allows you to run OPTIMIZE TABLE or ANALYZE TABLE on all (or most) tables in a MySQL database. ?View Code MYSQLDELIMITER $$   DROP PROCEDURE IF EXISTS `db_maintenance`$$   CREATE DEFINER = ‘root’@'%’ PROCEDURE db_maintenance ( IN p_mode TINYINT, [...]

MySQL Storage engine benchmarking

Here’s a stored procedure I use to perform some simple benchmarking of inserts for MySQL. It takes three parameters; p_table_type which should be set to the storage engine you wish to benchmark i.e. ‘MyISAM’, ‘InnoDB’, p_inserts; set this to the number of inserts to perform. p_autocommit; set the autocommit variable (relevant to InnoDB only) to [...]

Synchronize Mysql slave tables with mk-table-sync

I’ve been meaning to check out Maatkit for a while now. Today I had a reason to as one of our MySQL slaves got out of sync with the master. I’d heard about mk-table-sync, a tool that synchronizes tables, so I thought I’d give it a shot. As it turns out it’s this easy; mk-table-sync [...]

Backing up the structure of MySQL databases

Today I wanted a quick and easy way to generate a backup of the structure of all MySQL databases in one easy hit. Here’s a couple of ways you can do this with the tools you’re likely to find everywhere. Firstly, we can use the following query to generate a list of mysqldump commands. The [...]

Add column headers to a MySQL Outfile

Unfortunately the MySQL SELECT INTO OUTFILE command doesn’t support an option to output the headers of the result set you are exporting. A feature request has been open for over 2 years to sort this with no apparent activity. A few people have had the idea of using a UNION ALL to include the headers [...]

Parse MySQL Slow Logs with mysqlsla

Here’s a bash script that you can use to parse multiple MySQL Slow Query Log files, in one sweep, into something much more understandable. The script uses the handy utility mysqlsla so make sure this is in your path.  mysqlsla parses, filters, analyzes and sorts MySQL slow, general, binary and microslow patched logs in order [...]

Can’t reopen table: ‘t1′

I’m quite often jumping between MySQL and SQL Server so remembering the quirks and limitations of each system can be difficult. With MySQL, if you attempt to reference a temporary table more than once in the same query, you will encounter the following error; Error Code : 1137 Can’t reopen table: ‘t1’ The following provides [...]

Rename MySQL Stored Procedures

I’ve previously blogged about the limitations of MySQL Alter Procedure Syntax and I came across a thread on the MySQL forums with a possible solution. I thought it might be handy to wrap this up into a stored procedure akin to SQL Server’s sp_rename. This procedure will allow you to easily rename MySQL Stored Procedures [...]

Purge MySQL Binary Logs

From time-to-time you may need to manually purge binary logs on your MySQL slaves to free up a bit of disk space. We can achieve this by using the PURGE BINARY LOGS command from the MySQL command line client. MySQL advises the following procedure when purging these logs To safely purge binary log files, follow [...]