Database

  • PostgreSQL – Check value is in array

    With PostgreSQL, if you have a field containing an Array and you want to get all the results containing this value in the array, you can use the following command.

    Here, we are looking for all the results in the table my_table containing my_value in the field my_array.

    SELECT * FROM my_table WHERE 'my_value'=ANY(my_array) ORDER BY id;
    
       id  |        type        |             title             |             my_array                                                            
    -------+--------------------+-------------------------------+------------------------------------
     93646 | this_is_my_type    | this_is_my_title              | {my_1,my_2,my_3,my_value,my_4,my_5}
     94184 | this_is_my_type    | this_is_my_title2             | {my_3,my_value,my_4,my_5}
     98240 | this_is_my_type    | this_is_my_title3             | {my_1,my_2,my_3,my_value}             
    
    
    
    

  • Calculate Database/Table size in MySQL

    MySQL is an open-source relational database management system (RDBMS) available under the terms of the GNU General Public License, as well as under a variety of proprietary agreements.

    Thanks to some few functions, it’s possible to retrieve important information about database size and/or table size for a MySQL instance.

    Size for all databases

    SELECT table_schema "Database Name", SUM(data_length+index_length)/1024/1024
    "Database Size (MB)"  FROM information_schema.TABLES GROUP BY table_schema;
    +---------------------+--------------------+
    | Database Name       | Database Size (MB) |
    +---------------------+--------------------+
    | db_1                |       113.51562500 |
    | db_2                |       172.59375000 |
    | db_3                |        48.70312500 |
    | db_4                |      1830.53125000 |
    | db_5                |       432.89062500 |
    | information_schema  |         0.07812500 |
    | mysql               |         1.03164101 |
    | performance_schema  |         0.00000000 |
    +---------------------+--------------------+
    8 rows in set (0.41 sec)
    

    Size of tables for a specific database

    SELECT table_name "Table Name", table_rows "Rows Count", round(((data_length + index_length)/1024/1024),2) "Table Size (MB)" FROM information_schema.TABLES WHERE table_schema = "db_4";
    +-------------------------------+------------+-----------------+
    | Table Name                    | Rows Count | Table Size (MB) |
    +-------------------------------+------------+-----------------+
    | table_1                       |        245 |            0.05 |
    | table_2                       |          1 |            0.03 |
    | table_3                       |        537 |            0.11 |
    | table_4                       |          1 |            0.05 |
    | table_5                       |         57 |            0.05 |
    | table_6                       |          0 |            0.05 |
    | table_7                       |          2 |            0.05 |
    | table_8                       |        584 |            0.13 |
    | table_9                       |         48 |            0.03 |
    | table_10                      |      12972 |            2.16 |
    | table_11                      |        584 |            0.11 |
       [ ... ]
    | table_50                      |          0 |            0.05 |
    | table_51                      |     119864 |           16.03 |
    | table_52                      |     146688 |            9.55 |
    | table_53                      |         48 |            0.05 |
    | table_54                      |     852591 |          208.30 |
    | table_55                      |     138786 |           66.64 |
    | table_56                      |         47 |            0.08 |
    | table_57                      |        169 |            0.09 |
    | table_58                      |          0 |            0.03 |
    +-------------------------------+------------+-----------------+
    58 rows in set (0.00 sec)
    

    For a complete documentation, please refer to the official documentation on their website https://www.mysql.com

  • Increase display limit for a find in Mongo Shell

    When doing a find request on a Shell Mongo, the display is automatically paginated and you’re getting this:

    Type "it" for more

    By default, the number of rows returned is 20. But this value can easily be changed by using this command:

    DBQuery.shellBatchSize = 200;

    You will now have 200 rows displayed per page.

  • Manage permissions on MySQL for remote users

    MySQL has always been one of the most used database engine for websites. Its security is really important, isolating and managing access must be taken in consideration.

    Hereafter are some commands to perform some checking and some updates.
    I will consider those variables for my examples, but you should customize them with your own:

    • Show permissions for user myuser on server myfirewalleddomain.local :
      show grants for 'myuser'@'myfirewalleddomain.local';
    • Revoke all privileges on my_db for user myuser on server myfirewalleddomain.local :
      revoke ALL PRIVILEGES ON `my_db`.* FROM 'myuser'@'myfirewalleddomain.local';
    • Grant access for SELECT on my_db for user myuser on server myfirewalleddomain.local with a password:
      GRANT SELECT ON `my_db`.* TO 'myuser'@'myfirewalleddomain.local' IDENTIFIED BY 'mYsupErs3curePassw0Rd' WITH GRANT OPTION;
    • Grant all privileges on my_db for user myuser on server myfirewalleddomain.local with a password:
      GRANT ALL PRIVILEGES ON `my_db`.* TO 'myuser'@'myfirewalleddomain.local' IDENTIFIED BY 'mYsupErs3curePassw0Rd' WITH GRANT OPTION;
      
      

  • Gather/Aggregate results with MongoDB

    Need to gather or aggregate some results with MongoDB? Easy!

    You can use the group command over any collection to get a nice display of your results aggregated:

    db.collection.group(
       {
         key: { document: 1},
         cond: { },
         reduce: function( curr, result ) {
                     result.total += 1;
                 },
         initial: { total : 0 }
       }
    )
    

    You will get something like:

    [
    	{
    		"document" : "my_first_document",
    		"total" : 44124
    	},
    	{
    		"document" : "my_second_example",
    		"total" : 3503
    	},
    	{
    		"document" : "still_another_one",
    		"total" : 3928
    	}
    ]
    

    Here you are!

  • Find PostgreSQL database size

    PostgreSQL is an object-relational database management system (ORDBMS). It’s a free and open-source tool available under BSD licence.

    Thanks to some built-in functions, you can get several information regarding database and/or table size. You will find below the main and most used examples.

    Database size (in octal)

    dbpg-# SELECT pg_database_size(‘db_1’);
    pg_database_size
    ----------------
    342733824
    (1 line)

    Database size (in human understandable format)

    dbpg-# SELECT pg_size_pretty(pg_database_size(‘db_1’));
    pg_size_pretty
    --------------
    327 MB
    (1 line)

    All databases size

    dbpg-# SELECT pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS size FROM pg_database;
    datname  | size
    ---------+-----
    postgres | 3537 kB
    db_1     | 327 MB
    db_2     | 4732 kB
    db_3     | 29 MB
    db_4     | 168 MB
    temp0    | 3928 kB
    (6 lines)

    Table size (excluding indexes and some auxiliary data)

    dbpg-# SELECT pg_size_pretty(pg_relation_size(‘users’));
    pg_size_pretty
    --------------
    64 kB
    (1 line)

    Table size (including all data)

    dbpg-# SELECT pg_size_pretty(pg_total_relation_size(‘users’));
    pg_size_pretty
    --------------
    152 kB
    (1 line)

    Find the largest table in database

    dbpg-# SELECT relname, relpages FROM pg_class ORDER BY relpages DESC limit 1;
    relname | relpages
    --------+---------
    db_1    | 8732
    (1 line)

     For the full documentation, please visit http://www.postgresql.org/