Posts Tagged ‘MYSQL’

Optimize MySQL queries w/delayed JOIN & LIMIT

Permalink

Mysql Perfomancing Blog had a nice article on using delayed JOIN to optimize count(*) and LIMIT queries. I just personally optimized a couple of nagging queries with the “delayed limit” approach… very nice.

How-To: Connecting to MySQL Remotely from Windows with SSH

Permalink

Connecting to MySQL Remotely from Windows with SSH
Read the following for an introduction: http://dev.mysql.com/doc/refman/5.0/en/windows-and-ssh.html

Specific Instructions for Connecting with SSH Secure Shell:
(can anyone recommend a better client?)

  • Create an initial connection in SSH and verify you can connect.
  • ‘Edit Profiles’ and select the ‘Tunneling’ tab for that connection.
  • Add an ‘Outgoing’ tunnel as such:
    Display Name: MySQL
    Type: TCP
    Listen Port: 3307 (I used this instead of 3306 because I have a local instance running on 3306 already)
    Destination Host: localhost
    Destination Port: 3306

Then, if you’re using MySQL Query Browser or Administrator
Create a new connection with the following parameters

  • Connection: (enter whatever you prefer)
  • Username & Password: (should be your local access, not root)
  • Hostname: localhost
  • Port: 3307 (see above note for why I chose this port)

That’s It! Enjoy your secure connection!

MySQLDump Restore Error

Permalink

I’ve been working with MySQL for almost 10 years now. There, now that that’s out of the way, here’s my bonehead move:
Yesterday I wanted to throw a mysqldump created file into a local database so I issued the command:

mysqldump -uuser -p target_database < source_file.sql

MySQL spit out a result I didn’t expect:

-- MySQL dump 10.10
--
-- Host: localhost Database: jrob00_fshub
-- ------------------------------------------------------
-- Server version 5.0.27-community-nt

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
Read More »

MySQL 5: Comma & JOIN Precedence

Permalink
mysql mints
Photo: jimw

I use MySQL 5 on my local development machine which is probably both a good and bad idea at the same time.

It’s a good idea because the SQL I write and execute in the Query Browser will be forward compatible if our production machine ever makes the leap to version 5.

However it’s also a bad thing because syntax that works on older versions doesn’t always work on version 5.

I guess I’ve been lucky so far to not have encountered a query that follows that last rule until just today when I executed a presently known-to-work query that kept failing on my local machine.

It’s a simple query really:

SELECT t1.a, COUNT(*)
FROM t1, t2
LEFT JOIN t3 ON t3.a=t1.a
LEFT JOIN t4 ON t4.a=t3.a
GROUP BY t1.a;

But it yielded the error: “Unknown column ‘t1.a’ in ‘on clause’”

In an attempt to troubleshoot I stripped it down bit by bit until I found that it worked. Basically it worked fine until I attempted to join any tables to the first cross-joined tables.

After banging my head on the table for much longer than I should have, I discovered that the reason this fails is in fact new to MySQL 5. Specifically, it fails because the precedence of commas and joins has been changed.

In previous versions, the comma and join had the same precedence. Begining with version 5.0.12 however, joins were given a higher precedence than commas in order to comply the SQL standard.

Thus the above query actually means:

SELECT t1.a, COUNT(*)
FROM t1, (t2
LEFT JOIN t3 ON t3.a=t1.a
LEFT JOIN t4 ON t4.a=t3.a)
GROUP BY t1.a;

You can read more about it in the MySQL manual (search for ‘Previously, the comma’) but basically this change affects statements that use an ON clause because that clause can refer only to columns in the operands of the join. Since our statement is now trying to join t2 and t3 with a t1 column - it fails.

The fix is to simply supply parenthesis around our first two table calls to override the precedence interpreter.

SELECT t1.a, COUNT(*)
FROM (t1, t2)
LEFT JOIN t3 ON t3.a=t1.a
LEFT JOIN t4 ON t4.a=t3.a
GROUP BY t1.a;

I hope this helps someone else. Good Luck

Install a WAMP Environment with Ease

Permalink
XAMPP

For anyone that’s had to install a WAMP (Windows Apache Mysql PHP) environment with any level of regularity, it sure gets old doesn’t it?

It seems that no matter what I do to standardize the process every install ends up different with its own nuances and required troubleshooting.

I can’t go for too long without itching the desire to re-install my dev environment in an effort to make sure it’s “clean”. I suppose that sort of mindset comes after using windows for so long and seeing things slowly self-corrupt over time. In any event, I’ve been putting off a re-installation of the environment on my laptop simply because of the amount of time involved in doing so.

I’ve been so busy with development that I simply can’t afford to take my system down and rebuild it. If only there was an easier way… and that’s when I remembered a site that I had bookmarked a while back called apachefriends.org.

Apache Friends’ main project it is an Apache, Mysql, PHP installer called Xampp. Xampp not only installs on a Windows environment but also on Linux, Solaris and OS X.

Well an installer should take no time at all so I backed up my current environment, gave it a shot and haven’t looked back since. Thanks Apache Friends!