Category Archives: MySQL

MySQL Best Data Types For Common Types Of Data

Primary Key : INT UNSIGNED – About 4.3 billion available IDs. Going unsigned gives you the extra 2 billion. Phone Number : VARCHAR – Phone numbers are identifiers that happen to use digits. – See this post. Price : FLOAT … Continue reading

Posted in MySQL | Leave a comment

How To Determine a Good Unique Key

Make the unique key long enough to allow for reasonable duplication of names in the real world For example, for a table of people, a unique key of country/state/city/address might be a good choice.  This allows for two people in … Continue reading

Posted in MySQL | Leave a comment

How To Store Phone Numbers in MySQL

Store phone numbers as strings, not numbers. Phone numbers are identifiers that happen to use digits. Phone numbers starting with zero are valid, but may be interpreted as octal by a programming language. Strip the phone number to only digits … Continue reading

Posted in MySQL | 1 Comment

How To Use MySQL EXPLAIN

This stuff has helped improve many of my queries. The basics: Its bad whenever you have the second table in the explain returning more than 1 row. The driving table is first and all other tables joined to it should … Continue reading

Posted in MySQL | Leave a comment

Directed Acyclic Graph Information

SQL with Trees and Graphs http://www.artfulsoftware.com/mysqlbook/sampler/mysqled1ch20.html MySQL Stored Procedure of Dijkstra’s Algorithm http://www.artfulsoftware.com/infotree/queries.php?&bw=1059#766 http://www.codeproject.com/KB/database/Modeling_DAGs_on_SQL_DBs.aspx More on Nested Tree Model http://dev.mysql.com/tech-resources/articles/hierarchical-data.html http://articles.sitepoint.com/article/hierarchical-data-database/2

Posted in Data Structures, MySQL | Leave a comment

SQL: ORDER BY With Zeroes Last

How to sort values from lowest to highest, but leaving the zeroes last: SELECT foo FROM bar order by foo = 0, foo ASC

Posted in MySQL | Leave a comment

How To Fix "The total number of locks exceeds the lock table size"

This usually happens when you’re doing massive queries on InnoDB tables: ERROR 1206 (HY000): The total number of locks exceeds the lock table size In /etc/my.cnf, do something like this: innodb_buffer_pool_size=524288000 By default, MySQL sets innodb_buffer_pool_size to 8MB. 500 megabytes … Continue reading

Posted in Linux, MySQL | Leave a comment

How To Add a MySQL Column with Index

ALTER TABLE mytable ADD mycolumn INT UNSIGNED NOT NULL AFTER othercolumn , ADD INDEX ( mycolumn );

Posted in MySQL | Tagged | Leave a comment

MySQL Slow Query Optimization Tips

Slow Query Example SELECT widget_id , widget_name , COUNT(DISTINCT type) AS num_types FROM widget GROUP BY type ORDER BY num_types DESC With lots (>100K) of rows, this query will get slow because it orders on an aggregate COUNT DISTINCT function. … Continue reading

Posted in MySQL | Leave a comment

How to Optimize MySQL Queries Using Indexes

When optimizing a query, the order of fields matter in a multi-field index. In general, you should use the most selective fields first. For example, in a hypothetical location index, use zip, city, state, rather than the reverse. Source: MySQL … Continue reading

Posted in MySQL | Leave a comment

MySQL Schema Best Practices

Use UNSIGNED INT (or BIGINT if needed) for primary key, since you don’t need negative primary keys. Don’t use ON DELETE CASCADE, since you might delete rows you wanted to keep.  Explicitly delete rows in your code. Don’t use ON … Continue reading

Posted in MySQL | 1 Comment

How to Quickly Add Multiple Indexes to a MySQL Table

Instead of: ALTER TABLE my_table ADD KEY `key1` (`apple`); ALTER TABLE my_table ADD KEY `key2` (`banana`); do this: ALTER TABLE my_table ADD KEY `key1` (`apple`), ADD KEY `key2` (`banana`);

Posted in MySQL | Leave a comment

Munin MySQL plugin not working or blank graphs

Add a user “nobody” only accessible via “localhost” with no password and only “usage” privileges. If you look at the MySQL log, you will see the munin client accessing MySQL via the “nobody” user. To test the munin client, connect … Continue reading

Posted in Linux, Munin, MySQL | Leave a comment

How to move a column in MySQL

Move a column after some other column mysql> ALTER TABLE mytable MODIFY COLUMN mycolumn INT AFTER someothercolumn; Move a column to the first position mysql> ALTER TABLE mytable MODIFY COLUMN mycolumn INT FIRST;

Posted in MySQL | 1 Comment

Troubleshooting a MySQL query

Use EXPLAIN to show which indexes are being used. mysql> EXPLAIN SELECT mycolumns FROM table1 LEFT JOIN table2;\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: ag type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 1010 … Continue reading

Posted in MySQL | Leave a comment