<?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>Skidoosh &#187; MySQL</title> <atom:link href="http://www.skidoosh.co.uk/category/mysql/feed/" rel="self" type="application/rss+xml" /><link>http://www.skidoosh.co.uk</link> <description>Skidoosh - PHP, Python, Django, Ruby on Rails Web Developer in North Wales</description> <lastBuildDate>Sat, 12 Jun 2010 09:08:31 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.2</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Optimize your MySQL queries to fine tune you application</title><link>http://www.skidoosh.co.uk/mysql/optimize-your-mysql-queries-by-fine-tuning-the-find-model-method/</link> <comments>http://www.skidoosh.co.uk/mysql/optimize-your-mysql-queries-by-fine-tuning-the-find-model-method/#comments</comments> <pubDate>Sat, 12 Jun 2010 09:08:31 +0000</pubDate> <dc:creator>Glyn Mooney</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[performance]]></category><guid isPermaLink="false">http://www.skidoosh.co.uk/?p=86</guid> <description><![CDATA[As applications are getting more complex and manipulating data in more complex ways it's becoming more important to optimise our database queries to be as efficient as possible. This article shows how more explicit queries can reduce execution time and take some of the strain off system resources.]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.skidoosh.co.uk%2Fmysql%2Foptimize-your-mysql-queries-by-fine-tuning-the-find-model-method%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.skidoosh.co.uk%2Fmysql%2Foptimize-your-mysql-queries-by-fine-tuning-the-find-model-method%2F&amp;source=skidoosh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br /> </a></div><p>As applications are getting more complex and manipulating data in more complex ways it&#8217;s becoming more important to optimise our database queries to be as efficient as possible. I&#8217;m not going to cover caching in this article, instead I&#8217;m going to cover a more fundamental rule that should be followed when querying any database and follows this Python rule:</p><blockquote><p> &#8220;Explicit is better than implicit.&#8221;</p></blockquote><p>Lets start by looking at MySQL query performance from the command line. Lets imagine we had a user table with the following fields:</p><pre name="code" class="sql">
CREATE  TABLE  `article_db`.`users` (
`id` INT( 6  )  UNSIGNED NOT  NULL  AUTO_INCREMENT  PRIMARY  KEY ,
`group_id` INT( 6  )  UNSIGNED NOT  NULL ,
`username` VARCHAR( 30  )  CHARACTER  SET utf8 COLLATE utf8_general_ci NOT  NULL ,
`email` VARCHAR( 100  )  CHARACTER  SET utf8 COLLATE utf8_general_ci NOT  NULL ,
`password` VARCHAR( 36  )  CHARACTER  SET utf8 COLLATE utf8_general_ci NOT  NULL ,
`created` DATETIME NOT  NULL DEFAULT  '0000-00-00 00:00:00',
`updated` DATETIME NOT  NULL DEFAULT  '0000-00-00 00:00:00',
`active` TINYINT( 1  )  NOT  NULL DEFAULT  '0',
UNIQUE (
`username` ,
`email`
)
)  ENGINE  =  MYISAM  CHARACTER  SET utf8 COLLATE utf8_general_ci;
</pre><p>Normally to retrieve data from this table you would execute a query like the following. please note that I am using a remote database to create some lag. If you use a local database on your development machine you will probably retrieve the data in 0 seconds.</p><pre name="code" class="sql">
mysql> SELECT * FROM users;
+----+----------+----------+-----------------------+----------------------------------+---------------------+---------------------+--------+
| id | group_id | username | email                 | password                         | created             | updated             | active |
+----+----------+----------+-----------------------+----------------------------------+---------------------+---------------------+--------+
|  1 |        1 | admin    | admin@example.com     | 21232f297a57a5a743894a0e4a801fc3 | 2010-04-11 23:08:51 | 2010-04-11 23:08:51 |      1 |
|  2 |        2 | user     | user@example.com      | ee11cbb19052e40b07aac0ca060c23ee | 2010-04-11 23:08:51 | 2010-04-11 23:08:51 |      1 |
|  3 |        3 | test     | test@example.com      | 098f6bcd4621d373cade4e832627b4f6 | 2010-04-11 23:13:50 | 2010-04-11 23:13:50 |      0 |
|  4 |        4 | temp     | temp@example.com      | 3d801aa532c1cec3ee82d87a99fdf63f | 2010-04-11 23:13:50 | 2010-04-11 23:13:50 |      1 |
|  5 |        2 | me       | me@example.com        | ab86a1e1ef70dff97959067b723c5c24 | 2010-04-11 23:15:50 | 2010-04-11 23:15:50 |      1 |
|  6 |        2 | you      | you@example.com       | 639bae9ac6b3e1a84cebb7b403297b79 | 2010-04-11 23:15:50 | 2010-04-11 23:15:50 |      0 |
|  7 |        1 | john     | john@example.com      | 527bd5b5d689e2c32ae974c6229ff785 | 2010-04-11 23:17:04 | 2010-04-11 23:17:04 |      1 |
|  8 |        1 | master   | master@example.com    | eb0a191797624dd3a48fa681d3061212 | 2010-04-11 23:17:04 | 2010-04-11 23:17:04 |      1 |
|  9 |        1 | theboss  | theboss@testing.com   | b248e08d5c23541514558eea059c08cf | 2010-04-11 23:23:43 | 2010-04-11 23:23:43 |      1 |
| 10 |        2 | susan    | susan@localdomain.com | ac575e3eecf0fa410518c2d3a2e7209f | 2010-04-11 23:23:43 | 2010-04-11 23:23:43 |      1 |
+----+----------+----------+-----------------------+----------------------------------+---------------------+---------------------+--------+
10 rows in set (0.05 sec)
</pre><p>In the previous query we used the wild-card selector &#8216;*&#8217; to return all columns from the &#8216;users&#8217; table. This in turn queried the database and returned the results as above. What I&#8217;m going to do next is query the database for the same data but this time I&#8217;m going to name the fields that I require in my returned dataset:</p><pre name="code" class="sql">
mysql> SELECT id,group_id,username,email,password,created,updated,active FROM users;
+----+----------+----------+-----------------------+----------------------------------+---------------------+---------------------+--------+
| id | group_id | username | email                 | password                         | created             | updated             | active |
+----+----------+----------+-----------------------+----------------------------------+---------------------+---------------------+--------+
|  1 |        1 | admin    | admin@example.com     | 21232f297a57a5a743894a0e4a801fc3 | 2010-04-11 23:08:51 | 2010-04-11 23:08:51 |      1 |
|  2 |        2 | user     | user@example.com      | ee11cbb19052e40b07aac0ca060c23ee | 2010-04-11 23:08:51 | 2010-04-11 23:08:51 |      1 |
|  3 |        3 | test     | test@example.com      | 098f6bcd4621d373cade4e832627b4f6 | 2010-04-11 23:13:50 | 2010-04-11 23:13:50 |      0 |
|  4 |        4 | temp     | temp@example.com      | 3d801aa532c1cec3ee82d87a99fdf63f | 2010-04-11 23:13:50 | 2010-04-11 23:13:50 |      1 |
|  5 |        2 | me       | me@example.com        | ab86a1e1ef70dff97959067b723c5c24 | 2010-04-11 23:15:50 | 2010-04-11 23:15:50 |      1 |
|  6 |        2 | you      | you@example.com       | 639bae9ac6b3e1a84cebb7b403297b79 | 2010-04-11 23:15:50 | 2010-04-11 23:15:50 |      0 |
|  7 |        1 | john     | john@example.com      | 527bd5b5d689e2c32ae974c6229ff785 | 2010-04-11 23:17:04 | 2010-04-11 23:17:04 |      1 |
|  8 |        1 | master   | master@example.com    | eb0a191797624dd3a48fa681d3061212 | 2010-04-11 23:17:04 | 2010-04-11 23:17:04 |      1 |
|  9 |        1 | theboss  | theboss@testing.com   | b248e08d5c23541514558eea059c08cf | 2010-04-11 23:23:43 | 2010-04-11 23:23:43 |      1 |
| 10 |        2 | susan    | susan@localdomain.com | ac575e3eecf0fa410518c2d3a2e7209f | 2010-04-11 23:23:43 | 2010-04-11 23:23:43 |      1 |
+----+----------+----------+-----------------------+----------------------------------+---------------------+---------------------+--------+
10 rows in set (0.01 sec)
</pre><p>As you can see, by defining a more explicit query you have reduced the execution time from 0.05 to 0.01 seconds. This may not be too impressive, but imagine the &#8220;now&#8221; common application queries a database. During the life cycle of a page execution in a content management system you could be looking at around 10 &#8211; 40 (this is just an example figure) database queries being executed during this time. In turn each of these queries take .05 seconds to execute (40 * .05) = 2 seconds. So you go through your application and modify it to select one the columns you need. If we take the examples above we would have (40 * .01) = .04, thats a massive 1.96 seconds faster!</p><p>Hope this helps!</p> ]]></content:encoded> <wfw:commentRss>http://www.skidoosh.co.uk/mysql/optimize-your-mysql-queries-by-fine-tuning-the-find-model-method/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>MySQL Find and Replace Function</title><link>http://www.skidoosh.co.uk/mysql/mysql-find-and-replace-function/</link> <comments>http://www.skidoosh.co.uk/mysql/mysql-find-and-replace-function/#comments</comments> <pubDate>Wed, 16 Dec 2009 19:21:21 +0000</pubDate> <dc:creator>Glyn Mooney</dc:creator> <category><![CDATA[MySQL]]></category><guid isPermaLink="false">http://www.skidoosh.co.uk/?p=62</guid> <description><![CDATA[A quick guide to one of the most useful MySQL functions. The replace function is a great way to find and replace bulk text matches in one easy step.]]></description> <content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.skidoosh.co.uk%2Fmysql%2Fmysql-find-and-replace-function%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.skidoosh.co.uk%2Fmysql%2Fmysql-find-and-replace-function%2F&amp;source=skidoosh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br /> </a></div><p>The MySQL REPLACE function is one of the most useful function MySQL has to offer. It&#8217;s great if you need to do a bulk find and replace on text in your database table. It&#8217;s very useful for migrations from one domain name to another and I have used it countless times to move Wordpress installations from a testing to a production environment to re-point images. Here the code:</p><pre name="code" class="sql">UPDATE table_name SET column_name = REPLACE(column_name, 'find_string_in_quotes', 'replace_string_in_quotes');</pre><p>Hope you find this useful. If you know a better way please leave a reply!</p> ]]></content:encoded> <wfw:commentRss>http://www.skidoosh.co.uk/mysql/mysql-find-and-replace-function/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 7/13 queries in 0.004 seconds using disk

Served from: www.skidoosh.co.uk @ 2010-09-08 16:46:39 -->