How to get sql database space using KQL?

Mark A. Pizarro 0 Reputation points
2023-02-09T20:30:43.8766667+00:00

How to get sql database space using KQL?

Azure SQL Database
{count} votes

4 answers

Sort by: Most helpful
  1. Mark A. Pizarro 0 Reputation points
    2023-02-09T21:06:23.7833333+00:00

    Yes, total space used for a SQL database as well total space.

    space used = 2.1GB

    total space = 10GB

    Thanks, Mark

    0 comments No comments

  2. Oury Ba-MSFT 20,911 Reputation points Microsoft Employee Moderator
    2023-02-09T23:53:46.77+00:00

    Hi Mark A. Pizarro

    Thank you for getting back.

    You can map to a SQL table...Querying an external table of type SQL.

    You can also pack that into a request, you can use sql_request plugin.

    Please let me know if that works for you.

    Regards,

    Oury

    0 comments No comments

  3. Mark Pizarro 0 Reputation points
    2023-02-10T15:40:59.4966667+00:00

    Hi Oury,

    Thank you for your answers but I was looking something along the lines of

    AzureMetrics

    | where ResourceProvider == "MICROSOFT.SQL" and Resource != "MASTER"

    | where TimeGenerated > ago(24h)

    | where MetricName == "storage"

    | summarize avg(Average) by Resource, bin(TimeGenerated, 1h)

    | extend AverageMB = avg_Average / 1000000

    | project TimeGenerated, Resource, AverageMB

    But this is not outputting anything.

    User's image

    If this worked I was looking to replace Resource with name of database.

    Thanks,

    Mark

    0 comments No comments

  4. Oury Ba-MSFT 20,911 Reputation points Microsoft Employee Moderator
    2023-02-14T21:52:58.64+00:00

    Hi Mark A. Pizarro

    As suggested above, you can use KQL's sql_request plugin. You can run SQL queries through it, so something like this should give you what you're looking for.

    evaluate sql_request(

    '<SQL DB CONNECTION STRING>',

    "SELECT DB_NAME() AS DB, size as TotalSpace , size - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT) AS FreeSpace FROM sys.database_files where type_desc = 'ROWS'"

    Hope that helps.

    Let me know the result and please do not forget to mark as accept answer if the reply was helpful.

    Regards,

    Oury


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.