Sunday, December 22, 2024

Programmer Overkill (MySQL)

Share

I have a peeve about MySQL. Oh, not about MySQL directly: it’s great. I love it, it’s wonderful, no complaints. It’s the people who use it when they don’t need to that get me shaking my head and talking to myself.

This falls in the same general category as my previous rant about text vs. binary. Some people use MySQL for idiotic purposes.

If you have a couple of hundred records to store, a database of any kind is just plain ridiculous. You can open that file and find anything by brute force far quicker than you can make a database connection and process a SELECT. Furthermore, the data is immediately available to any other program, to shell scripts, and to human beings with text editors.

To show that, I created a small MySQL database with 99 records and a text file to match. A Perl program reads through the text file and prints it out. The other Perl program does nothing but connect to the database, disconnect and exit. Here’s the code:

# text version
#!/usr/bin/perl
open(I,"tst");
while( <I> ) {
chomp;
print "$_\n";
}

# mysql version
#!/usr/bin/perl5
use DBI;
$dbh=DBI->connect('DBI:mysql:foo:localhost','root','8k3t2s95', {PrintError=>1, RaiseError=>1}) or die "$DBI::errstr";
$dbh->disconnect;
exit 0;

The MySQL doesn’t read a single record and prints nothing to the screen. Watch:

$ time mysqlversion.pl
real 0m0.187s
user 0m0.108s
sys 0m0.070s
$ time textversion.pl
foo, 1
foo, 2
foo, 3
...
foo, 97
foo, 98
foo, 99

real 0m0.020s
user 0m0.011s
sys 0m0.001s

The text version, which actually reads and prints all the records, takes about a tenth of just the startup time for the MySQL version.

So you say, sure, but that’s just a hundred records. Let’s try a thousand, shall we?

foo, 1
foo, 2
foo, 3
...
foo, 997
foo, 998
foo, 999

real 0m0.069s
user 0m0.005s
sys 0m0.016s

See what I mean? That’s reading and printing one thousand records, and it still does it before MySQL is ready to do anything at all.

Again, it’s not MySQL that’s the problem. If you choose to drive an eighteen wheeler to pick up groceries, we can’t blame the truck. Actually, if it were just you making that choice, I wouldn’t care, but when some otherwise nice piece of software insists upon using MySQL for no good reason, I get annoyed. And I mutter. And I rant here, of course.

*Originally published at APLawrence.com

A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

Table of contents

Read more

Local News