1

I am running a Perl script (using DBI) which reads from raw files from a hard disk, and updates MySQL database (which is on a separate SSD). My performance is rather slow (1000 files processed in 30-60 seconds), but I can not find the bottleneck.

CPU. Network, Disk and Memory are all rather unused. I am running Windows 7 64bit on an i7 machine (8 cores). MariaDB is version 10.0.10. My database is around 78G in size with 5M entries, all tables properly indexed.

Perfmon confirms this showing total CPU 6%, Network 0%, HD Disk 140 I/O/s, Memory 10%. None of the CPU cores are used more than 3-4%.

I have experimented with changing all these mysql variables with no success: innodb_use_global_flush_log_at_trx_commit innodb_buffer_pool_instances global.max_connections innodb_thread_sleep_delay global.innodb_io_capacity global.innodb_sync_spin_loops innodb_flush_log_at_trx_commit wait_timeout

Mysqltuner did not report anything of interest, except for: Data in InnoDB tables: 78G (Tables: 12) Total fragmented tables: 10 Query cache is disabled Thread cache is disabled InnoDB data size/buffer pool: 78.1G/2.0G

Perl profiling showed that majority of time is taken up by DBI::st::execute (invoking sql).

I have also tried disabling firewall and virus scanner too - no difference.

6
  • Can you give the code of your script?
    – nightfox79
    Jul 18, 2014 at 20:22
  • Are you using prepared statements in your $sth->execute calls?
    – friedo
    Jul 18, 2014 at 20:39
  • 140 IOPS is pretty high for a mechanical drive. That's probably your bottleneck.
    – AKHolland
    Jul 18, 2014 at 21:15
  • Windows 7 64bit -- i think thats your problem. Running what you have is not ideal to be running on a Desktop. Now if you were using a Linux Server like Ubuntu or Redhat on your system youl see a speed gain between 25 to 50 % across the board.
    – Tasos
    Jul 19, 2014 at 6:29
  • Sorry for not answering before. I was on an extended vacation. Aug 13, 2014 at 1:06

2 Answers 2

2

Conclusion and a workaround

After exhausting all options on Windows, I decided to switch to Linux, mostly because I was frustrated with inability to profile and debug in detail.

I have moved the whole setup to Ubuntu 14.04. I first tried XAMPP but gave up because of conflicts between XAMPP and MySQL and MySQL Workbench. Then I moved to vanilla MySQL (5.5, I think) and vanilla Apache 2.

However, I was still left with the same problem – no visible bottleneck and resources still underutilized. I suspected throttling in TCP sockets (used between Perl code and MySQL), but further profiling proved this not to be the case.

Then, I turned my attantion to Perl DBI module DBD::SQL, thinking that it may be doing some throttlinig. I did some tests where I replaced DBI calls in Perl with system calls (system("mysql -e'INSERT INTO blah blah …'). I have determined that the performance did not change, therefore absolving DBI as a culprit.

I need to add one important detail now: I was in fact always running a number of my Perl scripts in parallel. Given that the CPU has 8 cores, this is necessary to utilize all of them, of course. Further debugging showed that almost all my perl processes which were supposed to work furiously were sleeping most of the time. Ubunty System Monitor shawed them as waiting on Waiting Channels wait_answer_interruptible or unix_stream_recvmsg. CPU History graph in System Monitor showed all perl processes jumping to 100% CPU utilization and then dropping to ~0% in unison. I suspected that MySQL server is not configured for multi threading, but htop showed 17 mysqld threads activated, confirming that all should be ok.
I suspected that all MySQL threads were waiting on the same semaphore and were locked out for most of the time. I dreaded delving into the dark bowels of MySQL trying to figure out what goes on inside. Instead, I decided to replace MySQL with MariaDB, even though MariaDB seems to have had the same issue originally when I was running it on Windows.

Lo and behold – this finally worked. My perl scripts were screaming.

One last problem remained: I had a very rudimentary method of parallelising the perl scripts: I would just run 10 or 20 with their respective loads and hope that they would utilize all the resources.

This has obvious drawbacks: if too many processes are spawned, the OS may spend too much time swapping them (although not a serious issue with only 20 processes, it becomes an issue with e.g. 1000). If not enough processes are spawned (e.g. less than 8, for each core) the CPU will not be utilized fully for sure. If too many processes exhaust RAM, Linux will turn to disk and will start swapping. As soon as this starts happening, everything grinds to a halt.

I searched but could not find a perl library/script/code which would spawn new processes only when CPU, memory and disk are under utilized. Hence I created my own: raspawn.pl (resource aware spawn) which I placed on github. Raspawn.pl spawns a number of processes while trying to keep resources utilization just below the maximum. It constantly checks the CPU, memory and disk utilization and only if all are less than ~90% utilized, starts a new process.

Finally, this worked. I can now process my whole load in around 7 days, instead of many months...

1

You do not provide much information about your system or program, but here are very general pieces of advise to improve the writing performance of your system:

  • Number one reason why MySQL can be slow is because it has inappropriate queries: are tables indexed correctly? Do you have a fast and flexible table design? Are you using memory efficiently? Partitioning? Do your queries involve too many disk IOPS? You should answer those questions first, then try to tune your server parameters.

  • Profile MySQL in the same way that you did with Perl. Low CPU usage doesn't mean there isn't a problem, as actually, it could be a symptom of a bottleneck. There are several tools available to do that, but one is integrated inside the server itself (performance_schema).

  • Augment your buffer pool (innodb_buffer_pool_size). It is the main buffer system used by InnoDB. You will probably not be able to cover your entire dataset (78 GB), but it probably can be higher than 2GB -you have a lot of free memory. The rule of the thumb is making it 80% of your physical memory, but your mileage may vary. Make sure that you are not swapping in any case.

  • Augment your transaction log (innodb_log_file_size). The default is too small in most versions, and all writes have to be modify this file. If it is not large enough, you will get a bottleneck on your writes.

  • Reduce durability (innodb_flush_log_at_trx_commit = 2). As you are writing from disk files, your database crashing may not affect too much your data durability (corruption is not affected when tuning this parameter, innodb is always consistent with disk). Allowing more operations to be buffered in memory is dangerous for banking operations, but maybe you can get by in your particular workload, making the performance much better.

  • innodb_log_buffer_size. Large transactions can become a bottleneck, specially involving large blob-like fields. Make sure you monitor Innodb_log_waits and configure this variable accordingly.

  • Logs - Make sure that you disable or limit their output (general log, slow log, binary log). The binary log is not usually recommended to be disabled -it is useful for disaster recovery and replication-, but in extreme cases, with lots of (large) updates it can be a performance penalty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.