Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Thursday, January 29, 2009

MySQL bench marks, comparing MyISAM to InnoDB and evaluating the performance hit of foreign keys

During an interview earlier this week, the question of how using foreign keys would impact performance on MySQL. I decided that it would be worthwhile to actually check this out in practice. I started out with a basic structure as follows:

CREATE TABLE IF NOT EXISTS `a` (
`id` int(11) NOT NULL,
`data` varchar(255) NOT NULL,
`data2` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `b` (
`id` int(11) NOT NULL,
`a_id` int(11) NOT NULL,
`data3` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Then added a second database where we had a foreign key:

CREATE TABLE IF NOT EXISTS `b` (
`id` int(11) NOT NULL,
`a_id` int(11) NOT NULL,
`data3` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `a_id` (`a_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `b`
ADD CONSTRAINT `b_ibfk_1` FOREIGN KEY (`a_id`) REFERENCES `a` (`id`) ON DELETE CASCADE;

And just to make things complete, I added a third database identical to the first, but using MyISAM as the engine.

The first thing is to do a data population. I wrote a perl script to generate 10,000 inserts of random data into table A, each with 0-5 random inserts into table B.
Next a selection of queries. I did 500 variations of SELECT * FROM a INNER JOIN b ON (a.id=b.a_id) WHERE a.id=?

For deletes, I deleted 500 rows, using DELETE FROM a WHERE id=?; DELETE FROM b WHERE a_id=? for the databases without the foreign key and the simple DELETE FROM a for the foreign key database.

The results for inserts:
databaseinsert time (s)select time (s)delete time (s)
InnoDB, no FK4.50.253
InnoDB, FK4.00.01
MyISAM1.00.750

I had always assumed that the advantage of MyISAM over InnoDB was the opposite of what it turned out to be with this benchmark. Perhaps even more surprisingly, the foreign key version of the InnoDB database was uniformly faster than the non FK version. I would have expected to see some overhead at least in the insertion stage, but even on multiple trials, I found a consistent advantage for the foreign keys on the insertion.

Monday, April 7, 2008

Installing DBD::mysql on OS X Leopard 10.5.2

One of those minor nightmare installs. I need this though, so I've gotta work for a while.

Here is the problem that shows up:

~/.cpan/build/DBD-mysql-4.006-j5Hpiv> prove -v -l t/00base.t
t/00base......
1..5
ok 1
ok 2
ok 3
install_driver(mysql) failed: Can't locate loadable object for module DBD::mysql in @INC (@INC contains: /Users/dhosek/.cpan/build/DBD-mysql-4.006-j5Hpiv/lib /Users/dhosek/.cpan/build/DBD-mysql-4.006-j5Hpiv/lib /System/Library/Perl/5.8.8/darwin-thread-multi-2level /System/Library/Perl/5.8.8 /Library/Perl/5.8.8/darwin-thread-multi-2level /Library/Perl/5.8.8 /Library/Perl /Network/Library/Perl/5.8.8/darwin-thread-multi-2level /Network/Library/Perl/5.8.8 /Network/Library/Perl /System/Library/Perl/Extras/5.8.8/darwin-thread-multi-2level /System/Library/Perl/Extras/5.8.8 /Library/Perl/5.8.6 /Library/Perl/5.8.1 .) at (eval 3) line 3
Compilation failed in require at (eval 3) line 3.
Perhaps a module that DBD::mysql requires hasn't been fully installed
at t/00base.t line 38
Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/5 subtests

Test Summary Report
-------------------
t/00base.t (Wstat: 512 Tests: 3 Failed: 0)
Non-zero exit status: 2
Parse errors: Bad plan. You planned 5 tests but ran 3.


First step, is that I'm finding that the blib/arch/auto/DBD/mysql/mysql.so file is not being created.

Actually, that's not the problem! Looking closer, I'm seeing that the .bs and .bundle files are apparently the ones that apply now. Interestingly using prove -b t/00base.t does in fact work. But make test has the same test fail...

Ignoring that issue, I move on to the next test:

prove -b t/10connect.t
t/10connect......dyld: lazy symbol binding failed: Symbol not found: _mysql_server_init
Referenced from: /Users/dhosek/.cpan/build/DBD-mysql-4.006-j5Hpiv/blib/arch/auto/DBD/mysql/mysql.bundle
Expected in: dynamic lookup

dyld: Symbol not found: _mysql_server_init
Referenced from: /Users/dhosek/.cpan/build/DBD-mysql-4.006-j5Hpiv/blib/arch/auto/DBD/mysql/mysql.bundle
Expected in: dynamic lookup

t/10connect...... Failed 2/2 subtests

Test Summary Report
-------------------
t/10connect.t (Wstat: 5 Tests: 0 Failed: 0)
Parse errors: Bad plan. You planned 2 tests but ran 0.
Files=1, Tests=0, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.04 cusr 0.00 csys = 0.05 CPU)
Result: FAIL


Update: 11 April 2008 The problem has its roots in using the pre-compiled distribution of mysql from mysql.com rather than building from source. This resulted in getting incompatible libraries between what was compiled for DBD::mysql and what I had in the mysql directory. Building from source resolved the problem. Good instructions on doing the job are available at HiveLogic.