Home » Performance » MySQL » Performance of a database with a lot of tables
Performance of a database with a lot of tables [message #696] Fri, 09 February 2007 03:58 Go to next message
Kynlem  is currently offline Kynlem
Messages: 11
Registered: February 2007
Location: Ukraine
Junior Member
Hello,

I am working on hosting solution for a popular open source script, where users can sign up and get an own forum. We use InnoDB, and each user has an own set of tables (around 30); there are currently around 500 users registered. Despite the large count of tables, database is small, around 50 MB.

Server is dual Xeon with 4GB of RAM and MySQL uses all memory, available on the server, so there's even nothing left for Apache.

As the user base is growing, what steps would you suggest me to do, in order to keep good database performance and make it more efficient on RAM usage? The former is more important.

------------------------
Olexandr Melnyk,
http://omelnyk.net/
Re: Performance of a database with a lot of tables [message #699 is a reply to message #696 ] Fri, 09 February 2007 08:08 Go to previous messageGo to next message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
Olexandr,

First - I think you configured MySQL to consume too much memory this is why it takes all memory available. check your my.cnf

Regarding your structure - if you will get very large amount of users you will get into trouble - Innodb does not like too many tables. Thousands are OK but if it starts to be tens-hundreds of thousands you're in trouble.

It is best to charge software so it can share tables.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Re: Performance of a database with a lot of tables [message #700 is a reply to message #696 ] Fri, 09 February 2007 08:52 Go to previous messageGo to next message
Kynlem  is currently offline Kynlem
Messages: 11
Registered: February 2007
Location: Ukraine
Junior Member
Hello Peter, and thanks for the prompt reply.

Quote:

First - I think you configured MySQL to consume too much memory this is why it takes all memory available. check your my.cnf


There was some misunderstanding between and the server admin, MySQL is actually taking 700 MB in memory; and looks like the problem wasn't it. Sorry for taking your take with that.

Quote:

Regarding your structure - if you will get very large amount of users you will get into trouble - Innodb does not like too many tables. Thousands are OK but if it starts to be tens-hundreds of thousands you're in trouble.


Around hundred users sign up for the service daily, so as I understand this can become an issue soon. Wouldn't splitting database into several databases, or over several servers help in this case?

Quote:

It is best to charge software so it can share tables.


Well, the software is quite huge, so that's not really an option. I understand the approach I use is "incorrect" from the database theory standpoint, and much from the performance but it's much harder to do it using a single table set. Are there any other options, besides switching to a shared [for all users] tables model, which would increase performance?
Re: Performance of a database with a lot of tables [message #701 is a reply to message #700 ] Fri, 09 February 2007 10:05 Go to previous messageGo to next message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
You obviously can split it to many MySQL servers, the question is rather efficiency at which you can handle it. Ie having 10.000 or 100.000 users per server can be a big difference :)

Two problems you should watch out id Innodb internal table dictionary which is never purged - so all open tables stay in memory forever and estimating stats which is done on table open first time after MySQL restart.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Re: Performance of a database with a lot of tables [message #704 is a reply to message #701 ] Fri, 09 February 2007 13:20 Go to previous messageGo to next message
Kynlem  is currently offline Kynlem
Messages: 11
Registered: February 2007
Location: Ukraine
Junior Member
Quote:

You obviously can split it to many MySQL servers, the question is rather efficiency at which you can handle it. Ie having 10.000 or 100.000 users per server can be a big difference :)


Yeah

Quote:

Two problems you should watch out id Innodb internal table dictionary which is never purged - so all open tables stay in memory forever and estimating stats which is done on table open first time after MySQL restart.


Thanks for the tips. Does the first one mean that this can become a memory issue? I've been reading your blog posts, especially about open table scalability (great posts BTW!), is there anything else but the points described there I should pay attention to?

[Updated on: Fri, 09 February 2007 13:20]

Re: Performance of a database with a lot of tables [message #707 is a reply to message #704 ] Fri, 09 February 2007 16:54 Go to previous messageGo to next message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
There might be. I wrote about Innodb issues with multiple tables few times.

But anyway I listed the points here.

yes it can become memory issue up to the point it will take more memory than you have at all - ie 1.000.000 of tables 4K each take 4GB of RAM which is serious waste.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Re: Performance of a database with a lot of tables [message #710 is a reply to message #696 ] Sat, 10 February 2007 15:33 Go to previous messageGo to next message
Kynlem  is currently offline Kynlem
Messages: 11
Registered: February 2007
Location: Ukraine
Junior Member
Quote:

it can become memory issue up to the point it will take more memory than you have at all - ie 1.000.000 of tables 4K each take 4GB of RAM which is serious waste.


An idea, I came up with recently: what about setting up own views for each user with "on update" and "on delete" handlers, which would perform like they are separate tables, but make the requested changes / fetch data from a single table using a key field? The problem that comes up is that MySQL doesn't allow to set the data change/delete handlers for views. Of course, we can use empty tables with "before" triggers instead of using views, but then it wouldn't make much sense. Is there a workaround for this or any other implementation of this approach?
Re: Performance of a database with a lot of tables [message #711 is a reply to message #710 ] Sat, 10 February 2007 17:03 Go to previous messageGo to next message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
Good idea.

The views should be updatable in such case - if you only restrict things by user_id. What exactly fails ?


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Re: Performance of a database with a lot of tables [message #714 is a reply to message #696 ] Sun, 11 February 2007 10:13 Go to previous messageGo to next message
Kynlem  is currently offline Kynlem
Messages: 11
Registered: February 2007
Location: Ukraine
Junior Member
Quote:

The views should be updatable in such case - if you only restrict things by user_id. What exactly fails ?

I don't know how to define "on delete/update/insert" and "on change" hadlers for views. Or can "before insert/change/delete" triggers be used for views as well?
Re: Performance of a database with a lot of tables [message #715 is a reply to message #714 ] Sun, 11 February 2007 11:52 Go to previous messageGo to next message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
If you're creating a view in a way MySQL will be able to find a view in original table to perform update, it should be updatable without extra steps.

As you mention you simply filter by user_id it should be the case, so I'm curious what exactly does not work.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Re: Performance of a database with a lot of tables [message #728 is a reply to message #696 ] Thu, 15 February 2007 15:09 Go to previous messageGo to next message
Kynlem  is currently offline Kynlem
Messages: 11
Registered: February 2007
Location: Ukraine
Junior Member
Quote:

As you mention you simply filter by user_id it should be the case, so I'm curious what exactly does not work.

Not that it doesn't work, but I do not know how to do it technically. How can I set an additional field to a fixed value when an insert/update/delete query is being issued?
Re: Performance of a database with a lot of tables [message #729 is a reply to message #728 ] Thu, 15 February 2007 16:34 Go to previous messageGo to next message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
You update the view and it is view which sets it to the fixed value for you and finds proper row in base table.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Re: Performance of a database with a lot of tables [message #730 is a reply to message #696 ] Fri, 16 February 2007 00:33 Go to previous messageGo to next message
Kynlem  is currently offline Kynlem
Messages: 11
Registered: February 2007
Location: Ukraine
Junior Member
Quote:

You update the view and it is view which sets it to the fixed value for you and finds proper row in base table.


Okay, great. Wasn't sure whether MySQL will do it automatically or not.

Talking more closely to how application works right now, would it give any benefit to split up tables into several databases?

[Updated on: Fri, 16 February 2007 00:34]

Re: Performance of a database with a lot of tables [message #732 is a reply to message #730 ] Fri, 16 February 2007 08:23 Go to previous messageGo to next message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
Sure. I would not hold tens of thousands of tables in the same database. It is at least inconvenient and some commands would run too long.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Re: Performance of a database with a lot of tables [message #735 is a reply to message #696 ] Fri, 16 February 2007 09:45 Go to previous messageGo to next message
al.james  is currently offline al.james
Messages: 16
Registered: October 2006
Junior Member

It sounds to me that each of your users has a copy of the same set of tables.

Surely the best way to manage this is to just have one of each table and add a 'user id' field. This would reduce number of tables, and I am sure it would give you just as good performance if properly indexed.
Re: Performance of a database with a lot of tables [message #809 is a reply to message #696 ] Mon, 26 February 2007 02:37 Go to previous messageGo to next message
Kynlem  is currently offline Kynlem
Messages: 11
Registered: February 2007
Location: Ukraine
Junior Member
Thanks for the replies.

Continuing talking about using views, would it produce a lot of overhead in comparison with adding the proper condition to queries in the application, like:

select post_id,
       post_title
  from posts
 where post_id = 5
   and user_id = 7

?

Are there any side effects I should consider when using views?

[Updated on: Mon, 26 February 2007 02:40]

Re: Performance of a database with a lot of tables [message #810 is a reply to message #809 ] Mon, 26 February 2007 05:49 Go to previous messageGo to next message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
I guess you need to test it.

Your views are going to be simple and so they should basically do same rewrite as you're mentioning. If it is enough I would fix the application though.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Re: Performance of a database with a lot of tables [message #812 is a reply to message #696 ] Mon, 26 February 2007 10:39 Go to previous message
Kynlem  is currently offline Kynlem
Messages: 11
Registered: February 2007
Location: Ukraine
Junior Member
Is there a way to make MySQL set user_id automatically when I perform an insert to username_tablename (it is a view on tablename)?
Previous Topic:varchar index killing csv import
Next Topic:ORDER BY … LIMIT Performance Optimization
Goto Forum:
  



Current Time: Fri Dec 5 11:48:09 EST 2008

Total time taken to generate the page: 0.00905 seconds
.:: Contact :: Home :: MySQL Support by Percona.com ::.

Powered by: FUDforum 2.7.5.
Copyright ©2001-2006 FUD Forum Bulletin Board Software

MySQL Performance | Forum authority Badge