First of all, I am working on providing a shared hosting service with PHP 5.4.11 with the extension PDO and MySQL 5.1.66 (with Debian compression).
I am currently developing a service in which users have a limited quota for storing data in a database. At the moment, there is only one table in which user information is stored, which must be respected regarding the quota (but this can change). All tables use the InnoDB storage engine and utf8_unicode_ci command for text columns. Suppose a quota table has the following columns:
+--------------+-----------+ | Column name | Type | +--------------+-----------+ | id | int | | userId | int | | created | timestamp | | lastModified | timestamp | | description | varchar | | content | text | +--------------+-----------+
Now I need to calculate the size in bytes of all lines belonging to a specific user. I searched the documentation and went to Google, but found other people asking similar questions without receiving a satisfactory answer.
I know the MySQL LENGTH() function, but since it is a string function, it does not return the space occupied by numerical and date / time-fixed fields. And if you took into account only the string fields, the user could simply fill the database with blank lines that never reach their quota. I also know that in MySQL there is some overhead for each line of description, but I do not want to include it in the calculation. (As an equivalent, I would like to calculate the actual file size, not the file size on disk.)
In addition, I do not want to rely on the specific structure of the table, as this may change, and you will need to remember the function that calculates the quota.
Due to the lack of an existing solution, I came up with my own (see below). But it has some disadvantages, for example:
- He needs a list of data types and their corresponding sizes used in the table.
- It cannot accurately process the data types
FLOAT(p) , DECIMAL(M,D) , NUMERIC(M,D) and BIT(M) (althouhg this can be implemented). - He needs two separate requests.
So, here is what I came up with:
$db = new PDO(...); $tablename = 'users'; $userId = 1;
My question is: Is there a more βnativeβ, less hacker way to do this? If not, do you have any suggestions for optimizing performance?