<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MySQL Fanboy &#187; Tips</title>
	<atom:link href="/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlfanboy.com</link>
	<description>Almost crazy about Opensource / Free  information.</description>
	<lastBuildDate>Tue, 09 Aug 2011 17:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>What is this MySQL file used for?</title>
		<link>http://www.mysqlfanboy.com/2011/02/what-is-this-mysql-file-used-for/</link>
		<comments>http://www.mysqlfanboy.com/2011/02/what-is-this-mysql-file-used-for/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 00:25:42 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[MyISAM]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Table]]></category>
		<category><![CDATA[Tables]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=516</guid>
		<description><![CDATA[MySQL keeps many different files, some contain real data, some contain meta data. Witch ones are important? Witch can your throw away? This is my attempt to create a quick reference of all the files used by MySQL, whats in them, what can you do if they are missing, what can you do with them. [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL  keeps many different files, some contain real data, some contain meta  data. Witch ones are important? Witch can your throw away?</p>
<p>This  is my attempt to create a quick reference of all the files used by  MySQL, whats in them, what can you do if they are missing, what can you  do with them.</p>
<p>When I was working for Dell doing Linux support my first words to a customer where “DO YOU HAVE COMPLETE AND VERIFIED BACKUP?” Make one now before you think about doing anything I suggest here.</p>
<p>You should always try to manage your data through a MySQL client.   If things have gone very bad this may not be possible. MySQL may not  start. If your file system get corrupt you may have missing files.  Sometimes people create other files in the MySQL directory (BAD).  This  should help you understand what is safe to remove.</p>
<p><span id="more-516"></span></p>
<p>Before you try to work with one of these files make sure you have the file permissions set correctly.</p>
<p>This  may not be a complete list of files used my MySQL.  It most certainly  doesn’t describe everything each table is used for. If you know of ways  to replace a missing file or what happens to MySQL when a file is  missing that I haven’t described here, please leave me a comment or  email me.  I’ll update this document and give your a reference.</p>
<h3>my.cnf</h3>
<p>This file alters the default configuration settings. MySQL looks in the /etc directory for my.cnf. You should review this file  to insure you are looking in the right place for all other MySQL files.   MySQL WILL run without it.  If you have trouble getting MySQL to  start, read the error log then try moving are renaming this file.</p>
<p>There are other places MySQL looks for configuration files.</p>
<ul>
<li>
<pre>/etc/my.cnf</pre>
</li>
<li>
<pre>/etc/mysql/my.cnf</pre>
</li>
<li>
<pre>~/my.cnf</pre>
</li>
<li>
<pre>&lt;$MYSQLHOME&gt;/my.cnf</pre>
</li>
<li>
<pre>~/.my.cnf</pre>
</li>
</ul>
<p>MySQL will use only one configuration file if you use <a href="http://dev.mysql.com/doc/refman/5.1/en/option-files.html">DEFAULTS-FILE</a> = /etc/foo.bar.</p>
<h3>mysql &lt;directory&gt;</h3>
<p>This directory contains all your data.  This may not be its name.  On  Linux servers the default location for MySQL files is /var/lib/mysql.   This directory is controlled by the “datadir” variable in your my.cnf file.</p>
<p>Do I need to say, deleting this directory deletes everything?</p>
<h3>mysql/mysql&lt;directory&gt;</h3>
<p>This directory is MySQL&#8217;s database and is inside the directory containing all your data (see above).  All the files in this directory are MyISAM tables for MySQL information like table and column privilages, users, functions and more.  MySQL will not run without this this database.</p>
<p>If you delete this directory, and all the files/tables within, you can recreate a blank set with the MySQL install utility.</p>
<pre style="padding-left: 30px;">mysql_install_db --user=mysql --datadir=/var/lib/mysql</pre>
<p>After running this MySQL will rediscover any database that still exist (directory in the MySQL directory).  You will however have to recreate all users, permissions, functions, events, processes, and maybe more.</p>
<h3>ibdata1</h3>
<p>If your remove this file your InnoDB DATA IS GONE and MySQL will recreate an empty file.</p>
<p>If  you are not using the innodb_file_per_table option (default), this file  holds almost ALL of your data in InnoDB tables.  This file is all but  useless without its corresponding  ‘.frm’ file for each table in the  right database directory. If all you have is the .frm files you can  recreate the structure of your tables. (See below.)</p>
<p>Idbdata1  can get really big. The default size is 10MB. MySQL will automatically  extended it by the default size as needed. If MySQL crashed, some of  your InnoDB data may be in your transaction logs (ib_logfile.*).</p>
<p>By design the InnoDB file does not shrink. The <a href="http://vdachev.net/2007/02/22/mysql-reducing-ibdata1/">safest way to shrink</a> this file is to take a complete backup, stop MySQL, remove the ib* files, start MySQL and restore all your data.  REALLY.</p>
<h3>DatabaseName &lt;directory in mysql&gt;</h3>
<p>Each  MySQL database has a directory named after it.  Each directory holds  the meta data for the database. Your MyISAM data is in this directory.  InnoDB tables may be here if the innodb_file_per_table variable is used.  By default InnoDB tables are in the ibdata1 file (see above).</p>
<p>If  you delete the directory your data may be gone. MyISAM data WILL be  lost.  InnoDB tables may still be in the ibdata1.  If so, you will need  to recreate the meta data files to recover your data.</p>
<p>Creating  a directory is almost equivalent to ‘create database’.  If a directory  exists MySQL will show you have a database by that name. The create  database command may also creates a db.opt file.</p>
<h3>&lt;TableName&gt;.frm</h3>
<p>This file is key  to both InnoDB and MyISAM databases. It is the meta data to the  location of your data. It contains the table column definitions.</p>
<p>If  you remove this file MySQL will tell you your DATA doesn’t exist.  It  does. Your data is still in the ibdata (.ibd) file or the ibdata1 file.  You need to recreate the table to recreate this file. If you don’t know  the exact structure of this table your out of luck.</p>
<p>Stop  MySQL and move the .MYD and .MYI files to another directory.  (You  might also make a backup copy.) Start MySQL and recreate this table.  Stop MySQL and copy the .MYD and .MYI files back to the database  directory and restart MySQL.</p>
<h3>&lt;TableName&gt;.MYD</h3>
<p>THIS IS YOUR MyISAM DATA.  If this is all you have, and you know the data structure of the the  table, all is not lost.  (See .frm Above.)  You may also need to  recreate the .MYI index file.</p>
<h3>&lt;TableName&gt;.MYI</h3>
<p>This  file contains the indexs for your table.  If it becomes corrupt or is  deleted you can recreate it using the ‘REPAIR TABLE table_name USE_FRM;’  command.</p>
<h3>/tmp/#&lt;TableName&gt;.[ frm MYD MYI ]</h3>
<p>MySQL creates temporary files in the /tmp director (C:\windows\temp) when sorting (<span style="color: #339966;"><code>ORDER BY</code></span> or <span style="color: #339966;"><code>GROUP           BY</code></span>).  It also creates temp files on a replication server when  the <span style="color: #339966;">LOAD DATA LOCAL INFILE&#8230;</span> command is used. Replication will fail if these files are removed.</p>
<h3>&lt;TableName&gt;.ibd</h3>
<p>THIS IS YOUR InnoDB DATA. If  this is all you have, and you know the data structure of the the table,  all is not lost.  (See .frm Above.) Unlike MyISAM tables the indexes  are contained in this file with your data.</p>
<p>MySQL doesn’t create this file unless you are using the innodb_file_per_table option.</p>
<p>If you have a &#8220;Clean&#8221; .ibd file you can import it into a database with the command &#8220;ALTER TABLE <em><code>tbl_name</code></em> IMPORT TABLESPACE;&#8221;</p>
<h3>&lt;TableName&gt;.CSV</h3>
<p>THIS IS YOUR CSV DATA.  This file contains comma separated text data. These file do not have indexes.</p>
<h3>&lt;TableName&gt;.CSM</h3>
<p>This  file contains meta data for CSV and archive tables.  I have not found  what is stored here. I do know it tells MySQL if you are logging to the  general logs.</p>
<h3>ib_logfile*</h3>
<p>This file contains your un-committed transactions data. MySQL uses it to recover from a crash.</p>
<p>If you shut down InnoDB cleanly, you can remove them. MySQL will recreate them.</p>
<p>If  you change the size of innodb_log_file_size, you will need to recreate  these files by stopping MySQL cleanly and deleting them.</p>
<h3>mysql-bin.*</h3>
<p>This “<a href="http://forge.mysql.com/wiki/MySQL_Internals_Binary_Log#High-Level_Binary_Log_Structure_and_Contents">Bin Log</a>”  files contain any change made to any database. Each transaction is  assigned a MASTER_LOG_POS(ion).  These files are not created by default.  They are used for <a href="http://dev.mysql.com/doc/refman/5.1/en/replication-howto.html">replication</a> and point-in-time recovery.</p>
<p>You  can stop the server and remove these files IF you remove the  mysql-bin.index file as well.  MySQL creates a new bin log file each  time it starts or the logs are flushed.  Deleting these files will the  server is running will break replication.</p>
<h3>mysql-bin.index</h3>
<p>This file is used by MySQL to keep a Bin Log list.  It is a simple text file like;</p>
<p style="padding-left: 30px;">./mysql-bin.000001</p>
<p style="padding-left: 30px;">./mysql-bin.000002</p>
<p style="padding-left: 30px;">./mysql-bin.000003</p>
<p>If  you remove this file, MySQL will recreate it with only the newest bin  log name. If you need to remove old bin logs use the command “purge binary logs [to mysql-bin.######] [before “yyyy-mm-dd”]”.</p>
<p>You can control the number of bin logs using the expire_logs_days variable.</p>
<h3>mysqld.log</h3>
<p>This is MySQL’s primary <a href="http://dev.mysql.com/doc/refman/5.1/en/log-file-maintenance.html">administration log</a>.  MySQL reports starts and stops as well as some warning and errors in  this file.  If MySQL crashes, mysqld_safe will restart it. This log will  report this.</p>
<p>You can delete this file if needed.</p>
<h3>slow.log</h3>
<p>The <a href="http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html">slow query log</a> consists of all SQL statements that took more than<a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_long_query_time"> long_query_time</a> seconds to execute and (as of MySQL 5.1.21) required at least<a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_min_examined_row_limit"> min_examined_row_limit</a> rows to be examined.</p>
<p>You can delete this file if needed.</p>
<h3>db.opt</h3>
<p>Database  characteristics, like the CHARACTER SET clause are stored in the db.opt  file. You may have strange query results if this file is missing. MySQL  will use it’s default. You can recreate this file my altering the table  with the correct settings.</p>
<h3>mysql.pid</h3>
<p>The <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-server.html">PID file</a> hold the process ID number for the running server. MySQL creates this  file and scripts that start and stop MySQL use it to control MySQL.</p>
<p>MySQL  will remove this file when is stops. You should not delete it if MySQL  is running. If mysql is NOT running and the file exists MySQL may have  crashed and you should delete this file.</p>
<h3>master.info<br />
relay-log.info</h3>
<p>These are found only on the slave servers. Slaves read the commands from those files in order to replicate what the master has done. Simularly to the bin log, it will create files with numbered extensions as needed.</p>
<p>Master.info is a simple text file. This is a store of the master command is used by mysql when it restarts to make the slave connection.</p>
<pre style="padding-left: 30px;">15
mysql-bin.006004
1006945586
192.168.1.2
repl_user
P@sSw0Rd
3306
60
0</pre>
<p>Removing these files will breat replication and / or through your slave out of sync.</p>
<h3>*relay-bin.index</h3>
<p>The &#8220;*&#8221; is either the hostname or the same as the .PID file.  This file is the index for the relay log used in replication.  Relay bin files are found only on slave servers.</p>
<h3>&lt;TableName&gt;.TRG</h3>
<p>Contains the list of triggers belonging to a given table</p>
<h3>&lt;TrigerName&gt;.TRN</h3>
<p>Contains some metadata on the trigger</p>
<h3>&lt;TableName&gt;.ARZ</h3>
<p>It’s the file containing the data for an ARCHIVE table.</p>
<h3>&lt;TableName&gt;.par</h3>
<p>Contains the definition of a partition</p>
<p>tablename#P#partitionname.{MYD,ARZ,ibd}<br />
contains the data related to a given partition</p>
<p><img class="alignnone" src="http://mark.grennan.com/images/MarkGrennanSigniture.bmp" alt="" width="166" height="69" /></p>
<h6>References:</h6>
<p>FULL DISK<br />
<a href="http://dev.mysql.com/doc/refman/5.1/en/full-disk.html">http://dev.mysql.com/doc/refman/5.1/en/full-disk.html</a></p>
<p>MySQL Database Backup .MYI and .MYD<br />
<a href="http://www.aeonity.com/frost/mysql-database-backup-myi-myd">http://www.aeonity.com/frost/mysql-database-backup-myi-myd</a></p>
<p>Recovering from Crashes<br />
<a href="http://dev.mysql.com/tech-resources/articles/recovering-from-crashes.html">http://dev.mysql.com/tech-resources/articles/recovering-from-crashes.html</a></p>
<p>Using Per-Table Tablespaces<a href="http://dev.mysql.com/doc/refman/5.0/en/innodb-multiple-tablespaces.html"></p>
<p>http://dev.mysql.com/doc/refman/5.0/en/innodb-multiple-tablespaces.html</a></p>
<p>Troubleshooting <code>InnoDB</code> Data Dictionary Operations<br />
<a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-troubleshooting-datadict.html">http://dev.mysql.com/doc/refman/5.1/en/innodb-troubleshooting-datadict.html</a></p>
<p><cite><strong>Giuseppe Maxia</strong> &#8211; who writes a great blog about mysql at<a href="http://datacharmer.blogspot.com/"> </a></cite><a href="http://datacharmer.blogspot.com/">http://datacharmer.blogspot.com/</a> contributed several files I missed.<a rel="external nofollow" href="http://datacharmer.blogspot.com/"><br />
</a></p>
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2011%2F02%2Fwhat-is-this-mysql-file-used-for%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=What is this MySQL file used for? - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2011/02/what-is-this-mysql-file-used-for/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>It&#8217;s about Time.</title>
		<link>http://www.mysqlfanboy.com/2011/01/its_about_time/</link>
		<comments>http://www.mysqlfanboy.com/2011/01/its_about_time/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 21:08:27 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Commentary]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Tunning]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=301</guid>
		<description><![CDATA[WHAT TIME IS IT? This post started with a simple question: &#8220;Does the function NOW() get executed every time a row is examined?&#8221; According to the manual,  &#8220;Functions that return the current date or time each are evaluated only once per query &#8230;. multiple references to a function such as NOW() &#8230; produce the same [...]]]></description>
			<content:encoded><![CDATA[<h3>WHAT TIME IS IT?</h3>
<p>This post started with a simple question: &#8220;Does the function NOW() get executed every time a row is examined?&#8221; According to the manual,  <em>&#8220;Functions  that return the current date or time each are evaluated only once per  query &#8230;. multiple references to a function such as NOW() &#8230; produce  the same result. &#8230;. (However,) as of MySQL 5.0.12, SYSDATE()  returns  the time (the row is) executes. &#8220;</em></p>
<ul>
<li><strong>CURDATE()</strong> returns the current date.</li>
<li><strong>CURTIME()</strong> returns the current time.</li>
<li><strong>UTC_DATE()</strong> returns the current UTC date.</li>
<li><strong>UTC_TIME()</strong> returns the current UTC time.</li>
<li><strong>NOW()</strong> return the current date and time.</li>
<li><strong>UTC_TIMESTAMP()</strong> returns the current UTC date and time.</li>
<li><strong>SYSDATE()</strong> returns the date and time at which the function executes.</li>
<li><strong>LOCALTIME(), LOCALTIME(), LOCALTIMESTAMP(), LOCALTIMESTAMP()</strong> are synonyms for NOW().</li>
</ul>
<p>All these functions return some value of a date / time. For example, WEEK() returns the week number for <em>date</em>.</p>
<p>Here is an example of a common query.</p>
<pre style="padding-left: 30px;">SELECT  stn, temp, lat, lon WHERE valid_time &gt; NOW() – INTERVAL 1 HOUR;</pre>
<p>This is the right way to make this query because NOW() is only executed once.  If  SYSDATE() was used, each row selected would call SYSDATE().</p>
<p>So, if NOW() is only executed once and SYSDATE() is executed every time, we should see this difference if we BENCHMARK these functions.</p>
<pre style="padding-left: 30px;">mysql&gt; SELECT BENCHMARK( 10000000, NOW() );
+------------------------------+
| BENCHMARK( 10000000, NOW() ) |
+------------------------------+
|                            0 |
+------------------------------+
1 row in set (<strong><span style="color: #ff0000;">0.07 sec</span></strong>)

+----------------------------------+
| BENCHMARK( 10000000, SYSDATE() ) |
+----------------------------------+
|                                0 |
+----------------------------------+
1 row in set (<strong><span style="color: #ff0000;">5.49 sec</span></strong>)</pre>
<p>And it seems to be true.  But what about real SQL statements. Will this hold up? Here is a simple table.</p>
<pre style="padding-left: 30px;">CREATE TABLE `TableName1` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1</pre>
<p>I used this SQL statement to put records in the table. It adds a new records with a new date/time with each query. Like:</p>
<pre style="padding-left: 30px;">INSERT INTO `stage`.`TableName1`(`id`,`dtime`) VALUES ( NULL,NOW())</pre>
<p>What if we then query the table every second.  Will NOW() be processed as a part of the results?  I used the SLEEP() function to delay each record.</p>
<pre style="padding-left: 30px;">SELECT id, dtime, (SELECT NOW()) FROM TableName1 WHERE NOT SLEEP(1);</pre>
<p>This query took ten seconds to run for a table with ten rows and returns these results.</p>
<pre style="padding-left: 30px;">id	dtime	                (SELECT NOW())
 1	2010-08-06 15:55:57	2010-08-06 16:05:06
 2	2010-08-06 15:56:12	2010-08-06 16:05:06
 3	2010-08-06 15:56:16	2010-08-06 16:05:06
 4	2010-08-06 15:56:20	2010-08-06 16:05:06
 5	2010-08-06 15:56:30	2010-08-06 16:05:06
 6	2010-08-06 16:48:23	2010-08-06 16:05:06
 7	2010-08-06 16:48:26	2010-08-06 16:05:06
 8	2010-08-06 16:48:27	2010-08-06 16:05:06
 9	2010-08-06 16:48:28	2010-08-06 16:05:06
 10	2010-08-06 16:48:29	2010-08-06 16:05:06</pre>
<p>As the documentation states, the NOW() is NOT processes every time.</p>
<p>What if  NOW() is a part of a JOIN?</p>
<pre style="padding-left: 30px;">SELECT t.id, t.dtime, d.n  FROM TableName1 AS t, (SELECT 1, NOW() AS n) AS d  WHERE NOT SLEEP(1);</pre>
<p>Again, this SELECT takes ten seconds to run for a table with ten rows.</p>
<pre style="padding-left: 30px;">id	dtime                 	n
 1	2010-08-06 15:55:57	2010-08-06 17:09:00
 2	2010-08-06 15:56:12	2010-08-06 17:09:00
 3	2010-08-06 15:56:16	2010-08-06 17:09:00
 4	2010-08-06 15:56:20	2010-08-06 17:09:00
 5	2010-08-06 15:56:30	2010-08-06 17:09:00
 6	2010-08-06 16:48:23	2010-08-06 17:09:00
 7	2010-08-06 16:48:26	2010-08-06 17:09:00
 8	2010-08-06 16:48:27	2010-08-06 17:09:00
 9	2010-08-06 16:48:28	2010-08-06 17:09:00
 10	2010-08-06 16:48:29	2010-08-06 17:09:00</pre>
<p>And again the NOW() function is only executed once.</p>
<p>The WHERE clause is harder to test. I inserted a row in the table only seconds apart. I then quickly ran the SELECT again to see if the NOW() matched the times in the table.</p>
<pre style="padding-left: 30px;">SELECT t.id, t.dtime FROM TableName1 AS t WHERE NOT SLEEP(1) AND dtime = (SELECT NOW() - INTERVAL 15 SECOND)  ;</pre>
<p>Here is the data table.</p>
<pre style="padding-left: 30px;">id	dtime
 1	2010-08-06 17:35:00
 2	2010-08-06 17:35:00
 3	2010-08-06 17:35:01
 4	2010-08-06 17:35:03
 5	2010-08-06 17:35:04
 6	2010-08-06 17:35:04
 7	2010-08-06 17:35:04
 8	2010-08-06 17:35:05
 9	2010-08-06 17:35:05
 10	2010-08-06 17:35:05
 11	2010-08-06 17:35:06
 12	2010-08-06 17:35:06
 13	2010-08-06 17:35:06
 14	2010-08-06 17:35:07
 15	2010-08-06 17:35:07
 16	2010-08-06 17:35:07
 17	2010-08-06 17:35:08
 18	2010-08-06 17:35:08
 19	2010-08-06 17:35:08
 20	2010-08-06 17:35:09
 21	2010-08-06 17:35:09
 22	2010-08-06 17:35:09
 23	2010-08-06 17:35:09
 24	2010-08-06 17:35:10
 25	2010-08-06 17:35:10</pre>
<p>The select statement above only took three seconds to run and returned three rows all the same.</p>
<pre style="padding-left: 30px;"> id	dtime
 11	2010-08-06 17:35:06
 12	2010-08-06 17:35:06
 13	2010-08-06 17:35:06</pre>
<p>MySQL is evaluating dtime = (SELECT NOW() &#8211; INTERVAL 15 SECOND) before the SLEEP(1).   MySQL only ran the SLEEP(1) for each record that matched.  I reversed the two parts of the AND and   the order of the didn&#8217;t matter.</p>
<p>Next I tried these functions with SYSDATE().  I found it is executed every time it is evaluated.  This SELECT took 17 seconds and return a different time for each.</p>
<pre style="padding-left: 30px;">SELECT id, dtime, (SELECT SYSDATE()) FROM TableName1 WHERE NOT SLEEP(1);</pre>
<pre style="padding-left: 30px;">id    dtime                 (SELECT SYSDATE())
1    2010-08-06 18:28:42    2010-08-06 19:00:57
2    2010-08-06 18:28:42    2010-08-06 19:00:58
3    2010-08-06 18:28:42    2010-08-06 19:00:59
4    2010-08-06 18:28:43    2010-08-06 19:01:00
5    2010-08-06 18:28:43    2010-08-06 19:01:01
6    2010-08-06 18:28:43    2010-08-06 19:01:02
7    2010-08-06 18:28:43    2010-08-06 19:01:03
8    2010-08-06 18:28:44    2010-08-06 19:01:04
9    2010-08-06 18:28:44    2010-08-06 19:01:05
10    2010-08-06 18:28:44    2010-08-06 19:01:06
11    2010-08-06 18:28:44    2010-08-06 19:01:07
12    2010-08-06 18:28:45    2010-08-06 19:01:08
13    2010-08-06 18:28:45    2010-08-06 19:01:09
14    2010-08-06 18:28:45    2010-08-06 19:01:10
15    2010-08-06 18:28:45    2010-08-06 19:01:11
16    2010-08-06 18:28:46    2010-08-06 19:01:12
17    2010-08-06 18:28:46    2010-08-06 19:01:13</pre>
<p>The SET TIMESTAMP statement affects the value returned by NOW() but not by SYSDATE(). This means that timestamp settings in the binary log have no effect on invocations of SYSDATE().</p>
<h3>CACHING</h3>
<p>MySQL doesn&#8217;t cache nondeterministic functions like NOW() and SYSDATE().  If your application processes the same data base queries within the same second or if you need the two queries to return the same results, you will need to turn NOW() into a string.</p>
<p style="padding-left: 30px;">SELECT stn, temp, lat, lon WHERE valid_time &gt; ‘2010-05-06 23:20:00’ – INTERVAL 1 HOUR;</p>
<p>Even better, do the math in your program.  Here are some benchmarks show this outcome.</p>
<p style="padding-left: 30px;">SELECT stn, temp, lat, lon WHERE valid_time &gt; ‘2010-05-06 22:20:00’;</p>
<pre style="padding-left: 30px;">+------------------------------------------------------------------------+
| BENCHMARK( 10000000, NOW() -  INTERVAL 1 HOUR = '2011-01-27 19:00:00') |
+------------------------------------------------------------------------+
|                                                                      0 |
+------------------------------------------------------------------------+
1 row in set (<strong><span style="color: #ff0000;">0.36 sec</span></strong>)

mysql&gt; SELECT BENCHMARK( 10000000, SYSDATE() -  INTERVAL 1 HOUR = '2011-01-27 19:00:00');
+----------------------------------------------------------------------------+
| BENCHMARK( 10000000, SYSDATE() -  INTERVAL 1 HOUR = '2011-01-27 19:00:00') |
+----------------------------------------------------------------------------+
|                                                                          0 |
+----------------------------------------------------------------------------+
1 row in set (<strong><span style="color: #ff0000;">2.42 sec</span></strong>)

mysql&gt; SELECT BENCHMARK( 10000000, '2010-05-06 23:20:00' -  INTERVAL 1 HOUR = '2011-01-27 19:00:00');
+----------------------------------------------------------------------------------------+
| BENCHMARK( 10000000, '2010-05-06 23:20:00' -  INTERVAL 1 HOUR = '2011-01-27 19:00:00') |
+----------------------------------------------------------------------------------------+
|                                                                                      0 |
+----------------------------------------------------------------------------------------+
1 row in set (<strong><span style="color: #ff0000;">3.98 sec</span></strong>)

mysql&gt; SELECT BENCHMARK( 10000000, '2010-05-06 23:20:00' = '2011-01-27 19:00:00');
+---------------------------------------------------------------------+
| BENCHMARK( 10000000, '2010-05-06 23:20:00' = '2011-01-27 19:00:00') |
+---------------------------------------------------------------------+
|                                                                   0 |
+---------------------------------------------------------------------+
1 row in set (<strong><span style="color: #ff0000;">0.22 sec</span></strong>)</pre>
<p>REFERENCE:</p>
<p>http://dev.mysql.com/doc/refman/5.0/en/functions.html</p>
<p>http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html</p>
<p><img class="alignnone" src="http://mark.grennan.com/images/MarkGrennanSigniture.bmp" alt="" width="166" height="69" /></p>
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2011%2F01%2Fits_about_time%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=It's about Time. - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2011/01/its_about_time/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MySQL GIS &#8211; Part 1</title>
		<link>http://www.mysqlfanboy.com/2010/08/mysql-gis-part-1/</link>
		<comments>http://www.mysqlfanboy.com/2010/08/mysql-gis-part-1/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 21:49:55 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[GIS]]></category>
		<category><![CDATA[HOW TO]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[GEO]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SHP]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=314</guid>
		<description><![CDATA[In my business (weather) we use lots map based (Geo) information.  Almost every table has latitude and longitude. Working with this kind of data can be exciting and frustrating.  This should give you a quick start into GIS with MySQL. &#8220;A geographic information system (GIS), or geographical information system, is any system that captures, stores, [...]]]></description>
			<content:encoded><![CDATA[<p>In my business (weather) we use lots map based (Geo) information.   Almost every table has latitude and longitude. Working with this kind of  data can be exciting and frustrating.  This should give you a quick  start into GIS with MySQL.</p>
<p><em>&#8220;A <strong>geographic information system (GIS)</strong>, or <strong>geographical information system</strong>,   is any system that captures, stores, analyzes, manages, and presents   data that are linked to location. In the simplest terms, GIS is the   merging of cartography, statistical analysis, and database technology.   GIS systems are used in <a title="Cartography" href="https://secure.wikimedia.org/wikipedia/en/wiki/Cartography">cartography</a>, <a title="Remote sensing" href="https://secure.wikimedia.org/wikipedia/en/wiki/Remote_sensing">remote sensing</a>, <a title="Land surveying" href="https://secure.wikimedia.org/wikipedia/en/wiki/Land_surveying">land surveying</a>, <a title="Utility management (page does not exist)" href="https://secure.wikimedia.org/wikipedia/en/w/index.php?title=Utility_management&amp;action=edit&amp;redlink=1">utility management</a>, <a title="Natural resource management" href="https://secure.wikimedia.org/wikipedia/en/wiki/Natural_resource_management">natural resource management</a>, <a title="Photogrammetry" href="https://secure.wikimedia.org/wikipedia/en/wiki/Photogrammetry">photogrammetry</a>, <a title="Geography" href="https://secure.wikimedia.org/wikipedia/en/wiki/Geography">geography</a>, <a title="Urban planning" href="https://secure.wikimedia.org/wikipedia/en/wiki/Urban_planning">urban planning</a>, <a title="Emergency management" href="https://secure.wikimedia.org/wikipedia/en/wiki/Emergency_management">emergency management</a>, <a title="Navigation" href="https://secure.wikimedia.org/wikipedia/en/wiki/Navigation">navigation</a>, and <a title="Local search (Internet)" href="https://secure.wikimedia.org/wikipedia/en/wiki/Local_search_%28Internet%29">localized search engines</a>.&#8221;</em> &#8211; <a href="http://secure.wikimedia.org/wikipedia/en/wiki/Geographic_information_system">Wikipedia</a></p>
<p>GIS / Mapping Systems work with both text data and graphical data.   Applications and utilities often blur the lines between the two types  and make understanding difficult.  Map servers blend raster images, with  point or polygon data, and bitmap images to make complete images to  display in the user&#8217;s client application.  For this post I will  concentrate on the text type &#8220;data&#8221;.  The type we can index in a MySQL  database.</p>
<h2>THE SEARCH</h2>
<p>After months of reading, [1]  I&#8217;m writing this post to describes what  I have learned about how to get started using GEO coding data as  quickly as possible.  I found very little piratical information on GIS  and MySQL.  The <a href="http://dev.mysql.com/doc/refman/5.1/en/spatial-extensions.html">MySQL manual</a> covers the functions but doesn&#8217;t supply much practical information on GEO.  <a href="http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html">Anders Karlsson</a> wrote a nice and short story about GIS that give me a good start.</p>
<p>The best information has be written by Florin Duroiu in his post titled <a rel="bookmark" href="http://blog.newsplore.com/2009/02/22/spincloud-labs-political-boundaries-overlay-in-google-maps-part-1">&#8220;Political boundaries overlay in Google maps&#8221;</a>. A good part of my post is based on his work.</p>
<h2>STEP BY STEP</h2>
<p>Below are the detailed needed to to produce a MySQL database with the Points of Interest (POI).  This is <strong>based on CentOS 5.5 with MySQL 5.1</strong>.</p>
<pre>yum install gdal
mkdir geo
mkdir data
mkdir data/Oklahoma
cd geo/data/Oklahoma
wget http://downloads.cloudmade.com/north_america/united_states/oklahoma/oklahoma.shapefiles.zip
unzip oklahoma.shapefiles.zip
mysql -e 'create database geo'
ogr2ogr -f "MySQL" MySQL:"geo,user=root,host=localhost,password=" -nln oklahoma_poi -lco engine=MYISAM oklahoma_poi.shp
mysql geo -e '<strong>desc oklahoma_poi</strong>'

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| OGR_FID  | int(11)      | NO   | PRI | NULL    | auto_increment |
| SHAPE    | geometry     | NO   | MUL | NULL    |                |
| category | varchar(30)  | YES  |     | NULL    |                |
| name     | varchar(113) | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

mysql geo -e "<strong>select name, category, Y(SHAPE) as lat, X(SHAPE) as lng  from oklahoma_poi where NAME like 'School:Putnam%'</strong>"

+-------------------------------------+--------------------------------+------------+-------------+
| name                                | category                       | lat        | lng         |
+-------------------------------------+--------------------------------+------------+-------------+
| School:Putnam City West High School | Government and Public Services |  35.492557 | -97.6605975 |
| School:Putnam City North School     | Government and Public Services | 35.5892209 | -97.6372648 |
| School:Putnam City School           | Government and Public Services | 35.5122794 | -97.6142079 |
| School:Putnam High School           | Government and Public Services | 35.5214459 | -97.6086523 |
| School:Putnam Heights Academy       | Government and Public Services | 35.5081143 | -97.5397619 |
+-------------------------------------+--------------------------------+------------+-------------+
</pre>
<p>In a coming set of post I&#8217;ll go over:</p>
<ul>
<li>The &#8220;Data&#8221; types your will find and how to convert between them.</li>
<li>What data is available and where can you find it?</li>
<li>More examples on what you can do with GIS data.</li>
<li>Viewing our GIS data.</li>
<li>How to collect your own GIS data.</li>
<li>Good and bad examples of searching GIS data.</li>
<li>Optimizing MySQL GIS.  Is it really worth using?</li>
</ul>
<p><img class="alignnone" src="http://mark.grennan.com/images/MarkGrennanSigniture.bmp" alt="" width="166" height="69" /></p>
<p>[1] <a href="http://www.amazon.com/GIS-Dummies-Michael-N-DeMers/dp/0470236825">Books: GIS for Dummies</a> &#8211; <strong><em>Author:</em></strong><em> Michael N. DeMers &#8211; John Wiley &amp; Sons (2009) &#8211; <strong>ISBN:</strong> 0470236825</em><a title="View this posting." href="http://www.osgeo.org/books/grassbook"><br />
Open Source GIS: A GRASS GIS Approach. Third Edition.</a>-  <strong><em>Author</em>:</strong> <em>Markus Neteler and Helena Mitasova</em><strong> &#8211; ISBN:</strong> <em>978-0-38735767-6</em><a title="View this posting." href="http://www.osgeo.org/books/webmapping"><br />
Web Mapping Illustrated: Using Open Source GIS Toolkits</a> &#8211; <strong><em>Author</em>:</strong> <em>Tyler Mitchell</em><strong> &#8211; ISBN:</strong> <em>9780596008659</em></p>
<p>﻿</p>
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2010%2F08%2Fmysql-gis-part-1%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=MySQL GIS - Part 1 - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2010/08/mysql-gis-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Does Size or Type Matter?</title>
		<link>http://www.mysqlfanboy.com/2010/07/does-size-or-type-matter/</link>
		<comments>http://www.mysqlfanboy.com/2010/07/does-size-or-type-matter/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 18:08:16 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Tunning]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Profilling]]></category>
		<category><![CDATA[Query]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=238</guid>
		<description><![CDATA[MySQL seems to be happy to convert types for you. Developers are rushed to complete their project and if the function works they just move on. But what is the costs of mixing your types? Does it matter if your are running across a million rows or more? Lets find out. Here is what the [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL seems to be happy to convert types for you.  Developers are rushed to complete their project and if the function works they just move on.  But what is the costs of mixing your types?  Does it matter if your are running across a million rows or more?  Lets find out.</p>
<p>Here is what the programmers see.</p>
<pre style="padding-left: 30px;">mysql&gt; select <span style="color: #ff0000;"><strong>1+1</strong></span>;
+-----+
| 1+1 |
+-----+
|   2 |
+-----+
1 row in set (<span style="color: #008000;">0.00 sec</span>)

mysql&gt; select <span style="color: #ff0000;"><strong>"1"+"1"</strong></span>;
+---------+
| "1"+"1" |
+---------+
|       2 |
+---------+
1 row in set (<span style="color: #008000;">0.00 sec</span>)</pre>
<p><strong>Benchmark</strong></p>
<p>What if we do a thousand simple loops?  How long does the looping itself take?<strong> </strong></p>
<p>The <a href="http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_benchmark"><code>BENCHMARK()</code></a> function           executes the expression <em><code>expr</code></em> repeatedly <em><code>count</code></em> times. It may be           used to time how quickly MySQL processes the expression. The           result value is always <code>0</code>.</p>
<pre>
<pre style="padding-left: 30px;">mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;"><strong>1</strong></span>);
+--------------------------+
| benchmark(1000000000, 1) |
+--------------------------+
|                        0 |
+--------------------------+
1 row in set (<span style="color: #008000;">5.42 sec</span>)

mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;"><strong>"1"</strong></span> );
+-----------------------------+
| benchmark(1000000000, "1" ) |
+-----------------------------+
|                           0 |
+-----------------------------+
1 row in set (<span style="color: #008000;">5.40 sec</span>)</pre>
</pre>
<p>So maybe type doesn&#8217;t matter? About five seconds just to loop but the type didn&#8217;t change it.   What if we add <span style="color: #ff0000;"><strong>1+&#8221;1&#8243;</strong></span>?</p>
<p><span id="more-238"></span></p>
<pre style="padding-left: 30px;">mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;"><strong>1+1</strong></span>);
+----------------------------+
| benchmark(1000000000, 1+1) |
+----------------------------+
|                          0 |
+----------------------------+
1 row in set (<span style="color: #008000;">12.65 sec</span>)

mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;"><strong>1+"1"</strong></span>);
+------------------------------+
| benchmark(1000000000, 1+"1") |
+------------------------------+
|                            0 |
+------------------------------+
1 row in set (<span style="color: #008000;">35.58 sec</span>)
<pre>mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;"><strong>"1"+"1"</strong></span>);
+--------------------------------+
| benchmark(1000000000, "1"+"1") |
+--------------------------------+
|                              0 |
+--------------------------------+
1 row in set (<span style="color: #008000;">51.59 sec</span>)</pre>
<p>It looks like type does matter.  But does it always matter?</p>
<pre style="padding-left: 30px;">mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">sum(1+1)</span>);
+---------------------------------+
| benchmark(1000000000, sum(1+1)) |
+---------------------------------+
|                               0 |
+---------------------------------+
1 row in set (<span style="color: #008000;">9.69 sec</span>)

mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">sum("1"+"1")</span>);
+-------------------------------------+
| benchmark(1000000000, sum("1"+"1")) |
+-------------------------------------+
|                                   0 |
+-------------------------------------+
1 row in set (<span style="color: #008000;">9.94 sec</span>)

mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">sum("1.23456789"+"1.23456789")</span>);
+-------------------------------------------------------+
| benchmark(1000000000, sum("1.23456789"+"1.23456789")) |
+-------------------------------------------------------+
|                                                     0 |
+-------------------------------------------------------+
1 row in set (<span style="color: #008000;">10.32 sec</span>)</pre>
<p>So, not all functions are the same.  But it looks like size might matter!</p>
<pre style="padding-left: 30px;">mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">1.1+1.1</span>);
+--------------------------------+
| benchmark(1000000000, 1.1+1.1) |
+--------------------------------+
|                              0 |
+--------------------------------+
1 row in set (<span style="color: #008000;">34.90 sec</span>)

mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">"1.1"+"1.1"</span>);
+------------------------------------+
| benchmark(1000000000, "1.1"+"1.1") |
+------------------------------------+
|                                  0 |
+------------------------------------+
1 row in set (<span style="color: #008000;">1 min 15.32 sec</span>)

mysql&gt; select  benchmark(1000000000, <span style="color: #ff0000;">"1.123456789"+"1.123456789"</span>);
+----------------------------------------------------+
| benchmark(1000000000, "1.123456789"+"1.123456789") |
+----------------------------------------------------+
|                                                  0 |
+----------------------------------------------------+
1 row in set (<span style="color: #008000;">1 min 53.32 sec</span>)</pre>
<p>Sorry.  Looks like size does matter.<br />
This doesn't seem logical.</p>
<pre>mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">1=1</span>);
+----------------------------+
| benchmark(1000000000, 1=1) |
+----------------------------+
|                          0 |
+----------------------------+
1 row in set (<span style="color: #008000;">12.75 sec</span>)

mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">1="1"</span>);
+------------------------------+
| benchmark(1000000000, 1="1") |
+------------------------------+
|                            0 |
+------------------------------+
1 row in set (<span style="color: #008000;">40.78 sec</span>)
mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">1=true</span>);
+-------------------------------+
| benchmark(1000000000, 1=true) |
+-------------------------------+
|                             0 |
+-------------------------------+
1 row in set (<span style="color: #008000;">12.73 sec</span>)

mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">1="true"</span>);
+---------------------------------+
| benchmark(1000000000, 1="true") |
+---------------------------------+
|                               0 |
+---------------------------------+
1 row in set, <span style="color: #ff0000;">65535 warnings</span> (<span style="color: #008000;">3 min 5.72 sec</span>)
mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">"true"="true"</span>);
+--------------------------------------+
| benchmark(1000000000, "true"="true") |
+--------------------------------------+
|                                    0 |
+--------------------------------------+
1 row in set (<span style="color: #008000;">57.25 sec</span>)</pre>
<p>Maybe we should CAST our work?</p>
<pre style="padding-left: 30px;">mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">cast("1" as unsigned)</span>);
+----------------------------------------------+
| benchmark(1000000000, cast("1" as unsigned)) |
+----------------------------------------------+
|                                            0 |
+----------------------------------------------+
1 row in set (<span style="color: #008000;">32.27 sec</span>)

mysql&gt; select benchmark(1000000000, <span style="color: #ff0000;">cast("1" as unsigned) + cast("1" as unsigned)</span>);
+----------------------------------------------------------------------+
| benchmark(1000000000, cast("1" as unsigned) + cast("1" as unsigned)) |
+----------------------------------------------------------------------+
|                                                                    0 |
+----------------------------------------------------------------------+
1 row in set (<span style="color: #008000;">1 min 7.24 sec</span>)</pre>
</pre>
<p>Maybe not!<br />
Conclusion:  Be careful with your data types.  If you are taking user input, do the type conversion ONCE in your program.  Don&#8217;t let MySQL do the type conversions for you.<br />
query = &#8220;SELECT * FROM table where $INPUT = 1&#8243;;   could be doing your wrong.<br />
<img class="alignnone" src="http://mark.grennan.com/images/MarkGrennanSigniture.bmp" alt="" width="166" height="69" /></p>
<pre>References:

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_benchmark

http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html

http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html</pre>
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2010%2F07%2Fdoes-size-or-type-matter%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=Does Size or Type Matter? - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2010/07/does-size-or-type-matter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Network Connections</title>
		<link>http://www.mysqlfanboy.com/2010/05/mysql-network-connections/</link>
		<comments>http://www.mysqlfanboy.com/2010/05/mysql-network-connections/#comments</comments>
		<pubDate>Fri, 07 May 2010 20:59:39 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Tunning]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Settings]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=139</guid>
		<description><![CDATA[If your MySQL server has hundreds of clients (applications) and tens of thousands of queries per second,  MySQL default network settings may NOT be for you.  Network performance is not often a significant factor in the performance of MySQL.  That said, there are things to consider. If you are building new applications make these changes [...]]]></description>
			<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/05/small-Clipboard01.jpg"><img class="alignright size-full wp-image-208" title="small-Clipboard01" src="/wp-content/uploads/2010/05/small-Clipboard01.jpg" alt="" width="256" height="170" /></a>If your MySQL server has hundreds of clients (applications) and tens  of thousands of queries per second,  MySQL default network settings may <strong>NOT </strong>be for you.  Network performance is not often a significant factor  in the performance of MySQL.  That said, there are things to consider.</p>
<p>If you are building new applications make these changes now.   Developer expectations are hard to change.  My example below will break  your application if  developers open a database connections and then  spend ten minutes playing with their play doe before making a query.  If  you have working applications make changes slowly.</p>
<p><a href="http://en.wikipedia.org/wiki/Middleware">Middleware</a> provides database <a href="http://en.wikipedia.org/wiki/Connection_pool">connections  pooling</a>. This allows database connections to be used by multiple  applications. Any program that runs as a deamon or service can use  database pooling using <a href="http://libdbi.sourceforge.net/download.html">libdbi</a>.  Applications written in PHP or CGI code don&#8217;t pool DB connections. They  open and closes the connection each time a user hit a page.</p>
<p>The Apache module mod_dbd can manages database connections.  On  non-threaded platforms,     it provides a persistent connection. On  threaded platform, it provides an altogether more     scalable and  efficient <em>connection pool</em>, as     described in <a href="http://www.apachetutor.org/dev/reslist">this     article at  ApacheTutor</a>.  Note that <code><a href="http://httpd.apache.org/docs/2.1/mod/mod_dbd.html">mod_dbd</a></code> supersedes the modules presented in that article.</p>
<p>PHP provides <a href="http://php.net/manual/en/function.mysql-pconnect.php">mysql_pconnect</a>.  It first tries to find a    (persistent) link that&#8217;s already open with  the same host,    username and password (NOT PORT).  If one is found, an  identifier for it    will be returned instead of opening a new  connection.  Second, the connection to the SQL server will not be closed  when    the execution of the script ends.  Instead, the link will  remain    open for future use (<a href="http://www.php.net/manual/en/function.mysql-close.php">mysql_close()</a> will not    close links established by <strong>mysql_pconnect()</strong>).</p>
<p>Back to MySQL.</p>
<p><strong>Max Connections</strong> has a default of <strong>100</strong> (&lt;= 5.1.14) to <strong>151</strong> (&gt;= 5.1.15).  If you exceed this number you will receive a &#8220;Too many  connections error&#8221;.  This is often the first setting people find they  have to changed as their usage grows.</p>
<p><strong>Max User Connections</strong> controls the maximum number of  simultaneous connections allowed per MySQL user  account. By default this is set to unlimited (zero).  The default is  fine. Your system will not run out of connections or memory if you have  set Max Connections correctly.  Except a bad applications can &#8220;steel&#8221;  all the connections from all the others.   Hopefully you have assigned a  different user for each application.  Set this to the maximum number of  connections any given  user (application) will make.</p>
<p><strong>Wait  timeout</strong> is the number of seconds the server waits for activity on a            non-interactive connection before closing it. The default is  eight hours. (28800 seconds)  This is a crazy amount of time.  How long  would you stay on the phone if no one was speaking?  Fifteen seconds is a  good place to start. If it gives you problems increase it by fifteen  until your good. Applications that open a connections and then process  their data for minutes before making a request will be effected by a  short time setting.</p>
<p><strong>Interactive timeout</strong> is the number of seconds the server waits  for activity on an interactive connection before closing it. This is  only used by applications using the  <a title="21.9.3.52. mysql_real_connect()" href="http://dev.mysql.com/doc/refman/5.1/en/mysql-real-connect.html"> mysql_real_connect()</a> function of the MySQL API. I set this to 600.  My idea is to give more sophisticated users more grace.  (Think connection pools have to reconnect.)</p>
<p><strong>Connect timeout</strong> is the number of seconds that the MySQL server  waits for a connect packet before responding with Bad handshake. The  default is ten seconds.  Five would be better.</p>
<p><strong>Max connections errors</strong> is a strange setting. IO stops after  this number of bad connections is made from a host. You can unblock  blocked hosts with the           <a title="12.4.6.3. FLUSH Syntax" href="http://dev.mysql.com/doc/refman/5.1/en/flush.html"><code>FLUSH  HOSTS</code></a> statement.  The default for this is ten (10). On a &#8220;good&#8221; network this shouldn&#8217;t have to be changed.  Because &#8220;the dabase should never stop&#8221; most DBAs make this a very large number. If you  have a over loaded network you should put your database on a network of  its own.  Or, you can try increasing this value.</p>
<p><strong>max_allowed_packet</strong> controls the size of your BLOBs.  If you are storing pictures in your database, you may  need to set this.  The default is one megabyte (1M). The           protocol limit for           <code>max_allowed_packet</code> is 1GB.            Set it to the biggest           <a title="10.4.3. The BLOB  and          TEXT Types" href="http://dev.mysql.com/doc/refman/5.1/en/blob.html"><code>BLOB</code></a> you need to store. The value should be a multiple of 1024 bytes.</p>
<p>Here are my my.cnf settings for a server with over 200 PHP and Perl  applications and peeking at 4,000 connections per second.</p>
<pre># Network management
port                    = 3306
max_connections         = 5000  # number of simultaneous client connections allowed
max_user_connections    = 200   # number of connections a user can make, 0 = Unlimited
wait_timeout            = 15    # seconds to waits for activity on non interactive connection
interactive_timeout     = 600   # seconds to waits for activity on interactive connection
connect_timeout         = 5     # on connect seconds to waits for a connect packet
max_connect_errors      = 1000000 # IO stops after this number of bad connections one good connect reset
max_allowed_packet      = 10M   # How big is your BLOB?</pre>
<p><img title="Mark Grennan" src="http://mark.grennan.com/images/MarkGrennanSigniture.bmp" alt="Mark Grennan" width="166" height="69" /></p>
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2010%2F05%2Fmysql-network-connections%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=MySQL Network Connections - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2010/05/mysql-network-connections/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL master/slave support merged into Linux-HA</title>
		<link>http://www.mysqlfanboy.com/2010/04/mysql-masterslave-support-merged-into-linux-ha/</link>
		<comments>http://www.mysqlfanboy.com/2010/04/mysql-masterslave-support-merged-into-linux-ha/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 21:07:12 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Commentary]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tunning]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=105</guid>
		<description><![CDATA[(Re-posted from Florian&#8217;s blog.) MySQL replication support for the Pacemaker cluster manager (the stuff that we explained in this webinar) has made it into the Linux-HA resource agents default branch. If you are interested in testing — and you should! — please read the extended announcement. Feedback is extremely welcome on the linux-ha-dev mailing list. [...]]]></description>
			<content:encoded><![CDATA[<p>(Re-posted from <a href="http://fghaas.wordpress.com/">Florian&#8217;s blog</a>.)</p>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/replication.html">MySQL  replication</a> support for the <a href="http://www.clusterlabs.org/">Pacemaker</a> cluster manager (the stuff that we explained in <a href="http://fghaas.wordpress.com/2010/04/08/mysqlpacemaker-scaleout-webinar-recording-available/">this  webinar</a>) <a href="http://hg.linux-ha.org/agents/rev/82206c4818dc">has  made it</a> into the Linux-HA resource agents default branch. If you  are interested in testing — and you should! — please read the <a href="http://lists.linux-ha.org/pipermail/linux-ha-dev/2010-April/017350.html">extended  announcement</a>. Feedback is extremely welcome on <a href="http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev">the  linux-ha-dev mailing list</a>.</p>
<p>We are expecting to release this as part of resource-agents 1.0.4, in  late May/early June.</p>
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2010%2F04%2Fmysql-masterslave-support-merged-into-linux-ha%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=MySQL master/slave support merged into Linux-HA - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2010/04/mysql-masterslave-support-merged-into-linux-ha/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developer Tips using MySQL</title>
		<link>http://www.mysqlfanboy.com/2010/04/developer-tips-using-mysql/</link>
		<comments>http://www.mysqlfanboy.com/2010/04/developer-tips-using-mysql/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 16:17:02 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Developer]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=44</guid>
		<description><![CDATA[I get ask, by application developers,  &#8220;how do you optimize MySQL&#8221;.  I do lots of things that don&#8217;t really relate to a developer. I analyze the percent of queries are being pulled from cache for instance.  What a developer can do to optimize the SQL they develop is a different questions.   So here is a [...]]]></description>
			<content:encoded><![CDATA[<p>I get ask, by application developers,  &#8220;how do you optimize MySQL&#8221;.  I do lots of things that don&#8217;t really relate to a developer. I analyze the percent of queries are being pulled from cache for instance.  What a developer can do to optimize the SQL they develop is a different questions.   So here is a quick list of things applications developers should know about MySQL.</p>
<p><a href="http://dev.mysql.com/doc/refman/5.1/en/using-explain.html">Explain</a> will analyze your query.</p>
<p>This example shows the possible indexes (keys) that could be used and the index that was selected.  2,262 rows where selected and then sorted (Using file sorts) and one record was returned (limit 1).</p>
<pre><strong>mysql&gt;</strong> <strong>explain</strong> SELECT 5/9*(temp_F-32) as t, 5/9*(dewpt_F-32) as td, speed_mps as spd, dir
 &gt; where stn='KLDM' and date_time&lt;'2010-02-12 18:15' and date_time&gt;'2010-02-12 17:45'
 &gt; order by ABS( date_time - CAST('2010-02-12 18:00:00' as datetime) ) limit 1;</pre>
<pre>+----+-------------+----------+------+-----------------------+------+---------+-------+------+-----------------------------+
| id | select_type | table    | type | possible_keys         | key  | key_len | ref   | rows | Extra                       |
+----+-------------+----------+------+-----------------------+------+---------+-------+------+-----------------------------+
|  1 | SIMPLE      | metar_nc | ref  | PRIMARY,stn,date_time | stn  | 8       | const | 2262 | Using where; Using filesort |
+----+-------------+----------+------+-----------------------+------+---------+-------+------+-----------------------------+</pre>
<p>Using <a href="http://dev.mysql.com/tech-resources/articles/using-new-query-profiler.html">profiling</a> can give you even more information;  Don’t forget to turn it off with a ‘<strong>set profiling=0</strong>’ when you are done.</p>
<pre><strong>mysql&gt;</strong> set profiling=1;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| starting           | 0.000110 |
| Opening tables     | 0.000014 |
| query end          | 0.000004 |
| freeing items      | 0.000008 |
| logging slow query | 0.000002 |
| cleaning up        | 0.000003 |
+--------------------+----------+
6 rows in set (0.01 sec)
<strong>mysql&gt;</strong> <strong>set profiling=0;</strong></pre>
<p><strong><br />
Indexing Basics</strong></p>
<p>Avoiding disk reads is the name of the game.  Indexes are presorted and small.  Two or three disk reads of an index can point to a large amount of data.</p>
<ul>
<li>MySQL Can only use Prefixes of the index<br />
mysql&gt; SELECT AVG(age) FROM user GROUP BY city;<br />
This is a complex query that needs to scan all the rows. You can make it traverse shorter indexes by adding an index on (city,age)</li>
<li>Index (A,B) can be used for<br />
WHERE A=5 ,  WHERE A=6 AND  B=5 , WHERE A=7 AND B&gt;5<br />
It can&#8217;t be used for  –  WHERE B=6 AND B&lt;2</li>
<li>Only Equality/List allows second key part usage<br />
WHERE A=5 AND B&gt;6 &#8211; will use 2 key parts<br />
IN (1,2) AND B=2 &#8211; will use 2 key parts</li>
<li>A&gt;5 and B=2 will use 1 key part only<br />
The B=2 will be checked while reading row/index only<br />
A=5 ORDER BY B – will use the index<br />
A&gt;5 ORDER BY B – will NOT use the index</li>
<li>For simple cross reference look ups, add the data to the index to skip the data read.<br />
SELECT name FROM login=”Jack123”;</li>
</ul>
<p>If this is a very common part of your code, make the index (login,name).  When the index is read the data is in memory. Don’t add every column you just double the disk space and magnify the disk access.</p>
<p><strong>More Tips</strong></p>
<ol>
<li>Check that all tables have PRIMARY KEYs <span style="text-decoration: line-through;">on columns with high cardinality</span>. Primary keys must be unique.<br />
A column like, `gender` low cardinality (selectivity), an id column (Int &#8211; auto increment) is a good candidate to become a primary key.</li>
<li>All joins (inter, outer, ‘,’) should have indexes.</li>
<li>Fields you often search on (appear frequently in WHERE, ORDER BY or GROUP BY clauses) need indexes.<br />
But don’t add too many: the worst thing you can do is to add an index on every column of a table.</li>
<li>Don&#8217;t use DISTINCT when you have or could use GROUP BY</li>
<li>Open to the server just before you are going to use it.  Unless you are using a persistent connection library, don&#8217;t open a database connections and then run minutes of calculations before making your query.  You may find your connections has been &#8220;gone away&#8221; before you make your query.</li>
<li>When your index many columns, create a hash column. Then your query will look like:<br />
SELECT *<br />
FROM table<br />
WHERE hash_column = MD5( CONCAT(col1, col2) )<br />
AND col1=&#8217;aaa&#8217; AND col2=&#8217;bbb&#8217;;</li>
<li>Use less RAM by declaring columns only as large as they need to be to hold the values stored in them.<br />
Use CHAR type when possible (instead of VARCHAR, BLOB or TEXT) — when values of a column have constant length: MD5-hash (32 symbols) or  ICAO or IATA airport code (4 and 3 symbols). This is also true for indexes.  If only the last 4 symbols are unique index only that part.</li>
<li>Use SQL_NO_CACHE when you are SELECT-ing frequently updated data or large sets of data.  This way you will not kick good data out of the cache.</li>
<li>Avoid wildcards at the start of LIKE queries.  (LIKE ‘%find%’).  Finding ‘1234find’ in 10000 records requires up to 40,000 searches.</li>
<li>Normalizing redundant data is good but don’t split a table because you have too many columns.</li>
<li>Think of storing users sessions data (or any non-critical / high access data) in <a href="http://dev.mysql.com/doc/refman/5.0/en/memory-storage-engine.html">MEMORY</a> table — it’s very fast.</li>
<li>Divide complex queries into several simpler ones — they have more chances to be cached, so will be quicker.</li>
<li>A column must be declared as NOT NULL if it really is. This speeds up table traversing.</li>
<li>If you usually retrieve rows in the same order like expr1, expr2, &#8230;, make <a href="http://dev.mysql.com/doc/refman/5.0/en/alter-table.html">ALTER TABLE &#8230; ORDER BY expr1, expr2</a>, &#8230; to optimize the table.</li>
<li>Don’t use PHP loop to fetch rows from database one by one just because you can — use IN instead, e.g.<br />
SELECT *<br />
FROM `table`<br />
WHERE `id` IN (1,7,13,42);</li>
<li>Reuse your database connections.  Opening a new connection to the database will add one or more seconds to your query.<br />
In PHP use mysql_pconnect() to open a persistent connection with mod_php. Perl provides persistent connections with Apache::DBI with mod_perl. Python does not have persistent connections in mod_python.  But you can maintain them in your application.  (<a href="http://www.modpython.org/FAQ/faqw.py">http://www.modpython.org/FAQ/faqw.py</a>)</li>
<li>When inserting data, insert only those values that differs from the default. This reduces the query parsing time.</li>
<li>Use <a href="http://dev.mysql.com/doc/refman/5.0/en/insert-delayed.html">INSERT DELAYED</a> or <a href="http://dev.mysql.com/doc/refman/5.0/en/insert.html">INSERT LOW_PRIORITY</a> (for MyISAM) to write to your change log table.<br />
Also, if it’s MyISAM, you can add DELAY_KEY_WRITE=1 option — this makes index updates faster because they are not flushed to disk until the table is closed.</li>
<li>For your web application, images and other binary assets should normally be stored as files.</li>
<li> That is, store only a reference to the file rather than the file itself in the database.</li>
</ol>
<p><img title="Mark Grennan" src="http://mark.grennan.com/images/MarkGrennanSigniture.bmp" alt="Mark Grennan" width="166" height="69" /><br />
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2010%2F04%2Fdeveloper-tips-using-mysql%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=Developer Tips using MySQL - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2010/04/developer-tips-using-mysql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
