Skip to content

Instantly share code, notes, and snippets.

@ManojKiranA
Last active July 13, 2021 07:30
Show Gist options
  • Save ManojKiranA/c4aeb6b3793fe3ccd2882fb5e187558a to your computer and use it in GitHub Desktop.
Save ManojKiranA/c4aeb6b3793fe3ccd2882fb5e187558a to your computer and use it in GitHub Desktop.
//To Get Size of all the Databases
//please Avoid using this it will take very Long time based on the number of databases
$sizeOfAllDataBases = DB::table('information_schema.TABLES')
->select(['TABLE_NAME as TableName','table_rows as TableRows','data_length as DataLength','index_length as IndexLength','TABLE_SCHEMA as DatabaseName'])
->get()
->map(function($eachDatabse){
$dataIndex = $eachDatabse->DataLength + $eachDatabse->IndexLength;
$modifiedObject = new \StdClass;
$kbSize = ($dataIndex/1024);
$mbSize = ($kbSize/1024);
$modifiedObject->SizeInKb = $kbSize;
$modifiedObject->SizeInMB = $mbSize;
return (object)array_merge((array)$eachDatabse,(array)$modifiedObject);
})
->groupBy('DatabaseName')
->map(function($eachDb){
return $eachDb->keyBy('TableName');
});
//To Get Size of Current Application Database
$sizeOfCurrentProjectDatabse = DB::table('information_schema.TABLES')
->select(['TABLE_NAME as TableName','table_rows as TableRows','data_length as DataLength','index_length as IndexLength'])
->where('information_schema.TABLES.table_schema','=',config('database.connections.'.config('database.default').'.database'))
->get()
->map(function($eachDatabse){
$dataIndex = $eachDatabse->DataLength + $eachDatabse->IndexLength;
$modifiedObject = new \StdClass;
$kbSize = ($dataIndex/1024);
$mbSize = ($kbSize/1024);
$modifiedObject->SizeInKb = $kbSize;
$modifiedObject->SizeInMB = $mbSize;
return (object)array_merge((array)$eachDatabse,(array)$modifiedObject);
})
->keyBy('TableName');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment