Sometimes, I want to pop onto a database server, check the status of something, and then logout. So, for example, if I want to check on the number query cache free blocks, I run this long command:
% mysqladmin -u admin -p extended | grep -i qcache
Then I type in the password. Well, I grew tired of typing in the extra options, plus the password. Turns out, MySQL will look for the configuration file .my.cnf
in your home directory after it looks in /etc/my.cnf (it looks in a few other places as well). So I put this in my ~/.my.cnf
:
[client]
user=admin
password=secret
And now I can simply run:
% mysqladmin extended | grep -i qcache
and it works right away. Note that the password is stored in the clear.
Since I knew that the MySQL Query Cache used the literal queries as keys, it made sense that MySQL did not cache queries with certain SQL functions in them, such as this one:
$sql = "select event_id from events where event_dt >= curdate()";
Because MySQL knows that this query run today is not the same query when it is run tomorrow. There are other SQL functions such as rand()
and unix_timestamp()
that will bypass the query cache. These are listed here.
So I avoid these functions when possible by calculating the value in PHP. For example, I’d rewrite the above query as:
$date = date('Y-m-d');
$sql = "select event_id from events where event_dt >= '$date'";
At work, every project has an .htaccess
file containing at the least some mod_rewrite
rules. This way, all I need to do to run a project is check it out of version control. I don’t need to modify my local Apache configuration.
But turning this option on and allowing .htaccess
files may be a performance hit. More specifically, enabling the AllowOverride
option in Apache is a performance hit. The Apache docs sums up the problem best:
“Wherever in your URL-space you allow overrides (typically
.htaccess
files) Apache will attempt to open.htaccess
for each filename component. For example,DocumentRoot /www/htdocsAllowOverride all and a request is made for the URI
/index.html
. Then Apache will attempt to open/.htaccess
,/www/.htaccess
, and/www/htdocs/.htaccess
.”
So I disabled all .htaccess
files in production, and inserted each file’s individual mod_rewrite
rules into the main Apache config file. After a quick Apache Bench run, one project looked around 3% faster. Note that there are a few other useful optimizations on that page.
In MySQL (and MySQL only AFAIK), INSERT has a clause called ON DUPLICATE KEY UDPATE. When ON DUPLICATE KEY UPDATE is used with INSERT, the insert will update the record if a value for a unique or primary key already exists, or else create a record if the value does not exist. So now when a form can either create a new something or edit an existing something, you can use one query to do it, and not have to query to see if the something exists already.
So instead of this:
$num = $db->getone("select count(*) from events where event_id = 15");
if ($num > 0)
{
$sql = "update events set name = 'New name' where event_id = 15";
}
else
{
$sql = "insert into events (event_id, name) values (null, 'New name')";
}
you can do something like this:
$id = (empty($_POST['id'])) ? null : $_POST['id'];
$sql = "insert into events (event_id, name) values ($id, 'New name') on duplicate key update name = 'New name'";
Neat, eh?
In case you’re wondering what the difference is between ON DUPLICATE KEY UPDATE and a REPLACE query, a REPLACE fires a DELETE followed by an INSERT query, as opposed to a real UPDATE.
If you find yourself wanting to echo or print something to the screen, go ahead and do it, but be sure to add a debug-level logging call for the info too. Chances are, you or someone else will want to see the same info sometime in the future.