{"id":19561,"date":"2024-05-31T15:08:21","date_gmt":"2024-05-31T18:08:21","guid":{"rendered":"https:\/\/www.fabriciolima.net\/?p=19561"},"modified":"2024-05-31T15:08:21","modified_gmt":"2024-05-31T18:08:21","slug":"azure-sql-copilot-02-qual-query-o-copilot-usa-para-validar-a-performance-do-bd","status":"publish","type":"post","link":"https:\/\/fabriciolima.net\/blog\/2024\/05\/31\/azure-sql-copilot-02-qual-query-o-copilot-usa-para-validar-a-performance-do-bd\/","title":{"rendered":"Azure SQL Copilot (#02) &#8211; Qual query o Copilot usa para validar a performance do BD?"},"content":{"rendered":"<p>Fala Pessoal,<\/p>\n<p>Seguindo nos testes e posts sobre Copilot para SQL no Azure, hoje vou mostrar a query que ele usa para realizar a valida\u00e7\u00e3o de performance do ambiente.<\/p>\n<p>Ao perguntar ao Copilot como voc\u00ea pode melhorar a performance do ambiente, assim como no post anterior, ele responde como est\u00e1 o consumo de CPU no BD e se tenho algum lock no banco. N\u00e3o entrei nesse detalhe no post anterior, mas ele tamb\u00e9m mostra a query que ele usou para fazer essa valida\u00e7\u00e3o.<\/p>\n<p><strong>Pergunta feita ao Copilot:<\/strong> How can I improve the performance of my SQL database?<\/p>\n<p>Hoje trouxe um print do que vemos quando perguntamos:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-19562 aligncenter\" src=\"https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/mensagem-validacao-copilot-300x172.png\" alt=\"\" width=\"422\" height=\"242\" srcset=\"https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/mensagem-validacao-copilot-300x172.png 300w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/mensagem-validacao-copilot-410x235.png 410w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/mensagem-validacao-copilot-100x57.png 100w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/mensagem-validacao-copilot-275x158.png 275w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/mensagem-validacao-copilot-20x11.png 20w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/mensagem-validacao-copilot.png 577w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><\/p>\n<p>Ele avisa que est\u00e1 conectando no meu server chamado de <strong>copilotsqlbrazil<\/strong> e na minha base <strong>CopilotSQL.<\/strong><\/p>\n<p>Se n\u00e3o aparecer essa mensagem enquanto ele processa sua resposta \u00e9 bem prov\u00e1vel que ele nem chegou a se conectar na sua base para fazer valida\u00e7\u00f5es.<\/p>\n<p><strong>Resposta do Copilot:<\/strong><\/p>\n<p>Based on the CPU usage data from the past hour, it appears that your SQL database is not experiencing a high CPU usage scenario. The average CPU percent was 3.53%, and the maximum CPU percent reached was 95.41%. However, the CPU usage was over 80% for only 1 minute in the past hour. As per the guidelines, a &#8216;high CPU&#8217; scenario is defined as more than 3 minutes of CPU usage over 80% in the past hour. Therefore, your situation is classified as a &#8216;low\/no CPU&#8217; scenario.<\/p>\n<p>However, the query results indicate that there are blocking sessions in your database. Blocking occurs when one session (the blocker) holds a lock on a specific resource, and a second session attempts to acquire a conflicting lock type on the same resource. This can impact the performance of your SQL database.<\/p>\n<p>Here is a summary of the blocking sessions:<\/p>\n<ul>\n<li>Session 90 is blocked by session 92. The blocker query is &#8220;BEGIN tran\\r\\nUPDATE [TesteCopilot2]\\r\\nSET Cod = 44&#8221;. This query is attempting to update the &#8216;Cod&#8217; column in the &#8216;TesteCopilot2&#8217; table.<\/li>\n<\/ul>\n<p>To resolve these blocking issues, you can use the T-SQL command &#8216;KILL&#8217; to terminate the lead blocker sessions for immediate relief. However, this should be done with caution as it can impact running transactions.<\/p>\n<p>To avoid blocking sessions in the future, consider the following best practices:<\/p>\n<ol>\n<li>Keep transactions short.<\/li>\n<li>Ensure transactions isolation levels are appropriate (avoid SERIALIZABLE and REPEATABLE READ if possible).<\/li>\n<li>Use proper indexes and tune queries. Ensure long running queries and transactions are optimized.<\/li>\n<li>Look out for implicit transactions, which are common in Java applications.<\/li>\n<li>Continue to use read commit snapshot isolation (RCSI), which is default for Azure SQL Database.<\/li>\n<\/ol>\n<p>For more information on understanding and resolving blocking, you can refer to the official Microsoft documentation on Azure SQL Database.<\/p>\n<p>Remember, the &#8216;wait_type&#8217; column in the query results can help you understand different lock types. For more information on this, you can refer to the official Microsoft documentation on sys.dm_os_wait_stats.<\/p>\n<p>By addressing these blocking sessions and following the best practices, you can improve the performance of your SQL database.<\/p>\n<p>Click <strong>here<\/strong> to view the query or queries we used for your answer in Azure Portal&#8217;s SQL editor.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ao clicar nesse link que ele responde, chegamos na query que ele usa para realizar essa valida\u00e7\u00e3o:<\/strong><\/p>\n<pre class=\"lang:default decode:true \">WITH cteHead AS (\r\nSELECT\r\nsess.session_id,\r\nreq.request_id,\r\nLEFT(ISNULL(req.wait_type, ''), 50) AS 'wait_type', LEFT(ISNULL(req.wait_resource, ''), 40) AS 'wait_resource',\r\nLEFT(req.last_wait_type, 50) AS 'last_wait_type',\r\nreq.wait_time,\r\nreq.blocking_session_id,\r\nLEFT(req.[status], 15) AS 'request_status',\r\nsess.open_transaction_count AS 'open_tran_count',\r\nreq.command,\r\nreq.plan_handle,\r\nreq.[sql_handle],\r\nconn.most_recent_sql_handle,\r\nLEFT(sess.[status], 15) AS 'session_status',\r\nreq.query_hash,\r\nreq.query_plan_hash\r\nFROM sys.dm_exec_sessions AS sess\r\nLEFT OUTER JOIN sys.dm_exec_requests AS req\r\nON sess.session_id = req.session_id\r\nLEFT OUTER JOIN sys.dm_exec_connections AS conn\r\nON conn.session_id = sess.session_id\r\n),\r\ncteBlockingHierarchy AS (\r\nSELECT\r\nhead.session_id AS head_blocker_session_id,\r\nhead.session_id AS session_id,\r\nhead.blocking_session_id,\r\nhead.request_status,\r\nhead.open_tran_count,\r\nhead.wait_type,\r\nhead.wait_time,\r\nhead.wait_resource,\r\nhead.[sql_handle],\r\nhead.most_recent_sql_handle,\r\n0 AS [level]\r\nFROM cteHead AS head\r\nWHERE (head.blocking_session_id IS NULL OR head.blocking_session_id = 0)\r\nAND head.session_id IN (\r\nSELECT DISTINCT blocking_session_id\r\nFROM cteHead\r\nWHERE blocking_session_id != 0\r\n)\r\nUNION ALL\r\nSELECT\r\nh.head_blocker_session_id,\r\nblocked.session_id,\r\nblocked.blocking_session_id,\r\nblocked.request_status,\r\nblocked.open_tran_count,\r\nblocked.wait_type,\r\nblocked.wait_time,\r\nblocked.wait_resource,\r\nh.[sql_handle],\r\nh.most_recent_sql_handle,\r\n[level] + 1\r\nFROM cteHead AS blocked\r\nINNER JOIN cteBlockingHierarchy AS h\r\nON h.session_id = blocked.blocking_session_id\r\nAND h.session_id != blocked.session_id\r\nWHERE (h.wait_type COLLATE Latin1_General_BIN NOT IN ('EXCHANGE','CXPACKET')\r\nOR h.wait_type IS NULL)\r\n)\r\nSELECT\r\nbh.session_id AS blocked_session_id,\r\nbh.blocking_session_id AS blocker_session_id,\r\nbh.request_status,\r\nbh.wait_type,\r\nbh.wait_resource,\r\ntxt.[text] AS blocker_query\r\nFROM cteBlockingHierarchy AS bh\r\nOUTER APPLY sys.dm_exec_sql_text(\r\nISNULL([sql_handle], most_recent_sql_handle)\r\n) AS txt;\r\n\r\nSELECT\r\nAVG(avg_cpu_percent) AS 'Average CPU Percent',\r\nMAX(avg_cpu_percent) AS 'Maximum CPU Percent',\r\nAVG(avg_instance_cpu_percent) AS 'Average Instance CPU Percent',\r\nMAX(avg_instance_cpu_percent) AS 'Maximum Instance CPU Percent',\r\nSUM(CASE\r\nWHEN avg_cpu_percent &gt;= 80 THEN 1\r\nELSE 0\r\nEND) \/ 4. AS 'Minutes with CPU Percent over 80%'\r\nFROM sys.dm_db_resource_stats\r\nWHERE end_time &gt;= DATEADD(MINUTE, -60, GETDATE());<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Resultado da query executada na minha base:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-19563 aligncenter\" src=\"https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/Resultado-query-Copilot-300x56.jpg\" alt=\"\" width=\"605\" height=\"113\" srcset=\"https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/Resultado-query-Copilot-300x56.jpg 300w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/Resultado-query-Copilot-768x144.jpg 768w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/Resultado-query-Copilot-700x131.jpg 700w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/Resultado-query-Copilot-410x77.jpg 410w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/Resultado-query-Copilot-100x19.jpg 100w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/Resultado-query-Copilot-275x52.jpg 275w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/Resultado-query-Copilot-20x4.jpg 20w, https:\/\/fabriciolima.net\/blog\/wp-content\/uploads\/2024\/05\/Resultado-query-Copilot.jpg 964w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Por que eu achei isso legal e resolvi postar aqui para voc\u00eas?<\/strong><\/p>\n<p>Primeiro para terem mais detalhes de como funciona o Copilot e segundo para que quem n\u00e3o tenha o Copilot ainda possa executar a mesma query que ele usa e fazer a mesma an\u00e1lise que ele faria.<\/p>\n<p>Isso ajuda voc\u00ea a\u00ed hoje mesmo antes de receber o Copilot.<\/p>\n<p>Curtiram?<\/p>\n<p>Estou debatendo por email com o time do Copilot sobre a an\u00e1lise de uma query que o Copilot deveria apontar uma melhoria. Assim que ficar redondo essa an\u00e1lise, vai virar um post aqui para voc\u00eas tamb\u00e9m.<\/p>\n<p>At\u00e9 o pr\u00f3ximo artigo.<\/p>\n<p>Post Anterior:<\/p>\n<p><a href=\"https:\/\/www.fabriciolima.net\/blog\/2024\/05\/22\/azure-sql-copilot-01-identificando-locks-no-bd-perguntando-ao-copilot\/\" target=\"_blank\" rel=\"noopener\">Azure SQL Copilot (#01) \u2013 Identificando Locks no BD apenas perguntando ao Copilot<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Abra\u00e7os,<\/p>\n<p>Fabr\u00edcio Lima<\/p>\n<p>CEO na Fabr\u00edcio Lima Solu\u00e7\u00f5es em BD<\/p>\n<p>Microsoft Data Platform MVP<\/p>\n<p>Instagram:<a class=\"external-link wpel-icon-left\" href=\"https:\/\/www.instagram.com\/fabriciofrancalima\/\" target=\"_blank\" rel=\"noopener external noreferrer nofollow\" data-wpel-link=\"external\">@fabriciofrancalima<\/a><\/p>\n<p>Linkedin:\u00a0<a class=\"external-link wpel-icon-left\" href=\"https:\/\/www.linkedin.com\/in\/fabriciofrancalima\/\" target=\"_blank\" rel=\"noopener noreferrer external nofollow\" data-wpel-link=\"external\">https:\/\/www.linkedin.com\/in\/fabriciofrancalima\/<\/a><\/p>\n<p>Consultoria:\u00a0comercial@powertuning.com.br<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fala Pessoal, Seguindo nos testes e posts sobre Copilot para SQL no Azure, hoje vou mostrar a query que ele usa para realizar a valida\u00e7\u00e3o de performance do ambiente. Ao perguntar ao Copilot como voc\u00ea pode melhorar a performance do ambiente, assim como no post anterior, ele responde como est\u00e1 o consumo de CPU no [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[1961,1960,1962],"tags":[1968,1990,1988,1986,1983,1984,1981,1639,1967,1971,1982,1989,1993,1987,1972,1976,1974,1970,1994,1978,1969,1973,1991,1992,1985,1979,1977,33,1980,1975],"class_list":["post-19561","post","type-post","status-publish","format-standard","hentry","category-copilot","category-ia","category-inteligencia-artificial","tag-ai-in-databases","tag-ai-performance-tuning","tag-ai-tools-for-sql","tag-ai-driven-insights","tag-analise-de-desempenho","tag-automacao-de-banco-de-dados","tag-azure-database","tag-azure-sql","tag-azure-sql-copilot","tag-banco-de-dados-inteligente","tag-banco-de-dados-na-nuvem","tag-cloud-database-ai","tag-data-analysis-ai","tag-data-management-ai","tag-desempenho-de-banco-de-dados","tag-ferramentas-de-banco-de-dados","tag-gestao-de-bloqueios","tag-identificacao-de-locks","tag-intelligent-database-management","tag-locks-sql","tag-machine-learning","tag-monitoramento-de-transacoes","tag-smart-databases","tag-sql-administration","tag-sql-ai-integration","tag-sql-optimization","tag-sql-performance","tag-sql-server","tag-sql-troubleshooting","tag-treinamento-sql"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/posts\/19561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/comments?post=19561"}],"version-history":[{"count":7,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/posts\/19561\/revisions"}],"predecessor-version":[{"id":19570,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/posts\/19561\/revisions\/19570"}],"wp:attachment":[{"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/media?parent=19561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/categories?post=19561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fabriciolima.net\/blog\/wp-json\/wp\/v2\/tags?post=19561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}