简体   繁体   English

PHP / MYSQL - 如何为 25,000 个用户构建日志数据库

[英]PHP / MYSQL - how to structure a log database for 25,000 users

I'm dealing with about 25,000 users (employees) spread over 5 divisions in a company.我正在与分布在公司 5 个部门的大约 25,000 名用户(员工)打交道。

For all these users, there's an MS Excel spreadsheet in use at the moment.对于所有这些用户,目前正在使用 MS Excel 电子表格。 The spreadsheet consists of about 35 columns, logging the employees's daily activities.该电子表格由大约 35 列组成,记录了员工的日常活动。

Each row is 1 activity and there are on average about 3 activities per day (never ending, meaning the log just grows and grows).每行是 1 个活动,平均每天大约有 3 个活动(永无止境,这意味着日志只会不断增长)。

MY QUESTION:我的问题:

I would like to build a database (PHP/MYSQL) that holds the activity log for these users as opposed to the MS Excel files.我想建立一个数据库(PHP/MYSQL)来保存这些用户的活动日志,而不是 MS Excel 文件。

  1. Should I have a table per user with the 35 columns... leading to a database with 25,000 tables?我是否应该为每个用户创建一个包含 35 列的表...导致一个包含 25,000 个表的数据库?

  2. Or should I store the activities to a 35-sized array, convert it to binary and store it in a blob and build such a table per year... leading to 1 table per year with 25,000 rows?或者我应该将活动存储到一个 35 大小的数组中,将其转换为二进制并将其存储在一个 blob 中并每年构建一个这样的表......导致每年 1 个表有 25,000 行?

Employee
------------
employeeID
employee_name

Day
------------
dayID
day

Activity
-------------
activityID
activity_name
dayID
employeeID

This way you can see an activity for a day You can see activities for an employee Can see activity for an employee on a specific day这样您就可以查看一天的活动 您可以查看员工的活动 可以查看员工在特定日期的活动

I would use a 35-column table, if you actually use many/most of those fields per activity.如果您实际上在每个活动中使用许多/大部分这些字段,我会使用 35 列表。

CREATE TABLE users (
    uid    INT,
    name   VARCHAR(255),
    ...
);

CREATE TABLE activities (
    uid    INT, // references users.uid
    type   VARCHAR(32),
    date   DATE,
    ... // The 35 activity-related columns
);

And then I would partition on time.然后我会按时分区。 Perhaps per-year as you suggested (that would mean up to about 27.4 million rows per table), or per month (about 2.2 million rows per table) if search performance is important, and a per-year table is too big for good performance.如果搜索性能很重要,则可能是每年(这意味着每个表最多大约 2740 万行),或者每月(每个表大约 220 万行),并且每年的表太大而无法获得良好的性能.

As a rule of thumb, you don't want to create a table for each user.根据经验,您不想为每个用户创建一个表。 You want to have 1 table, users , for that matter, where you'd store IDs, names etc that pertain only to a user model.就此而言,您希望有 1 个表users ,您将在其中存储仅与用户 model 相关的 ID、名称等。 Then have a separate table would document users' daily activities with a user_id column as a reference to the user's row in the users table.然后有一个单独的表将记录用户的日常活动,其中user_id列作为users表中用户行的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM