MK-FIND(1)
MK-FIND(1) π User Contributed Perl Documentation π
okay bestie, listen up. This is mk-find(1), and it's giving "GNU find but make it MySQL tables." No cap. β¨
NAME π
mk-find β Find MySQL tables and execute actions, like GNU find.
SYNOPSIS πΈ
Usage: mk-find [OPTION...] [DATABASE...]
mk-find searches for MySQL tables and executes actions, like GNU find. The default action is to print the database and table name.
Find all tables created more than a day ago, which use the MyISAM engine, and print their names:
mk-find --ctime +1 --engine MyISAM
Find InnoDB tables that haven't been updated in a month, and convert them to MyISAM storage engine (data warehousing, anyone?):
mk-find --mtime +30 --engine InnoDB --exec "ALTER TABLE %D.%N ENGINE=MyISAM"
Find tables created by a process that no longer exists, following the name_sid_pid naming convention, and remove them.
mk-find --connection-id '\D_\d+_(\d+)$' --server-id '\D_(\d+)_\d+$' --exec-plus "DROP TABLE %s"
Find empty tables in the test and junk databases, and delete them:
mk-find --empty junk test --exec-plus "DROP TABLE %s"
Find tables more than five gigabytes in total size:
mk-find --tablesize +5G
Find all tables and print their total data and index size, and sort largest tables first (sort is a different program, by the way).
mk-find --printf "%T\t%D.%N\n" | sort -rn
As above, but this time, insert the data back into the database for posterity:
mk-find --noquote --exec "INSERT INTO sysdata.tblsize(db, tbl, size) VALUES('%D', '%N', %T)"
RISKS β οΈ
The following section is included to inform users about the potential risks, whether known or unknown, of using this tool. The two main categories of risks are those created by the nature of the tool (e.g., read-only tools vs. read-write tools) and those created by bugs.
mk-find only reads and prints information by default, but --exec and --exec-plus can execute user-defined SQL. You should be as careful with it as you are with any command-line tool that can execute queries against your database.
At the time of this release, we know of no bugs that could cause serious harm to users.
The authoritative source for updated information is always the online issue tracking system. Issues that affect this tool will be marked as such. You can see a list of such issues at the following URL: http://www.maatkit.org/bugs/mk-find.
See also "BUGS" for more information on filing bugs and getting help.
bestie, be careful with--execand--exec-plus. That's real SQL, and it will do the thing. π
DESCRIPTION π¦
mk-find looks for MySQL tables that pass the tests you specify, and executes the actions you specify. The default action is to print the database and table name to STDOUT.
mk-find is simpler than GNU find. It doesn't allow you to specify complicated expressions on the command line.
mk-find uses SHOW TABLES when possible, and SHOW TABLE STATUS when needed.
OPTION TYPES π
There are three types of options: normal options, which determine some behavior or setting; tests, which determine whether a table should be included in the list of tables found; and actions, which do something to the tables mk-find finds.
mk-find uses standard Getopt::Long option parsing, so you should use double dashes in front of long option names, unlike GNU find.
OPTIONS β¨
This tool accepts additional command-line arguments. Refer to the "SYNOPSIS" and usage information for details.
--ask-passβ Prompt for a password when connecting to MySQL.--case-insensitiveβ Specifies that all regular expression searches are case-insensitive.--charsetβ short form:-A; type: string. Default character set. If the value isutf8, sets Perl'sbinmodeonSTDOUTtoutf8, passes themysql_enable_utf8option toDBD::mysql, and runsSET NAMES UTF8after connecting to MySQL. Any other value setsbinmodeonSTDOUTwithout theutf8layer, and runsSET NAMESafter connecting to MySQL.--configβ type: Array. Read this comma-separated list of config files; if specified, this must be the first option on the command line.--day-startβ Measure times (for--mmin, etc) from the beginning of today rather than from the current time.--defaults-fileβ short form:-F; type: string. Only read mysql options from the given file. You must give an absolute pathname.--helpβ Show help and exit.--hostβ short form:-h; type: string. Connect to host.--orβ Combine tests with OR, not AND. By default, tests are evaluated as though there were an AND between them. This option switches it to OR. Option parsing is not implemented by mk-find itself, so you cannot specify complicated expressions with parentheses and mixtures of OR and AND.--passwordβ short form:-p; type: string. Password to use when connecting.--pidβ type: string. Create the given PID file. The file contains the process ID of the script. The PID file is removed when the script exits. Before starting, the script checks if the PID file already exists. If it does not, then the script creates and writes its own PID to it. If it does, then the script checks the following: if the file contains a PID and a process is running with that PID, then the script dies; or, if there is no process running with that PID, then the script overwrites the file with its own PID and starts; else, if the file contains no PID, then the script dies.--portβ short form:-P; type: int. Port number to use for connection.--[no]quoteβ default: yes. Quotes MySQL identifier names with MySQL's standard backtick character. Quoting happens after tests are run, and before actions are run.--set-varsβ type: string; default:wait_timeout=10000. Set these MySQL variables. Immediately after connecting to MySQL, this string will be appended toSETand executed.--socketβ short form:-S; type: string. Socket file to use for connection.--userβ short form:-u; type: string. User for login if not current user.--versionβ Show version and exit.
TESTS π₯
Most tests check some criterion against a column of SHOW TABLE STATUS output. Numeric arguments can be specified as +n for greater than n, -n for less than n, and n for exactly n. All numeric options can take an optional suffix multiplier of k, M or G (1_024, 1_048_576, and 1_073_741_824 respectively). All patterns are Perl regular expressions (see 'man perlre') unless specified as SQL LIKE patterns.
Dates and times are all measured relative to the same instant, when mk-find first asks the database server what time it is. All date and time manipulation is done in SQL, so if you say to find tables modified 5 days ago, that translates to SELECT DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 5 DAY). If you specify --day-start, if course it's relative to CURRENT_DATE instead.
However, table sizes and other metrics are not consistent at an instant in time. It can take some time for MySQL to process all the SHOW queries, and mk-find can't do anything about that. These measurements are as of the time they're taken.
If you need some test that's not in this list, file a bug report and I'll enhance mk-find for you. It's really easy.
--autoincβ type: string; group: Tests. Table's nextAUTO_INCREMENTis n. This tests theAuto_incrementcolumn.--avgrowlenβ type: size; group: Tests. Table avg row len is n bytes. This tests theAvg_row_lengthcolumn. The specified size can be"NULL"to test whereAvg_row_length IS NULL.--checksumβ type: string; group: Tests. Table checksum is n. This tests theChecksumcolumn.--cminβ type: size; group: Tests. Table was created n minutes ago. This tests theCreate_timecolumn.--collationβ type: string; group: Tests. Table collation matches pattern. This tests theCollationcolumn.--column-nameβ type: string; group: Tests. A column name in the table matches pattern.--column-typeβ type: string; group: Tests. A column in the table matches this type (case-insensitive). Examples of types are:varchar,char,int,smallint,bigint,decimal,year,timestamp,text,enum.--commentβ type: string; group: Tests. Table comment matches pattern. This tests theCommentcolumn.--connection-idβ type: string; group: Tests. Table name has nonexistent MySQL connection ID. This tests the table name for a pattern. The argument to this test must be a Perl regular expression that captures digits like this:(\d+). If the table name matches the pattern, these captured digits are taken to be the MySQL connection ID of some process. If the connection doesn't exist according toSHOW FULL PROCESSLIST, the test returns true. If the connection ID is greater than mk-find's own connection ID, the test returns false for safety.
Why would you want to do this? If you use MySQL statement-based replication, you probably know the trouble temporary tables can cause. You might choose to work around this by creating real tables with unique names, instead of temporary tables. One way to do this is to append your connection ID to the end of the table, thusly: scratch_table_12345. This assures the table name is unique and lets you have a way to find which connection it was associated with. And perhaps most importantly, if the connection no longer exists, you can assume the connection died without cleaning up its tables, and this table is a candidate for removal.
This is how I manage scratch tables, and that's why I included this test in mk-find.
The argument I use to --connection-id is "\D_(\d+)$". That finds tables with a series of numbers at the end, preceded by an underscore and some non-number character (the latter criterion prevents me from examining tables with a date at the end, which people tend to do: baron_scratch_2007_05_07 for example). It's better to keep the scratch tables separate of course.
If you do this, make sure the user mk-find runs as has the PROCESS privilege! Otherwise it will only see connections from the same user, and might think some tables are ready to remove when they're still in use. For safety, mk-find checks this for you.
See also --server-id.
--createoptsβ type: string; group: Tests. Table create option matches pattern. This tests theCreate_optionscolumn.--ctimeβ type: size; group: Tests. Table was created n days ago. This tests theCreate_timecolumn.--datafreeβ type: size; group: Tests.
Table's holding n bytes of free space, bestie β this one checks the Data_free column. And you can pass "NULL" as the size to test where Data_free IS NULL. π
--datasizeβ type: size; group: Tests. Table data is eating up n bytes of space. Tests theData_lengthcolumn. And yes, you can say"NULL"to test whereData_length IS NULL. π--dblikeβ type: string; group: Tests. Database name matches a SQL LIKE pattern. lowkey iconic.--dbregexβ type: string; group: Tests. Database name matches this pattern (regex girlies rise π§).--emptyβ group: Tests. Table has no rows, zero, nada. Tests theRowscolumn.--engineβ type: string; group: Tests. Table storage engine matches this pattern. Tests theEnginecolumn β or, in earlier versions of MySQL, theTypecolumn. slay.--functionβ type: string; group: Tests. Function definition matches pattern.--indexsizeβ type: size; group: Tests. Table indexes use n bytes of space. Tests theIndex_lengthcolumn."NULL"is valid here too, to test whereIndex_length IS NULL.--kminβ type: size; group: Tests. Table was checked n minutes ago. Tests theCheck_timecolumn.--ktimeβ type: size; group: Tests. Table was checked n days ago. Tests theCheck_timecolumn.--mminβ type: size; group: Tests. Table was last modified n minutes ago. Tests theUpdate_timecolumn.--mtimeβ type: size; group: Tests. Table was last modified n days ago. Tests theUpdate_timecolumn.--procedureβ type: string; group: Tests. Procedure definition matches pattern.--rowformatβ type: string; group: Tests. Table row format matches pattern. Tests theRow_formatcolumn.--rowsβ type: size; group: Tests. Table has n rows. Tests theRowscolumn."NULL"is allowed here to test whereRows IS NULL.--server-idβ type: string; group: Tests. Table name contains the server ID. Okay so here's the tea β: if you make temporary tables using the naming convention explained in--connection-id, but you also drop the server ID of the server where those tables live into the name, then this pattern match lets you make sure tables get dropped only on the server they were created on. That means a table can't get accidentally dropped on a slave while it's still in use β provided all your server IDs are unique, which, fr fr, they should be for replication to work.
Example time, because this one's a whole vibe. On the master (server ID 22) you create a table called scratch_table_22_12345. Now say you spot that same table on the slave (server ID 23) β you might think "oh it's safe to drop, there's no connection 12345". π But if you also force the name to match the server ID with --server-id '\D_(\d+)_\d+$', that table will not get dropped on the slave. chef's kiss. π
--tablesizeβ type: size; group: Tests. Table uses n bytes of space. Tests the sum of theData_lengthandIndex_lengthcolumns.--tbllikeβ type: string; group: Tests. Table name matches a SQL LIKE pattern.--tblregexβ type: string; group: Tests. Table name matches this pattern.--tblversionβ type: size; group: Tests. Table version is n. Tests theVersioncolumn.--triggerβ type: string; group: Tests. Trigger action statement matches pattern.--trigger-tableβ type: string; group: Tests.--triggeris defined on a table matching this pattern.--viewβ type: string; group: Tests.CREATE VIEWmatches this pattern.
π¬ ACTIONS π¬
Okay so the --exec-plus action runs after everything else, but other than that? Actions happen in an indeterminate order. It's giving chaos. If you need determinism, file a bug report and the author will add this feature. π
--execβ type: string; group: Actions. Execute this SQL with each item found. The SQL can contain escapes and formatting directives β go peek at--printffor those. β¨--exec-dsnβ type: string; group: Actions. Specify a DSN in key-value format to use when executing SQL with--execand--exec-plus. Any values you don't specify get inherited from the command-line arguments. no cap.--exec-plusβ type: string; group: Actions. Execute this SQL with all items at once. This option is not like--execβ there are no escaping or formatting directives here. There's only one special placeholder for the list of database and table names:%s. The list of tables found gets joined together with commas and substituted wherever you put%s.
So you might use this, for example, to drop every table you found:
DROP TABLE %s
It's sort of like GNU find's -exec command {} + syntax β only it's not totally cryptic. And it doesn't require writing a command-line parser. we love efficiency. π«Ά
--printβ group: Actions. Print the database and table name, followed by a newline. This is the default action if no other action is specified. the moment of truth. π--printfβ type: string; group: Actions. Print format on the standard output, interpreting'\'escapes and'%'directives. Escapes are backslashed characters, like\nand\t. Perl interprets these, so literally any escape Perl knows about is fair game. Directives get replaced by%s, and as of this writing you can't add special formatting instructions like field widths or alignment β though the author is musing over ways to do that. π
Here's the whole directive list, bestie. Note that most of them just come straight from columns of SHOW TABLE STATUS. If the column is NULL or doesn't exist, you get an empty string in the output. And a % character followed by anything not in this list is discarded β but the other character still gets printed.
aβAuto_incrementAβAvg_row_lengthcβChecksumCβCreate_timeDβDatabaseβ notes: the database name in which the table livesdβData_lengthEβEngineβ notes: in older versions of MySQL, this isTypeFβData_freefβInnodb_freeβ notes: parsed from theCommentfieldIβIndex_lengthKβCheck_timeLβCollationMβMax_data_lengthNβNameOβCommentPβCreate_optionsRβRow_formatSβRowsTβTable_lengthβ notes:Data_length+Index_lengthUβUpdate_timeVβVersion
π DSN OPTIONS π
These DSN options are used to create a DSN. Each option is given like option=value. Watch out β the options are case-sensitive, so P and p are not the same option. There cannot be whitespace before or after the = and if the value contains whitespace it must be quoted. DSN options are comma-separated. See the maatkit manpage for full details.
Aβ dsn:charset; copy:yes. Default character set.Dβ dsn:database; copy:yes. Default database.Fβ dsn:mysql_read_default_file; copy:yes. Only read default options from the given file.hβ dsn:host; copy:yes. Connect to host.pβ dsn:password; copy:yes. Password to use when connecting.Pβ dsn:port; copy:yes. Port number to use for connection.Sβ dsn:mysql_socket; copy:yes. Socket file to use for connection.uβ dsn:user; copy:yes. User for login if not current user.
π¦ DOWNLOADING π¦
You can download Maatkit from Google Code at http://code.google.com/p/maatkit/, or you can grab any of the tools super easily with a command like this:
wget http://www.maatkit.org/get/toolname
or
wget http://www.maatkit.org/trunk/toolname
Where toolname can be replaced with the name (or fragment of a name) of any of the Maatkit tools. Once downloaded, they're ready to run β no installation needed, we love a plug-and-play moment. π
The first URL gets the latest released version of the tool, and the second gets the latest trunk code from Subversion.
πΈ ENVIRONMENT πΈ
The environment variable MKDEBUG enables verbose debugging output in all of the Maatkit tools:
MKDEBUG=1 mk-....
β SYSTEM REQUIREMENTS β
You need these Perl modules: DBI and DBD::mysql.
π BUGS π
For a list of known bugs, peek at http://www.maatkit.org/bugs/mk-find.
Please use Google Code Issues and Groups to report bugs or request support: http://code.google.com/p/maatkit/. You can also join #maatkit on Freenode to discuss Maatkit. π«Ά
Please include the complete command-line you used to reproduce the problem you're seeing, the version of all MySQL servers involved, the complete output of the tool when run with --version, and if possible, debugging output produced by running with the MKDEBUG=1 environment variable. give us everything, no gatekeeping. π
π COPYRIGHT, LICENSE AND WARRANTY π
This program is copyright 2007-2011 Baron Schwartz. Feedback and improvements are welcome (see BUGS).
THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2; OR the Perl Artistic License. On UNIX and similar systems, you can issue man perlgpl or man perlartistic to read these licenses.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
β¨ AUTHOR β¨
Baron Schwartz
π ABOUT MAATKIT π
This tool is part of Maatkit, a toolkit for power users of MySQL. Maatkit was created by Baron Schwartz; Baron and Daniel Nichter are the primary code contributors. Both are employed by Percona. Financial support for Maatkit development is primarily provided by Percona and its clients. iconic, honestly. π
π VERSION π
This manual page documents Ver 0.9.23 Distrib 7540 $Revision: 7477 $.
And that's a wrap, bestie!! π This whole thing lives in perl v5.42.0 land, dated 2025-07-24, and it's the MK-FIND(1) manual page β go forth and find your tables, you absolute legend. ππβ¨
π¦ where this page comes from
- fedora 43: maatkit 7540-39.fc43 Β· GPL-2.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms