Home
%3CLINGO-SUB%20id%3D%22lingo-sub-775177%22%20slang%3D%22en-US%22%3EUsing%20Query%20Store%20with%20least%20privileges%20instead%20of%20db_owner%20to%20achieve%20Separation%20of%20Duties%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-775177%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20using%20the%20Query%20Store%20in%20a%20production%20environment%2C%20sometimes%20customers%20need%20to%20delegate%20very%20specifically%20who%20can%20do%20what%20in%20terms%20of%20Performance%20Analysis%20and%2For%20Tuning.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3EThe%20Query%20Store%20is%20a%20component%20that%20can%20be%20activated%20per%20database%20and%20is%20used%20for%20troubleshooting%20query%20performance.%20If%20you%20want%20to%20understand%2C%20how%20Query%20Store%20works%20and%20how%20it%20can%20help%2C%20you%20can%20start%20with%20this%20page%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fperformance%2Fmonitoring-performance-by-using-the-query-store%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMonitoring%20performance%20by%20using%20the%20Query%20Store%3C%2FA%3E.%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20article%20we%20will%20demonstrate%2C%20how%20customers%20can%20segregate%20the%20typical%20activities%20around%20the%20Query%20Store%20and%20delegate%20minimal%20sets%20of%20permissions%20to%20groups%20of%20users%20that%20are%20mandated%20to%20certain%20tasks.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1794341756%22%20id%3D%22toc-hId-1794341756%22%3EWho%20and%20what%3A%20Identify%20roles%3C%2FH2%3E%0A%3CP%3EWhen%20it%20comes%20to%20security%2C%20to%20generalize%2C%20it%20helps%20to%20%3CU%3Efirst%20identify%20the%20roles%3C%2FU%3Ethat%20will%20work%20with%20the%20feature%20in%20question.%3C%2FP%3E%0A%3CP%3E-%20I%20am%20using%20the%20term%20%3CSTRONG%3E%3CEM%3Erole%3C%2FEM%3E%3C%2FSTRONG%3Eas%20a%20term%20to%20summarize%20tasks%20that%20are%20similar%20and%20will%20most%20likely%20be%20undertaken%20by%20the%20same%20group%20of%20people.%20Usually%20it%20will%20end%20up%20being%20represented%20as%20a%20database%20role%20in%20SQL.%3C%2FP%3E%0A%3CP%3ETypically%2C%20there%20are%203%20aspects%20to%20the%20work%20with%20the%20Query%20Store%2C%20which%20can%20be%20reflected%20in%20roles%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E1)%20Configuration%20%E2%80%93%20turning%20Query%20Store%20on%20and%20off%2C%20clearing%20the%20contents%2C%20flushing%20its%20contents%20to%20disk%20and%20changing%20its%20settings.%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E2)%20Viewing%20the%20reports%20or%20using%20the%20DMVs%20to%20analyze%20the%20Query%20Store%20contents%20(queries%2C%20plans%20and%20wait%20statistics)%20to%20gain%20insights%20but%20not%20necessarily%20having%20the%20authority%20to%20change%20anything%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E3)%20Actively%20change%20Plans%20by%20forcing%20or%20un-forcing%2C%20based%20on%20the%20information%20obtained%20from%20(2)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYour%20mileage%20may%20vary.%20Most%20of%20the%20times%2C%20roles%202%20and%203%20will%20be%20fulfilled%20by%20the%20same%20group%20of%20people.%20In%20smaller%20environments%20with%20less%20segregation%20of%20duties%2C%20everything%20will%20be%20in%20the%20same%20hands.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--757815205%22%20id%3D%22toc-hId--757815205%22%3ERequired%20Permissions%3C%2FH2%3E%0A%3CP%3EOnce%20we%20know%20what%20we%20need%20to%20separate%20permission-wise%2C%20we%20can%20identify%20the%20minimally%20required%20permissions%20to%20adhere%20to%20the%20principle%20of%20least%20privileges.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CEM%3EQuick%20Takeaway%3C%2FEM%3E%3CBR%20%2F%3EBy%20now%2C%20you%20may%20realize%2C%20that%20there%20are%202%20very%20fundamental%20security%20principals%20guiding%20this%20article%3A%20%3CSTRONG%3E%3CEM%3ESeparation%20of%20Duties%20(SoD)%20%3C%2FEM%3E%3C%2FSTRONG%3Eand%3CSTRONG%3E%3CEM%3EPrinciple%20of%20Least%20Privileges%20(PoLP)%3C%2FEM%3E%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ERole%201%3C%2FSTRONG%3E%2C%20let%E2%80%99s%20call%20it%20%3CSTRONG%3E%3CEM%3EDatabaseAdmin%3C%2FEM%3E%3C%2FSTRONG%3E(indicating%20that%20most%20likely%20this%20will%20not%20be%20the%20only%20duty%20of%20the%20role%20in%20real%20life)%20will%20require%20to%20run%20commands%20such%20as%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EALTER%20DATABASE%20QS_SoD%20SET%20QUERY_STORE%20%3D%20ON%0A-%20To%20turn%20on%20the%20Query%20Store.%20Note%3A%20In%20Azure%20SQL%20Database%2C%20Query%20Store%20is%20enabled%20by%20default.%0AALTER%20DATABASE%20%5BQS_SoD%5D%20SET%20QUERY_STORE%20(MAX_STORAGE_SIZE_MB%20%3D%20200)%0AALTER%20DATABASE%20%5BQS_SoD%5D%20SET%20QUERY_STORE%20(QUERY_CAPTURE_MODE%20%3D%20ALL)%0A-%20To%20change%20the%20size%20or%20the%20capture%20mode.%0AALTER%20DATABASE%20%5BQS_SoD%5D%20SET%20QUERY_STORE%20CLEAR%20ALL%0A-%20Purging.%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20essence%3A%20All%20these%20configuration-options%20are%20part%20of%20the%20ALTER%20DATABASE-command.%3C%2FP%3E%0A%3CP%3EThere%20is%20one%20more%20thing%3A%20Flushing%20the%20contents%20to%20disk%20that%20have%20not%20yet%20been%20written%20yet%20to%20disk.%20This%20is%20done%20using%20the%20stored%20procedure%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-query-store-flush-db-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esp_query_store_flush_db%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EThe%20minimum%20permission%20required%20for%20all%20the%20above%2C%20including%20execution%20of%20the%20provided%20stored%20procedure%3A%20%3CSTRONG%3EALTER%20on%20the%20Database%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CEM%3EQuick%20Takeaway%3C%2FEM%3E%3CBR%20%2F%3EIn%20SQL%20Server%20or%20Azure%20SQL%20Database%20Managed%20Instance%2C%20you%20can%20grant%20%3CSTRONG%3EALTER%3C%2FSTRONG%3Eon%20a%20Database%20in%202%20ways%3A%20Besides%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fgrant-database-permissions-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDatabase%20Permission%3C%2FA%3E%E2%80%9CALTER%E2%80%9D%2C%20there%20is%20also%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fgrant-server-permissions-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EServer%20Permission%3C%2FA%3E%E2%80%9C%3CSTRONG%3EALTER%20ANY%20DATABASE%3C%2FSTRONG%3E%E2%80%9D.%20The%20Server%20Permission%20would%20make%20sense%20to%20use%20when%20you%20want%20to%20grant%20these%20permissions%20to%20all%20databases%20on%20a%20given%20SQL%20Instance%20in%20one%20command.%20It%20has%20to%20be%20granted%20to%20a%20Server%20Principal%20(ideally%20a%20custom%20Server%20role).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ERole%202%3C%2FSTRONG%3E%2C%20let%E2%80%99s%20call%20it%20%3CSTRONG%3E%3CEM%3EQueryStoreReader%3C%2FEM%3E%3C%2FSTRONG%3Ewill%20want%20to%20use%20SSMS%20to%20use%20the%20built-in%20reports%2C%20but%20also%20need%20access%20to%20the%20Query%20Store%20DMVs%20for%20more%20complex%20and%20bulk%20analysis.%20Since%20the%20SSMS%20reports%20are%20based%20on%20the%20DMVs%20this%20is%20covered%20by%20the%20same%20permission.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20700px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F125004iCA272C389A340855%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22QS_SSMS_Reports_m.jpg.png%22%20title%3D%22QS_SSMS_Reports_m.jpg.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20list%20of%20the%20Query%20Store%20DMVs%20is%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-database-query-store-options-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.database_query_store_options%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-query-context-settings-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.query_context_settings%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-query-store-plan-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.query_store_plan%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-query-store-query-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.query_store_query%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-query-store-query-text-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.query_store_query_text%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-query-store-wait-stats-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.query_store_wait_stats%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-query-store-runtime-stats-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.query_store_runtime_stats%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fsys-query-store-runtime-stats-interval-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.query_store_runtime_stats_interval%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThe%20minimum%20permission%20required%20for%20all%20the%20above%3A%20%3CSTRONG%3EVIEW%20DATABASE%20STATE%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CEM%3ENote%20on%20Azure%20SQL%20Database%3C%2FEM%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3EIf%20you%20are%20using%20the%20Azure%20Portal%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-query-performance%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EIntelligent%20Performance%3C%2FA%3Efor%20analyzing%20performance%20and%20gathering%20recommendation%2C%20this%20is%20independent%20from%20the%20Database%20permission%20within%20the%20SQL%20engine.%20Activity%20in%20the%20portal%20is%20controlled%20with%20Azure%20RBAC%20roles.%20This%20will%20be%20subject%20in%20another%20article.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ERole%203%3C%2FSTRONG%3E%2C%20let%E2%80%99s%20call%20it%20%3CSTRONG%3E%3CEM%3ETuningTeam%3C%2FEM%3E%3C%2FSTRONG%3Eis%20the%20one%20who%20can%20actively%20change%20query%20plans%20by%20forcing%20and%20enforcing%20plans%20through%20the%20Query%20Store%20in%20an%20easy%20manner.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CEM%3EQuick%20Takeaway%3C%2FEM%3E%3CBR%20%2F%3EAs%20a%20best%20practice%20this%20should%20be%20rather%20the%20exception%20or%20a%20quick%20fix%20until%20the%20query%20or%20indexes%20have%20been%20tuned%20in%20a%20way%20that%20the%20Query%20Optimizer%20can%20again%20automatically%20come%20up%20with%20a%20good%20plan.%20Therefore%2C%20it%20especially%20makes%20sense%20to%20not%20grant%20too%20many%20people%20this%20permission.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EForcing%20a%20plan%20can%20be%20accomplished%20via%20the%20Query%20Store%20Reports%2C%20or%20by%20calling%20the%20respective%20stored%20procedure%20directly.%3C%2FP%3E%0A%3CP%3EThe%20following%20stored%20procedures%20are%20actively%20changing%20plans%20or%20the%20stored%20contents%20for%20a%20specific%20plan%20and%20most%20likely%20make%20sense%20to%20be%20granted%20to%20this%20role%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-query-store-force-plan-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esp_query_store_force_plan%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-query-store-remove-plan-transct-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esp_query_store_remove_plan%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-query-store-remove-query-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esp_query_store_remove_query%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-query-store-reset-exec-stats-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esp_query_store_reset_exec_stats%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20style%3D%22font-weight%3A%20400%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-query-store-unforce-plan-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esp_query_store_unforce_plan%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThe%20minimum%20permission%20required%20for%20all%20the%20above%3A%20%3CSTRONG%3EALTER%20on%20the%20Database%3CBR%20%2F%3E%3C%2FSTRONG%3E(Besides%20this%2C%20the%20TuningTeam%20also%20needs%20to%20be%20able%20to%20see%20the%20information%20from%20the%20Query%20Store%2C%20hence%20VIEW%20DATABASE%20STATE%20as%20well.)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-984995130%22%20id%3D%22toc-hId-984995130%22%3EThe%20issue%3A%20high%20privileges%20required%3C%2FH2%3E%0A%3CP%3EThe%20requirement%20for%20the%20ALTER-Permission%20on%20Database%20for%20the%20TuningTeam%20is%20the%20obvious%20issue%3A%20With%20the%20current%20set%20of%20permissions%20required%20for%20the%20Query%20Store%20operations%2C%20%3CSTRONG%3Eseparating%20DatabaseAdmins%20from%20a%20TuningTeam%20is%20not%20possible%20out%20of%20the%20box%3C%2FSTRONG%3E.%20This%20is%20where%20we%20need%20to%20use%20a%20certain%20technique%20for%20customizing%20granular%20permissions%3A%20wrapping%20commands%20into%20special%20stored%20procedures.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CEM%3EQuick%20Takeaway%3C%2FEM%3E%3CBR%20%2F%3EThe%20ALTER-Permission%20on%20a%20Database%20currently%20grants%2054%20permissions%2C%20which%20is%20not%20only%20covering%20Query%20Store%20Configuration%20but%20also%20changing%20other%20database%20options%20up%20to%20creating%2C%20changing%20and%20dropping%20almost%20all%20database%20objects.%20It%20is%20still%20less%20than%20the%2081%20permissions%20that%20CONTROL%20implies%2C%20but%20it%20implies%20a%20variety%20of%20methods%20that%20would%20enable%20even%20further%20elevation%20of%20privileges.%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1567161831%22%20id%3D%22toc-hId--1567161831%22%3EThe%20Solution%3A%20Wrapping%20Code%20in%20stored%20procedures%20with%20EXECUTE%20AS%3C%2FH2%3E%0A%3CP%3EThe%20following%20solution%20has%20proven%20to%20be%20very%20handy%20many%20scenario%20since%20it%20was%20introduced%20in%20SQL%20Server%202005%3A%20%3CSTRONG%3EUsing%20the%20%3C%2FSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fexecute-as-clause-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSTRONG%3EEXECUTE%20AS%20clause%3C%2FSTRONG%3E%3C%2FA%3E%3CSTRONG%3Eof%20a%20custom%20stored%20procedure%20that%20contains%20the%20commands%20that%20require%20high%20privileges%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3EIn%20our%20scenario%20the%205%20%E2%80%9Ccommands%E2%80%9D%20in%20question%20are%20in%20fact%20stored%20procedures%20themselves.%20(sp_query_store_force_plan%2C%20sp_query_store_remove_plan%2C%20sp_query_store_remove_query%2C%20sp_query_store_reset_exec_stats%2C%20sp_query_store_unforce_plan).%3C%2FP%3E%0A%3CP%3EAll%20of%20them%20are%20executed%20with%20at%20least%20one%20parameter%2C%20either%20%3CEM%3Equery_id%3C%2FEM%3Eor%20%3CEM%3Eplan_id%3C%2FEM%3Eor%20both.%20No%20defaults.%20Therefore%2C%20it%20is%20very%20simple%20up%20to%20trivial%20to%20handle%20and%20the%20given%20example%20can%20be%20adopted%20without%20major%20changes%20to%20all%20the%20other%20four%20stored%20procedures.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20concept%20is%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E1)%20We%20need%20to%20have%20a%20database%20principal%20with%20the%20required%20privileges%20to%20run%20the%20commands%2Fstored%20procedures%3A%20ALTER%20DATABASE%3CBR%20%2F%3E-%20This%20principal%20should%20not%20be%20used%20for%20interactive%20work%2C%20and%20hence%20will%20be%20created%20without%20login%20and%20password.%3CBR%20%2F%3EIn%20the%20provided%20example%20script%20this%20user%20is%20called%20%3CEM%3Einternal_principal_ALTER_DB%3C%2FEM%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2060px%3B%22%3E%3CEM%3EQuick%20Takeaway%3C%2FEM%3E%3CBR%20%2F%3EAs%20a%20Best%20Practice%20that%20helps%20to%20simplify%20analyzing%20and%20reporting%20on%20given%20permissions%2C%20I%20always%2C%20without%20any%20exceptions%20use%20roles%20and%20%3CSTRONG%3Egrant%20permissions%20to%20roles%20exclusively%3C%2FSTRONG%3E.%20This%20is%20why%20in%20this%20example%20I%20am%20taking%20this%20extra%20step%20that%20required%20just%202%20commands%20more%20(CREATE%20and%20ALTER%20role)%20as%20well.In%20the%20provided%20example%20script%20this%20role%20is%20called%20role_internal_principal_ALTER_DB.%20(Not%20%E2%80%9Cpretty%E2%80%9D%2C%20but%20concise%20I%20hope)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E2)%20We%20create%20a%20stored%20procedure%20with%20EXECUTE%20AS%20using%20the%20principal%20created%20to%20specify%20the%20context%20under%20which%20the%20module%20is%20being%20run.%20This%20requires%20no%20extra%20permissions%20for%20the%20caller%20of%20the%20stored%20procedure%20aside%20from%20the%20EXECUTE-Permission%20itself.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ECREATE%20OR%20ALTER%20PROCEDURE%20db_tuning_tools.up_sp_query_store_force_plan%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%40query_id%20bigint%0A%20%20%20%20%20%20%20%2C%20%20%20%20%20%20%40plan_id%20bigint%0AWITH%20EXECUTE%20AS%20'internal_principal_ALTER_DB'%0AAS%0AEXECUTE%20sp_query_store_force_plan%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%40query_id%20%20%20%20%20%3D%20%40query_id%0A%20%20%20%20%20%20%20%2C%20%20%20%20%20%20%40plan_id%20%20%20%20%20%20%3D%20%40plan_id%0AGO%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E3)%20The%20stored%20procedure%20contains%20one%20of%20the%20listed%20stored%20procedures%20and%20passes%20any%20parameters%20on%20to%20it.%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E-%20As%20a%20benefit%20this%20enables%20you%20to%20add%20additional%20operational%20or%20business%20logic%20like%20for%20example%3A%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2060px%3B%22%3EA)%20Log%20Who%20and%20When%20the%20action%20was%20attempted%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2060px%3B%22%3EB)%20Include%20further%20checks%20like%20%E2%80%9CBlock%20execution%20during%20critical%20business%20hours%E2%80%9D%20or%20require%20a%202%3CSUP%3End%3C%2FSUP%3Eeye%20approval%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2060px%3B%22%3EC)%20Deferring%20the%20execution%20by%20applying%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fdatabase-engine%2Fconfigure-windows%2Fsql-server-service-broker%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EService%20Broker%3C%2FA%3Equeuing%20instead%20of%20direct%20execution%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3EJust%20to%20name%20a%20few%20further%20possibilities.%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E4)%20Grant%20the%20Execute-Permission%20on%20the%20wrapper%20procedures%20to%20the%20TuningTeam-Role.%3CBR%20%2F%3EIn%20the%20provided%20example%20script%20this%20role%20is%20called%20%3CEM%3ERole_TuningTeam%3C%2FEM%3E.%3CBR%20%2F%3EThis%20should%20not%20be%20done%20on%20a%20per%20object%20base%20but%20on%20a%20schema-scope.%20We%20also%20want%20to%20make%20sure%20not%20to%20use%20any%20existing%20schema%20to%20interfere%20with%20other%20objects.%20Hence%20the%20cleanest%20way%20is%20to%20create%20a%20separate%20schema%20for%20those%20special%20procedures%20that%20the%20TuningTeam%20needs%20access%20to.%3CBR%20%2F%3EIn%20the%20provided%20example%20script%20the%20schema%20name%20is%20%3CEM%3Edb_tuning_tools%3C%2FEM%3E.%3CBR%20%2F%3EBesides%20access%20to%20these%20procedures%2C%20the%20TuningTeam%20also%20needs%20to%20be%20able%20to%20view%20the%20contents%20of%20the%20query%20store%2C%20which%20requires%20the%20VIEW%20DATABASE%20STATE-permission.%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3EDrawback%3C%2FEM%3E%3CBR%20%2F%3EOne%20little%20downside%20I%20shall%20point%20out%20though%3A%20when%20using%20this%20kind%20of%20solution%2C%20namely%20instead%20of%20using%20the%20built-in%20stored%20procedures%20or%20commands%20executing%20custom%20code%2C%20the%20graphical%20UI%20of%20Management%20Studio%20cannot%20be%20used%20any%20more.%20Precisely%3A%20Forcing%20or%20Unforcing%20plans%20via%20the%20Query%20Store%20reports.%20The%20Reports%20themselves%20of%20course%20can%20still%20be%20opened.%20But%20trying%20to%20force%20a%20plan%20via%20the%20UI%20will%20simply%20result%20in%20a%20permission%20error%20and%20the%20TuningTeam%20in%20this%20case%20needs%20to%20be%20educated%20to%20use%20the%20custom%20procedures%20for%20those%20actions.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20650px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F125066iE06842BC280A9E3F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22QueryStore_ForcePlan_Permission_Error_m.jpg%22%20title%3D%22QueryStore_ForcePlan_Permission_Error_m.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-175648504%22%20id%3D%22toc-hId-175648504%22%3EConclusion%3C%2FH2%3E%0A%3CP%3EGranting%20access%20to%20just%20parts%20of%20the%20query%20store%20functionalities%20can%20be%20implemented%20with%20very%20little%20extra%20work%20using%20wrapper%20stored%20procedures%20with%20EXECUTE%20AS.%3C%2FP%3E%0A%3CP%3EYou%20can%20find%20the%20example%20solution%20in%20the%20%3CSTRONG%3Eattached%20scripts%20below%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHappy%20Tuning%20and%20Securing%3C%2FP%3E%0A%3CP%3EAndreas%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1721945334%22%20id%3D%22toc-hId-1721945334%22%3ELinks%20from%20the%20article%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fperformance%2Fmonitoring-performance-by-using-the-query-store%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMonitoring%20performance%20by%20using%20the%20Query%20Store%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fgrant-database-permissions-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDatabase%20Permissions%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fgrant-server-permissions-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EServer%20Permissions%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fquery-store-stored-procedures-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EQuery%20Store%20Stored%20Procedures%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-catalog-views%2Fquery-store-catalog-views-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EQuery%20Store%20Catalog%20Views%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-query-performance%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EQuery%20Performance%20Insight%20for%20Azure%20SQL%20Database%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Frole-based-access-control%2Fbuilt-in-roles%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EBuilt-in%20roles%20for%20Azure%20resources%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fexecute-as-clause-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EEXECUTE%20AS%20Clause%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fdatabase-engine%2Fconfigure-windows%2Fsql-server-service-broker%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EService%20Broker%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-775177%22%20slang%3D%22en-US%22%3E%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3EHere%20we%20are%20showing%20how%20access%20to%20the%20Query%20Store%20can%20be%20delegated%20without%20granting%20high%20privileges.%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3EUsing%20Query%20Store%20with%20least%20privileges%20instead%20of%20db_owner%20to%20achieve%20Separation%20of%20Duties%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-775177%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESecurity%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

When using the Query Store in a production environment, sometimes customers need to delegate very specifically who can do what in terms of Performance Analysis and/or Tuning.

 

The Query Store is a component that can be activated per database and is used for troubleshooting query performance. If you want to understand, how Query Store works and how it can help, you can start with this page: Monitoring performance by using the Query Store.

 

In this article we will demonstrate, how customers can segregate the typical activities around the Query Store and delegate minimal sets of permissions to groups of users that are mandated to certain tasks.

 

Who and what: Identify roles

When it comes to security, to generalize, it helps to first identify the roles that will work with the feature in question.

- I am using the term role as a term to summarize tasks that are similar and will most likely be undertaken by the same group of people. Usually it will end up being represented as a database role in SQL.

Typically, there are 3 aspects to the work with the Query Store, which can be reflected in roles:

 

1) Configuration – turning Query Store on and off, clearing the contents, flushing its contents to disk and changing its settings.

2) Viewing the reports or using the DMVs to analyze the Query Store contents (queries, plans and wait statistics) to gain insights but not necessarily having the authority to change anything

3) Actively change Plans by forcing or un-forcing, based on the information obtained from (2)

 

Your mileage may vary. Most of the times, roles 2 and 3 will be fulfilled by the same group of people. In smaller environments with less segregation of duties, everything will be in the same hands.

 

Required Permissions

Once we know what we need to separate permission-wise, we can identify the minimally required permissions to adhere to the principle of least privileges.

 

Quick Takeaway
By now, you may realize, that there are 2 very fundamental security principals guiding this article: Separation of Duties (SoD) and Principle of Least Privileges (PoLP).

 

Role 1, let’s call it DatabaseAdmin (indicating that most likely this will not be the only duty of the role in real life) will require to run commands such as:

 

ALTER DATABASE QS_SoD SET QUERY_STORE = ON
- To turn on the Query Store. Note: In Azure SQL Database, Query Store is enabled by default.
ALTER DATABASE [QS_SoD] SET QUERY_STORE (MAX_STORAGE_SIZE_MB = 200)
ALTER DATABASE [QS_SoD] SET QUERY_STORE (QUERY_CAPTURE_MODE = ALL)
- To change the size or the capture mode.
ALTER DATABASE [QS_SoD] SET QUERY_STORE CLEAR ALL
- Purging.

 

In essence: All these configuration-options are part of the ALTER DATABASE-command.

There is one more thing: Flushing the contents to disk that have not yet been written yet to disk. This is done using the stored procedure sp_query_store_flush_db.

The minimum permission required for all the above, including execution of the provided stored procedure: ALTER on the Database

 

Quick Takeaway
In SQL Server or Azure SQL Database Managed Instance, you can grant ALTER on a Database in 2 ways: Besides the Database Permission “ALTER”, there is also a Server PermissionALTER ANY DATABASE”. The Server Permission would make sense to use when you want to grant these permissions to all databases on a given SQL Instance in one command. It has to be granted to a Server Principal (ideally a custom Server role).

 

Role 2, let’s call it QueryStoreReader will want to use SSMS to use the built-in reports, but also need access to the Query Store DMVs for more complex and bulk analysis. Since the SSMS reports are based on the DMVs this is covered by the same permission.

QS_SSMS_Reports_m.jpg.png

The list of the Query Store DMVs is:

The minimum permission required for all the above: VIEW DATABASE STATE

 

Note on Azure SQL Database

If you are using the Azure Portal Intelligent Performance for analyzing performance and gathering recommendation, this is independent from the Database permission within the SQL engine. Activity in the portal is controlled with Azure RBAC roles. This will be subject in another article.

 

Role 3, let’s call it TuningTeam is the one who can actively change query plans by forcing and unforcing plans through the Query Store in an easy manner.

 

Quick Takeaway
As a best practice this should be rather the exception or a quick fix until the query or indexes have been tuned in a way that the Query Optimizer can again automatically come up with a good plan. Therefore, it especially makes sense to not grant too many people this permission.

 

Forcing a plan can be accomplished via the Query Store Reports, or by calling the respective stored procedure directly.

The following stored procedures are actively changing plans or the stored contents for a specific plan and most likely make sense to be granted to this role:

The minimum permission required for all the above: ALTER on the Database
(Besides this, the TuningTeam also needs to be able to see the information from the Query Store, hence VIEW DATABASE STATE as well.)

 

The issue: high privileges required

The requirement for the ALTER-Permission on Database for the TuningTeam is the obvious issue: With the current set of permissions required for the Query Store operations, separating DatabaseAdmins from a TuningTeam is not possible out of the box. This is where we need to use a certain technique for customizing granular permissions: wrapping commands into special stored procedures.

 

Quick Takeaway
The ALTER-Permission on a Database currently grants 54 permissions, which is not only covering Query Store Configuration but also changing other database options up to creating, changing and dropping almost all database objects. It is still less than the 81 permissions that CONTROL implies, but it implies a variety of methods that would enable even further elevation of privileges.

 

The Solution: Wrapping Code in stored procedures with EXECUTE AS

The following solution has proven to be very handy many scenario since it was introduced in SQL Server 2005: Using the EXECUTE AS clause of a custom stored procedure that contains the commands that require high privileges.

In our scenario the 5 “commands” in question are in fact stored procedures themselves. (sp_query_store_force_plan, sp_query_store_remove_plan, sp_query_store_remove_query, sp_query_store_reset_exec_stats, sp_query_store_unforce_plan).

All of them are executed with at least one parameter, either query_id or plan_id or both. No defaults. Therefore, it is very simple up to trivial to handle and the given example can be adopted without major changes to all the other four stored procedures.

 

The concept is as follows:

 

1) We need to have a database principal with the required privileges to run the commands/stored procedures: ALTER DATABASE
- This principal should not be used for interactive work, and hence will be created without login and password.
In the provided example script this user is called internal_principal_ALTER_DB.

 

Quick Takeaway
As a Best Practice that helps to simplify analyzing and reporting on given permissions, I always, without any exceptions use roles and grant permissions to roles exclusively. This is why in this example I am taking this extra step that required just 2 commands more (CREATE and ALTER role) as well.
In the provided example script this role is called role_internal_principal_ALTER_DB. (Not “pretty”, but concise I hope)

 

 

2) We create a stored procedure with EXECUTE AS using the principal created to specify the context under which the module is being run. This requires no extra permissions for the caller of the stored procedure aside from the EXECUTE-Permission itself.

 

CREATE OR ALTER PROCEDURE db_tuning_tools.up_sp_query_store_force_plan
              @query_id bigint
       ,      @plan_id bigint
WITH EXECUTE AS 'internal_principal_ALTER_DB'
AS
EXECUTE sp_query_store_force_plan
              @query_id     = @query_id
       ,      @plan_id      = @plan_id
GO

 

3) The stored procedure contains one of the listed stored procedures and passes any parameters on to it.

- As a benefit this enables you to add additional operational or business logic like for example:

A) Log Who and When the action was attempted

B) Include further checks like “Block execution during critical business hours” or require a 2nd eye approval

C) Deferring the execution by applying Service Broker queuing instead of direct execution

Just to name a few further possibilities.

 

4) Grant the Execute-Permission on the wrapper procedures to the TuningTeam-Role.
In the provided example script this role is called Role_TuningTeam.
This should not be done on a per object base but on a schema-scope. We also want to make sure not to use any existing schema to interfere with other objects. Hence the cleanest way is to create a separate schema for those special procedures that the TuningTeam needs access to.
In the provided example script the schema name is db_tuning_tools.
Besides access to these procedures, the TuningTeam also needs to be able to view the contents of the query store, which requires the VIEW DATABASE STATE-permission.

 

Drawback
One little downside I shall point out though: when using this kind of solution, namely instead of using the built-in stored procedures or commands executing custom code, the graphical UI of Management Studio cannot be used any more. Precisely: Forcing or Unforcing plans via the Query Store reports. The Reports themselves of course can still be opened. But trying to force a plan via the UI will simply result in a permission error and the TuningTeam in this case needs to be educated to use the custom procedures for those actions.

QueryStore_ForcePlan_Permission_Error_m.jpg

Conclusion

Granting access to just parts of the query store functionalities can be implemented with very little extra work using wrapper stored procedures with EXECUTE AS.

You can find the example solution in the attached scripts below.

 

Happy Tuning and Securing

Andreas

 

Links from the article