Skip to main content

What can we do with "Azure Automation"

שלום לכולם
פוסט זה נכתב בהשראת פוסט שכתב  ממיקרוסופט, שם המאמר:


כשקראתי את המאמר הזה פצחתי בריקוד, סוף סוף לא נהייה תלויים במפתחים צהלתי, ועל מה כל המהומה?

אין לנו Agent ב SQL Azure.

איזה DBA יכול לתפקד ללא Agent?

אז עד היום כשרצינו שמשהו ירוץ לפי לוח זמנים ב SQL Azure קבוע, היינו מתחננים למפתחים שיבנו לנו אפליקציה ושתריץ מה שצריך.

ועכשיו סוף סוף יש כלי שמריץ סקריפטים של PS כפי שאנו רוצים על בסיס קבוע.

בדקתי, בחנתי, ניסיתי וזה עובד ועובד טוב.

אולם הבעיה המרכזית כרגע היא שעדיין מדיניות ה Fair Share, מדיניות זו מגבילה הרצות בבסיסי הנתונים עד כחצי שעה, ומדיניות זו עדיין קיימת.

כלומר אם אתה רוצה להריץ בניה של אינדקס על טבלה ענקית זה יעוף אחרי כחצי שעה.

יש המון פרטים, איך יוצרים, איך בודקים, יש הגדרת הרשאות יש יצירת סקריפט ובדיקה שלו ואז מבצעים Publish ונוצר runbook.

ואז ניתן לתת לו לוח זמנים.

אני משתמש בו לעידכוני סטטיסטיקות ול reindex לטבלה מאד חשובה, אולם עדיין לטבלאות גדולות אין אפשרות להריץ תהליכים גדולים.

ה Azure automation נמצא בתפרטי הראשי מתחת ל CDN.


פה שמתי סקריפט שמובא במאמר שבודק גודל של בסיס נתונים ואם צריך לרוקן טבלה מרוקן...

להלן היסטוריה של ג'וב



ואם לוחצים על ההיסטוריה הנה ה output

להלן אצרף סקריפט שעושה update statistics לטבלה אחת , כאשר הסקריפט ניגש לבסיס נתונים Master ומשם עובר על כל בסיסי הנתונים - ויש לנו שם טבלה אחת ועובר עליה ומעדכן סטטיסטיקות.
הסקריפט מקבל 2 פרמטרים - אחד שם של הרשאות ושנית שם של שרת.

לא ארד לפרטים של איך מריצים זה מאד אינטואטיבי וקל.
אשמח לקבל מיילים אם יש בעית.

workflow update_statistcs
{
    param
    (
        # Fully-qualified name of the Azure DB server 
        [parameter(Mandatory=$true)] 
        [string] $SqlServerName,

        # Credentials for $SqlServerName stored as an Azure Automation credential asset
        # When using in the Azure Automation UI, please enter the name of the credential asset for the "Credential" parameter
        [parameter(Mandatory=$true)] 
        [PSCredential] $Credential
    )

    inlinescript
    {

        # Setup credentials   
        $ServerName = $Using:SqlServerName
        $UserId = $Using:Credential.UserName
        $Password = ($Using:Credential).GetNetworkCredential().Password

        # Setup threshold for % of maximum DB size
        $Threshold = 0.8

        # Create connection to Master DB
        $MasterDatabaseConnection = New-Object System.Data.SqlClient.SqlConnection
        $MasterDatabaseConnection.ConnectionString = "Server = $ServerName; Database = Master; User ID = $UserId; Password = $Password;"
        
        $MasterDatabaseConnection.Open();

        # Create command to query the current size of active databases in $ServerName
        $MasterDatabaseCommand = New-Object System.Data.SqlClient.SqlCommand
        $MasterDatabaseCommand.Connection = $MasterDatabaseConnection
        $MasterDatabaseCommand.CommandTimeout = 0
        $MasterDatabaseCommand.CommandText = 
            " 
                SELECT 
                       database_name,
                       storage_in_megabytes [SizeMB]
                FROM 
                       [sys].[databases] as db
                INNER JOIN
                       [sys].[resource_usage] as rs
                ON
                       rs.database_name = db.name
                WHERE
                       [time] = (SELECT Max([time]) FROM [sys].[resource_usage] WHERE database_name = db.name)
                GROUP BY 
                       database_name, storage_in_megabytes
            "
        # Execute reader and return tuples of results <database_name, SizeMB>
        $MasterDbResult = $MasterDatabaseCommand.ExecuteReader()

        # Proceed if there is at least one database
        if ($MasterDbResult.HasRows)
        {
            # Create connection for each individual database
            $DatabaseConnection = New-Object System.Data.SqlClient.SqlConnection
            $DatabaseCommand = New-Object System.Data.SqlClient.SqlCommand

            # Iterate through each database under $ServerName
            while($MasterDbResult.Read())
            {
                $DbName = $MasterDbResult[0]
                $DbSize = $MasterDbResult[1]

                # Apply conditions for user databases (i.e., not master DB)
                if($DbName -ne "Master")
                {
                    # Setup connection string for $DbName
                    $DatabaseConnection.ConnectionString = "Server=$ServerName; Database=$DbName; User ID=$UserId; Password=$Password;"
                    $DatabaseConnection.Open();

                    # Create command for a specific database $DBName
                    $DatabaseCommand.CommandTimeout = 0
                    $DatabaseCommand.Connection = $DatabaseConnection
                    $DatabaseCommand.CommandText = " update statistics DataRetention  with sample 10 PERCENT;"

                    # Execute query and return single scalar result 
                    $DbResultBytes = $DatabaseCommand.ExecuteScalar()
                    $MaxDbSizeMB = $DbResultBytes/(1Mb)

                    
                    # When the current $DbSize is greater than a percentage ($Threshold) of the $MaxDbSizeMB
                    # then perform a certain action, in this example, truncate a table on that database
                     
                        $start=Get-Date
                        Write-Output "Perform update statistics NewsFeedStream.Streams on $DbName :  $start"
                     

                    # Close connection to $DbName
                    $DatabaseConnection.Close()        
                }
            }
        } 

        # Close connection to Master DB
        $MasterDatabaseConnection.Close() 
    }    
}

Comments

Popular posts from this blog

ועוד קצת על ניהול פיתוח לענן

היום עקב תקלה קטנה מול מיקרוסופט בוצע disable לחשבון. הדבר גרם לאתר לא לעבוד וכמובן 3 רולים נוטרלו. כשחזרו לחיים נדרשנו לעשות מחדש deploy ל 3 הרולים. (רוצים הסבר קטן לעבודה על הענן? ובכן תמצית הדבר הוא שכשאנו עוקפים נהלים שאנו יצרנו בשרתים שלנו מיקרוסופט - לא מרשים לעקוף וכך הכל חייב להתנהל לפי הספר... מה שתעלה לענן זה מה שירוץ ואם תשנה - השינויים יימחקו...) הבעיה החלה כאשר הסתבר שלא כל קבצי ה deploy נשמרו על מכונת הגירסה וכי אחד הקבצים שודרג לגירסא חדשה שטרם עלתה לענן.... הדבר גזל 4 שעות בנסיון להחזיר את הגירסה... מסקנתי היא כי חייב להיות נוהל שמירת קבצי deploy מיד אחרי העלתם לענן - ובכך לשמור גיבוי לעת צרה - נכון - אל תצעקו עליי - בוצע לייבל ב TFS - ואפשר למשוך ולקמפל - אבל תראו לי עובד אחד שעשה את זה תוך חמש דקות....? יש לציין לטובה את ה SQL Azure - שלו - לא קרה כלום כל העת... כל הכבוד ל SQL... ובנימה יותצר רצינית - אל תשכחו לגבות כל מה שעולה ... - במיוחד אצלך . אגב בענן עצמו - זה כבר יגובה אל דאגה... ערב טוב

על בעיות של ניהול פיתוח לענן

על ניהול סביבת פיתוח מול הענן:   הבעיה המרכזית בניהול פיתוח לענן שייכת לתחום הבדיקות  - שום ענן מקומי ושם אימולטור אינו מדמה במאה אחוזים את מה שקורה בענן עצמו. בכל רכיבי הבדיקות, על בעיה זו ניתן להתגבר בשיטת עבודה טובה והקמת מערכת בדיקות בענן עצמו. על ניהול גרסאות מול הענן:    במידה ואתם עובדים מול לקוחות רגילים ומול לקוחות הרוצים מוצרים בענן  - מהי הדרך הטובה ביותר לנהל את הפיתוח כך שאפשר יהיה לתחזק את שתי המערכות ואת שתי סביבות הבדיקות? אפשר לומר כי מטרת מנהל הפיתוח היא להקים סביבת פיתוח אחת - אם הדבר לא אפשרי צריך למצוא את הפתרון לסינכרון 2 הסביבות. Check List -   למנהל המבולבל - מה הצוות צריך לבצע לפני העלאה לענן: על הפרוייקט להיות מקומפל בסביבת VS2010 - רצוי 64 Bits ולא 32. יש להריץ בענן מקומי (אימולטור) ולראות שהכול עובד כהלכה במידה ואתה משתמש ב Registery או ב Event Log עליך ליצור קובץ StartUp command שבעצם ירוץ בעליית ה Role וייצור את מה שצריך במחשב המיועד לך בענן. יש ליצור חבילה להעלאה - רצוי לשמור חבילה זו עם מספר ותיאור כללי. יש להעלות את החבילה ולבדוק שהכול רץ ועו

Azure SQL DB tiers comparison

Hi All In the last few month Brent Ozar gae us 2 masterpiece blogs related to Azure SQL DB:   How fast can a $21,468/mo Azure SQL DB load data?     In this blog Brent compared the abilities of Azure SQL DBs to load Data - he compared all combinations of vCors tiers. (When I asked him about comparing the Standard\Premium tiers, he told me to do it.... :-) )   There’s a bottleneck in Azure SQL DB storage throughput.   In this blog Brent showed us that in the vCors world the storage throughput has limit and there is not need to pay so much money when you need to upload lots of data.   So I took have taken up his challenge and done a comparison in Azure SQL DB in Standard\Premium tiers. I have created a new DB with 1 Table. I have generated 7 GB of DATA, and created the file in my local on premise drive (Yes, do not kill me, I did not had the time to put it on azure), and uploaded it via BCP command.   bcp "TableName" in "T:\MyTable.bcp"