Multiple table or one single table?

南笙酒味 提交于 2020-01-24 10:51:48

问题


I already saw a few forums with this question but they do not answer one thing I want to know. I'll explain first my topic:

I have a system where each log of multiple users are entered to the database (ex. User1 logged in, User2 logged in, User1 entered User management, User2 changed password, etc). So I would be expecting 100 to 200 entries per user per day. Right now, I'm doing it in a single table and to view it, I just have to filter out using UserID.

My question is, which is more efficient? Should I use one single table or create a table per user?

I am worried that if I use a single table, the system might have some difficulty filtering thousands of entries. I've read some pros and cons using multiple tables and a single table especially concerning updating the table(s).

I also want to know which one saves more space? multiple table or single table?


回答1:


As long as you use indexes on the fields you're selecting from, you shouldn't have any speed problems (although indexes slow writes, so too many are a bad thing). A table with a few thousand entries is nothing to mySQL (or any other database engine).

The overhead of creating thousands of tables is much worse -- say you want to make a change to the fields in your user table -- now you'd have to change thousands of tables.

A table we regularly search against for a single record @ work has about 150,000 rows, and because the field we search for is indexed, the search time is in very small fractions of a second.

If you're selecting those records without using the primary key, create an index on the field you use to select like this:

CREATE INDEX my_column_name ON my_table(my_column_name);

Thats the most basic form. To learn more about it, check here




回答2:


I would go with a single table. With an index on userId, you should be able to scale easily to millions of rows with little issue.

A table per user might be more efficient, but it's generally poor design. The problem with a table per user is it makes it difficult to answer other kinds of questions like "who was in user management yesterday?" or "how many people have changed their passwords?"

As for storage space used - I would say a table per user would probably use a little more space, but the difference between the two options should be quite small.




回答3:


I would go with just 1 table. I certainly wouldn't want to create a new table every time a user is added to the system. The number of entries you mention for each day really is really not that much data.

Also, create an index on the user column of your table to improve query times.




回答4:


Definitely a single table. Having tables created dynamically for entities that are created by the application does not scale. Also, you would need to create your queries with variable tables names, something which makes things difficult to debug and maintain. If you have an index on the user id you use for filtering it's not a big deal for a db to work through millions of lines.




回答5:


Any database worth its salt will handle a single table containing all that user information without breaking a sweat. A single table is definitely the right way to do it.

If you used multiple tables, you'd need to create a new table every time a new user registered. You'd need to create a new statement object for each user you queried. It would be a complete mess.




回答6:


I would go for the single table as well. You might want to go for multiple tables, when you want to server multiple customers with different set of users (multi tenancy). Otherwise if you go for multiple tables, take a look at this refactoring tool: http://www.liquibase.org/. You can do schema modifications on the fly.

I guess, if you are using i.e. proper indexing, then the single table solution can perform well enough (and the maintenance will be much more simple).




回答7:


Single table brings efficiency in $_POST and $_GET prepared statements of PHP. I think, for small to medium platforms, single table will be fine. Summary, few tables to many tables will be ideal.

However, multiple tables will not cause any much havoc as well. But, the best is on a single table.



来源:https://stackoverflow.com/questions/5156086/multiple-table-or-one-single-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!