Live data from Hacker News

MySQL and partitioning tables with millions of rows

chrismoos.com

21–23 of 23 posts

Re: MySQL and partitioning tables with millions of rows

#21

Honestly, the first thing I did was add indexes. On device_id, user_id, and date_added. I'm using Linode and I kept getting these I/O notifications...and the site would load extremely slow at times. After looking at the log of my Rails app, I saw that some requests were taking > 20 seconds. I determined that this was some kind of blocking at the database. I connected to MySQL and ran something simple... select count(…

1. Were the keys composite or not? The correct key for the queries you listed is (device_id, date_added DESC). In MySQL you'd add this by doing "ALTER TABLE location ADD INDEX (device_id, date_added DESC)"

MySQL uses one key per query, so having an index on (device_id) and an index on (date_added) individually will do no (or very little) good for the queries you listed.

2. MyISAM uses table-level locking. This will block all reads (selects) while writes (insert, update, delete) are happening. It does not prevent multiple selects from happening at the same time. If you have a write-heavy table that you are also reading from you need to be using InnoDB.

3. SELECT COUNT(*) FROM location should be instantaneous in MyISAM . It will be slow(er) in InnoDB. If this query was blocking it's because something was trying to write to the table.

4. Foreign key constraints have nothing to do with anything here. That's one advantage of InnoDB, although most web apps I know don't use them, but not one that would help you. Rather, what will help you is the table-level vs. row-level for MyISAM vs. InnoDB.

This allows selects to happen to unaffected rows while writes are happening.

Re: MySQL and partitioning tables with millions of rows

#22

Honestly, the first thing I did was add indexes. On device_id, user_id, and date_added. I'm using Linode and I kept getting these I/O notifications...and the site would load extremely slow at times. After looking at the log of my Rails app, I saw that some requests were taking > 20 seconds. I determined that this was some kind of blocking at the database. I connected to MySQL and ran something simple... select count(…

MySQL does not scan any rows for SELECT COUNT(*) FROM table — the number of records is stored in table metadata. (Mind you, it only works for this kind of query, as soon as you add WHERE the story changes). On the other hand, MyISAM only has table level locks, not row level. I guess it is time for you to analyze what your app does with DB and how it can affect locking…

Sort of true -- for the InnoDB engine SELECT COUNT(*) FROM table is more complicated because each transaction has its own view of the table. So unlike MyISAM (which uses one big table-level lock to guarantee consistency), it can't store a row count on a per-table basis.

Re: MySQL and partitioning tables with millions of rows

#23

Earlier quoted context omitted.

Can you tell us the size of your query cache and your key cache, if they are enabled? If not, we should talk about enabling them. :-)

They are rather low...I figured that would be good on a limited memory configuration (maybe not :( ). query_cache_limit = 1M query_cache_size = 16M key_buffer = 16M max_allowed_packet = 16M thread_stack = 192K thread_cache_size = 8

OK, at least they're enabled :-) I'm not a DBA, but have hit IO issues with my MYISAM/MYSQL database many times myself.

1) Have you run Mysqltuner? If not, wget http://mysqltuner.com/mysqltuner.pl chmod to executable and execute it. I find it rather helpful.

2) Are you indexing every field that is part of any WHERE statement (at least for the big tables)?

3) Have you run EXPLAIN on the original query? (Suggested by someone else in this thread, and a critical question.)

4) Is the slow_query_log enabled, and are you sure about which query is causing the slowdown?

5) Have you tried tools like MySQL Administrator, which lets you monitor connections, threads, keys, queries, etc, in realtime?

Post reply on HN