<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Le blog de SQLpro &#187; SQL Server 2005</title>
	<atom:link href="https://blog.developpez.com/sqlpro/pcategory/ms-sql-server/sql-server-2005/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/sqlpro</link>
	<description>Le SQL pour SQL Server, PostGreSQL et tous les autres SGBDR</description>
	<lastBuildDate>Thu, 15 Oct 2020 12:59:17 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Une peste nommée NOLOCK&#8230; et comment l&#8217;éviter</title>
		<link>https://blog.developpez.com/sqlpro/p13181/langage-sql-norme/une-peste-nommee-nolock-et-comment-leviter</link>
		<comments>https://blog.developpez.com/sqlpro/p13181/langage-sql-norme/une-peste-nommee-nolock-et-comment-leviter#comments</comments>
		<pubDate>Sun, 08 Jul 2018 14:55:07 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[Langage SQL (norme)]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[ISOLATION]]></category>
		<category><![CDATA[NOLOCK]]></category>
		<category><![CDATA[SNAPSHOT]]></category>
		<category><![CDATA[transaction]]></category>
		<category><![CDATA[verrou]]></category>
		<category><![CDATA[verrouilage]]></category>
		<category><![CDATA[verrous]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=818</guid>
		<description><![CDATA[L&#8217;utilisation du tag de table NOLOCK, hélas largement prisé par de nombreux développeurs, est, la plupart du temps, une absurdité conduisant à des résultats potentiellement faux. Est-il possible de faire autrement pour minimiser les blocages induit par les verrouillages dans SQL Server ? Réponse OUI ! À la suite de cette remarque : &#171;&#160;&#8230;je passe [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>L&rsquo;utilisation du tag de table NOLOCK, hélas largement prisé par de nombreux développeurs, est, la plupart du temps, une absurdité conduisant à des résultats potentiellement faux. Est-il possible de faire autrement pour minimiser les blocages induit par les verrouillages dans SQL Server ? Réponse OUI !<br />
<span id="more-818"></span><br />
À la suite de cette remarque :<br />
<em>&laquo;&nbsp;&#8230;je passe aussi sur le NOLOCK qui est une véritable connerie en production et donne des valeurs fausses !&nbsp;&raquo;</em><br />
Publiée ici :<br />
<a href="https://www.developpez.net/forums/d1870299/bases-donnees/ms-sql-server/developpement/utilisation-excessive-structure-conditionnelle-choix-multiples/#post10343427" rel="noopener" target="_blank"></a><br />
Mon interlocuteur me répondais :<br />
<em>&laquo;&nbsp;Merci pour cette précision. Comme vous abordez le sujet du NOLOCK effectivement j&rsquo;ai constaté certaines fois en production on avait des valeurs totalement erronées, mais je n&rsquo;arrivais pas à me l&rsquo;expliquer. J&rsquo;ai maintenant une piste et je vais creuser du côté du NOLOCK que nous utilisons beaucoup en production sur une base de données CRM 2013. Aussi par quoi pouvons nous remplacer le NOLOCK ??&nbsp;&raquo;</em></p>
<p>Je lui donnais donc l&rsquo;explication suivante&#8230;<br />
<strong><br />
Le &laquo;&nbsp;NOLOCK&nbsp;&raquo; peut parfaitement se remplacer par le niveau d&rsquo;isolation SNAPSHOT</strong> par exemple&#8230;</p>
<p>NOLOCK ne signifie pas &laquo;&nbsp;<em>je ne pose pas de verrou</em>&laquo;&nbsp;, mais plus précisément : &laquo;&nbsp;<em>j&rsquo;ignore qu&rsquo;il y a des verrous et je lit ce que je peut</em>&nbsp;&raquo; (ceci est plus connu dans la littérature professionnelle sous le nom de <em><strong>lecture dans le chaos</strong></em>&#8230;). Vous pouvez donc lire plusieurs fois les mêmes données ou encore ne pas lire certaines.</p>
<p><strong>Quid du mode SNAPSHOT ?</strong></p>
<p>SNAPSHOT fait du verrouillage optimiste pour les lectures. Par défaut SQL Server fait du verrouillage pessimiste, c&rsquo;est à dire que les lectures posent des verrous qui permettent d&rsquo;autres lectures concurrentes (verrous partagés, mode &laquo;&nbsp;SHARE&nbsp;&raquo;), mais empêchent les écritures des autres utilisateurs. Conceptuellement, le verrouillage optimiste (donc via le mode d’isolation SNAPSHOT) fait une copie (si besoin) des données avant de permettre leurs lectures. Comme vous travaillez sur une copie, les utilisateurs concurrent peuvent modifier les vraies ligne sans que cela vous concerne. Bien entendu il peut toujours y avoir des conflits, mais cela réduit considérablement le nombre de verrous bloquants tout en gardant l&rsquo;intégrité et la consistante de la base, ce qui n&rsquo;est pas du tout le cas du NOLOCK.</p>
<p><strong>Alors, à quoi sert le NOLOCK ? </strong></p>
<p>Cela peut servir pour certains cas ou l&rsquo;on désire des résultats dont on se fout de l&rsquo;intégrité ou de la précision. Souvent dans le domaine de la statistique.<br />
par exemple si vous voulez savoir combien les français ont d&rsquo;enfant par foyer et scrutez donc, pour ce faire, une table de 60 millions de lignes (population française), je ne suis pas sûr que dans un camembert représentant la chose, on fasse la distinction des quelques pixels de l&rsquo;imprécision de la 6e décimale du résultat liées à quelques mauvaises lectures de lignes !<br />
On utilise d&rsquo;ailleurs très souvent le NOLOCK pour des tâches d&rsquo;administration systèmes dans SQL Server afin d&rsquo;éviter tout blocage au risque de perdre quelques lignes, ce qui n&rsquo;a pas toujours une importance capitale lorsque l&rsquo;on veut remonter quelques informations concernant les 20% de requêtes les moins performantes !</p>
<p><strong>Mais alors, le SNAPSHOT fait la même chose en mieux ? </strong></p>
<p>Oui et non&#8230; Certes il permet de garantir l&rsquo;intégrité, la consistance, etc&#8230; mais son mécanisme est plus lourd (un peu moins rapide) et consomme des ressources pour générer les copies (SNAPSHOT). Rassurez-vous il est assez malin pour ne faire ces copies qui s&rsquo;il a besoin de les faire&#8230; En d&rsquo;autres termes si aucun utilisateur concurrent veut faire des modif sur le jeu de lignes que vous manipulez en mode SNAPSHOT, aucune copie ne sera générée. Mais si, lorsque vous avez démarré une lecture en mode SNAPSHOT, un utilisateur concurrent veut modifier des lignes, et bien la copie sera réalisée au cours de votre lecture sans que vous vous en aperceviez&#8230; En quelque sorte le système fonctionne à l&rsquo;envers&#8230; Bien évidemment il faut un espace de stockage pour ces copies et c&rsquo;est la base tempdb qui s&rsquo;y colle. Il faut donc, la dimensionner correctement&#8230;<br />
Tiens, à nouveau un travail de DBA !</p>
<p><strong>Des conflits ! Quels conflits ?</strong></p>
<p>J&rsquo;ai précédemment parlé de conflits possible avec l&rsquo;utilisation du mode SNAPSHOT. En effet, si vous voulez modifier des données lors d&rsquo;une transaction en mode d’isolation SNAPSHOT, c&rsquo;est possible, mais ces modifications seront faites sur la copie, et non sur la base originale, et seront reportées sur la base originale au moment du COMMIT. Deux hypothèses se font alors jour :<br />
■ soit aucune ligne visée par vos modifications faites en mode SNAPSHOT n&rsquo;a été modifié dans une autre session directement sur la base, alors la mise à jour est appliquée.<br />
Ou alors :<br />
■ s&rsquo;il existe au moins une ligne qui a été modifiée par une autre session entre temps sur la base originale, alors vos modifications ne pourront être appliquées aux données originales et vont êtres abandonnées.<br />
Dans ce dernier cas vous obtiendrez une message d&rsquo;erreur (3960) assez abscons, qui indique :<br />
<em>&laquo;&nbsp;La transaction d&rsquo;isolement d&rsquo;instantané a été abandonnée en raison d&rsquo;un conflit de mise à jour. &#8230;&nbsp;&raquo;</em></p>
<p>Notez qu&rsquo;il existe un autre tag plus intéressant que le NOLOCK&#8230;. Le <strong>READPAST</strong> !&#8230;  Qui fait presque la même chose, mais ignore tout ce qui est verrouillé !</p>
<p><strong>Les modes d&rsquo;isolation selon la norme SQL</strong></p>
<p>Sachez que le mode d’isolation SNAPSHOT ne fait pas partie de la norme SQL qui n&rsquo;autorise que les modes d&rsquo;isolation suivants :<br />
0 &#8211; READ UNCOMMITTED : lecture des données invalides (non &laquo;&nbsp;committées&nbsp;&raquo;)<br />
1 &#8211; READ COMMITTED : lecture de données valides (ce qui n&rsquo;empêche pas certaines anomalies transactionnelles comme la lecture non répétable, ou l&rsquo;apparition de lignes fantômes)<br />
2 &#8211; REPEATABLE READ : lecture répétable de données (ce qui n&rsquo;empêche pas certaines anomalies transactionnelles comme l&rsquo;apparition de lignes fantômes)<br />
3 &#8211; SERIALIZABLE : lecture en mode série (aucun accès concurrent aux tables lors des mises à jour, ce qui empêche toute anomalies)<br />
Évidemment plus on monte le niveau d’isolation, plus le verrouillage est important et moins les accès concurrents peuvent travailler.<br />
La norme SQL considère que les manipulation des données doivent porter sur des valeurs &laquo;&nbsp;vivantes&nbsp;&raquo;. Hors, en utilisant le mode SNAPSHOT et son versionnement des lignes, ont est appelé à lire potentiellement des valeurs antérieurs des données, ce n&rsquo;est donc pas une lecture de l&rsquo;état actuel des valeurs dans la base&#8230;</p>
<p>Pour informations, des SGBRD comme Oracle, ou PostGreSQL fonctionnent nativement en mode d&rsquo;isolation SNAPSHOT, autrement dit utilisent systématiquement le verrouillage optimiste et ne savent pas faire autres choses (verrouillage pessimiste en particulier) voir ne savent même pas utiliser certains autres niveau d&rsquo;isolation. Par exemple PostGreSQL ne permet pas de faires des transaction en mode READ UNCOMMITTED et Oracle ne permettent pas d&rsquo;utiliser le niveau d&rsquo;isolation REPEATABLE READ&#8230;</p>
<p><strong>Activer le mode s&rsquo;isolation SNAPSHOT</strong></p>
<p>Pour permettre ce mode d&rsquo;isolation, vous devez d&rsquo;abord autoriser la base à utiliser le mode SNAPSHOT. Ce paramétrage est à deux niveaux :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER DATABASE { CURRENT | nom_base } SET ALLOW_SNAPSHOT_ISOLATION ON;</div></div>
<p>permet d&rsquo;utiliser à tout moment le mode d’isolation SNAPSHOT qui doit être introduit préalablement par la commande SET TRANSACTION ISOLATION SNAPSHOT ou par un tag de table (SNAPSHOT)<br />
&#8230; et :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER DATABASE { CURRENT | nom_base } SET READ_COMMITTED_SNAPSHOT ON;</div></div>
<p>qui place d&rsquo;office toutes les lectures en mode SNAPSHOT (comme le fait Oracle ou PostGreSQL);<br />
<strong>ATTENTION</strong> : pour assurer le versionnement des lignes, SQL Server rajoute à toutes les lignes de toutes les tables une information de version qui est codé sur 14 octets. Ceci peut prendre du temps à mettre en œuvre, en particulier sur de grosses bases.<br />
<strong></p>
<p>Appendice :</strong></p>
<p>Pour une démonstration des effets du NOLOCK : <a href="http://mssqlserver.fr/les-dangers-du-nolock/" rel="noopener" target="_blank">http://mssqlserver.fr/les-dangers-du-nolock/</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Métrique de volumétrie du stockage par table, index et partition</title>
		<link>https://blog.developpez.com/sqlpro/p13171/ms-sql-server/metrique-de-volumetrie-du-stockage-par-table-index-et-partition</link>
		<comments>https://blog.developpez.com/sqlpro/p13171/ms-sql-server/metrique-de-volumetrie-du-stockage-par-table-index-et-partition#comments</comments>
		<pubDate>Wed, 07 Feb 2018 09:19:04 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[métrique]]></category>
		<category><![CDATA[partition]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[stockage]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=789</guid>
		<description><![CDATA[Voici une petite requête complémentaire pour mesurer les volumes stockées par partition, index et tables d&#8217;une base : SELECT s.name AS TABLE_SCHEMA, o.name AS TABLE_NAME, i.name AS INDEX_NAME, &#160; &#160; &#160; &#160;fs.name AS FILEGROUP_NAME, f.name AS FILE_NAME, f.physical_name, p.partition_number, &#160; &#160; &#160; &#160;p.ROWS, p.data_compression_desc, au.total_pages * 8 AS SIZE_TOTAL_KO, &#160; &#160; &#160; &#160;SUM&#40;au.total_pages * 8&#41; [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Voici une petite requête complémentaire pour mesurer les volumes stockées par partition, index et tables d&rsquo;une base :<br />
<span id="more-789"></span></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> s<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> TABLE_SCHEMA<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> INDEX_NAME<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;fs<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> FILEGROUP_NAME<span style="color: #66cc66;">,</span> f<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> FILE_NAME<span style="color: #66cc66;">,</span> f<span style="color: #66cc66;">.</span>physical_name<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>partition_number<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;p<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">ROWS</span><span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>data_compression_desc<span style="color: #66cc66;">,</span> au<span style="color: #66cc66;">.</span>total_pages <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">8</span> <span style="color: #993333; font-weight: bold;">AS</span> SIZE_TOTAL_KO<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>au<span style="color: #66cc66;">.</span>total_pages <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span>PARTITION <span style="color: #993333; font-weight: bold;">BY</span> i<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> SIZE_TOTAL_INDEX_KO<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>au<span style="color: #66cc66;">.</span>total_pages <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span>PARTITION <span style="color: #993333; font-weight: bold;">BY</span> s<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> SIZE_TOTAL_TABLE_KO<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>partitions <span style="color: #993333; font-weight: bold;">AS</span> p<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>allocation_units <span style="color: #993333; font-weight: bold;">AS</span> au<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> p<span style="color: #66cc66;">.</span>hobt_id <span style="color: #66cc66;">=</span> au<span style="color: #66cc66;">.</span>container_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>filegroups <span style="color: #993333; font-weight: bold;">AS</span> fs<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> au<span style="color: #66cc66;">.</span>data_space_id <span style="color: #66cc66;">=</span> fs<span style="color: #66cc66;">.</span>data_space_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>database_files <span style="color: #993333; font-weight: bold;">AS</span> f<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> fs<span style="color: #66cc66;">.</span>data_space_id <span style="color: #66cc66;">=</span> f<span style="color: #66cc66;">.</span>file_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> p<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>indexes <span style="color: #993333; font-weight: bold;">AS</span> i<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> p<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> p<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>index_id;</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vérification de la dernière sauvegarde d&#8217;une base</title>
		<link>https://blog.developpez.com/sqlpro/p13170/ms-sql-server/verification-de-la-derniere-sauvegarde-dune-base</link>
		<comments>https://blog.developpez.com/sqlpro/p13170/ms-sql-server/verification-de-la-derniere-sauvegarde-dune-base#comments</comments>
		<pubDate>Thu, 01 Feb 2018 10:51:55 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[verification]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=787</guid>
		<description><![CDATA[Parmi les opérations importantes à systématiser, la vérification de consistance d&#8217;une sauvegarde est une opération à ne pas négliger. Mais compte tendu qu&#8217;une sauvegarde peut être multi-famille (donc répartie sur plusieurs fichiers), multi-support (donc redondées dans plusieurs destinations) ou intégré dans un &#171;&#160;device&#160;&#187; (donc noyée au milieu de plusieurs sauvegarde dans un fichier), la complexité [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Parmi les opérations importantes à systématiser, la vérification de consistance d&rsquo;une sauvegarde est une opération à ne pas négliger. Mais compte tendu qu&rsquo;une sauvegarde peut être multi-famille (donc répartie sur plusieurs fichiers), multi-support (donc redondées dans plusieurs destinations) ou intégré dans un &laquo;&nbsp;device&nbsp;&raquo; (donc noyée au milieu de plusieurs sauvegarde dans un fichier), la complexité augmente d&rsquo;autant. Voici donc une petite procédure pour ce faire&#8230;<br />
<span id="more-787"></span></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> P_VERIFY_LAST_BACKUP @DB sysname<br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">WITH</span> <br />
T0 <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> TOP <span style="color: #cc66cc;">1</span> media_set_id<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">POSITION</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; msdb<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>backupset<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;database_name <span style="color: #66cc66;">=</span> @DB<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> backup_start_date <span style="color: #993333; font-weight: bold;">DESC</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'RESTORE VERIFYONLY FROM DISK = '</span><span style="color: #ff0000;">''</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> physical_device_name <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">''</span><span style="color: #ff0000;">' WITH FILE = '</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">POSITION</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">';'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; msdb<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>backupmediafamily<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> mf<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> T0 <span style="color: #993333; font-weight: bold;">ON</span> mf<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>media_set_id<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> T0<span style="color: #66cc66;">.</span>media_set_id;<br />
<br />
PRINT @<span style="color: #993333; font-weight: bold;">SQL</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>Exemple d&rsquo;utilisation :</p>
<p>EXEC P_VERIFY_LAST_BACKUP &lsquo;master';</p>
<p><a href="http://sqlpro.developpez.com/_fichierSQL/SET_STATISTIS_IO_AGGREGATE.txt" title="Fichier du code" target="_blank">Le code !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Agrégation des statistiques d&#8217;IO d&#8217;une requête</title>
		<link>https://blog.developpez.com/sqlpro/p13157/ms-sql-server/agregation-des-statistiques-dio-dune-requete</link>
		<comments>https://blog.developpez.com/sqlpro/p13157/ms-sql-server/agregation-des-statistiques-dio-dune-requete#comments</comments>
		<pubDate>Thu, 28 Dec 2017 18:55:58 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2000]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[ES]]></category>
		<category><![CDATA[IO]]></category>
		<category><![CDATA[performance requête]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[STATISTICS IO]]></category>
		<category><![CDATA[statistiques entrées sorties]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=778</guid>
		<description><![CDATA[Si le paramétrage SET STATISTICS IO ON de Microsoft SQL Server permet de savoir, table par table les statistiques d&#8217;entrées/sorties (IO pour Input/Output) des requêtes, il n&#8217;est pas toujours facile de s&#8217;y repérer lorsque la requête est complexe et fait appel à de multiples tables. Voici une procédure qui en effectue la synthèse. Rappelons que [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Si le paramétrage <strong>SET STATISTICS IO ON</strong> de Microsoft SQL Server permet de savoir, table par table les statistiques d&rsquo;entrées/sorties (IO pour Input/Output) des requêtes, il n&rsquo;est pas toujours facile de s&rsquo;y repérer lorsque la requête est complexe et fait appel à de multiples tables. Voici une procédure qui en effectue la synthèse.<br />
<span id="more-778"></span><br />
Rappelons que les statistiques IO sont le nombre de pages lues par le moteur SQL pour exécuter une requête. Le paramètre de session STATISTICS IO peut être mis à ON ou OFF. Par défaut il est à OFF. Mis sur ON, il permet, dans SQL Server Management Studio (SSMS), de voir le nombre de pages accédées pour une requête ou un lot de requête. C&rsquo;est une série de message d&rsquo;une ligne commençant par &laquo;&nbsp;Table&#8230;&nbsp;&raquo; qui suit immédiatement le message indiquant le nombre de lignes affectées par la requête.<br />
<div id="attachment_780" style="width: 530px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO.jpg" alt="SQL Server onglet message STATISTICS IO" width="520" height="310" class="size-full wp-image-780" /></a><p class="wp-caption-text">SQL Server onglet message STATISTICS IO</p></div> </p>
<p>Néanmoins il est assez difficile de lire tout cela et d&rsquo;en déduire une tendance globale.<br />
Ne serait-ce pas plus intéressante de les présenter de cette manière :<br />
<div id="attachment_781" style="width: 989px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO_GLOBAL.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO_GLOBAL.jpg" alt="SQL Server STATISTICS IO aggregate" width="979" height="329" class="size-full wp-image-781" /></a><p class="wp-caption-text">Statistiques IO SQL Server avec totaux et agrégation</p></div></p>
<p>La petite procédure que voici permet justement d&rsquo;effectuer cette tâche. Il suffit de lui passer le texte complet des message d&rsquo;IO et elle vous retournera tout cela, formaté, totalisé et agrégé !</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">USE</span> msdb;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> SCHEMA S_DBA;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> S_DBA<span style="color: #66cc66;">.</span>P_SPLIT_STATS_IO @<span style="color: #993333; font-weight: bold;">DATA</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @LINE NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span>;<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @L <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#40;</span>LINE NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">800</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @T <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sysname<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SCAN_COUNT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGICAL_READS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PHYSICAL_READS &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; READ_AHEAD_READS &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOB_LOGICAL_READS &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOB_PHYSICAL_READS &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOB_READ_AHEAD_READS &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
WHILE CHARINDEX<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">,</span> CHARINDEX<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#41;</span> &nbsp;<span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @L <span style="color: #993333; font-weight: bold;">SELECT</span> @LINE;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @<span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">,</span> LEN<span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> CHARINDEX<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @L <span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">DATA</span>;<br />
<span style="color: #993333; font-weight: bold;">DELETE</span> @L <span style="color: #993333; font-weight: bold;">WHERE</span> LINE <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">OR</span> LINE <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">LIKE</span> N<span style="color: #ff0000;">'%Table %'</span> <span style="color: #993333; font-weight: bold;">COLLATE</span> French_BIN;<br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @L <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;LINE <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%Nombre d'</span><span style="color: #ff0000;">'analyses%'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">--&gt; français</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Table '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' lectures logiques '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' lectures physiques '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' lectures anticipées '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'de données d'</span><span style="color: #ff0000;">'objets volumineux '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @L <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;LINE <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%Scan count%'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">--&gt; anglais</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Table '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'. Scan count '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' logical reads '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' physical reads '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' read-ahead reads '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'lob'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'SELECT '</span> <span style="color: #66cc66;">+</span> LINE <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">' UNION ALL '</span> <span style="color: #993333; font-weight: bold;">FROM</span> @L;<br />
<span style="color: #993333; font-weight: bold;">SET</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">,</span> LEN<span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @T<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">WITH</span> <br />
T <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @T<br />
<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'°°° TOTAL °°°'</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>SCAN_COUNT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOGICAL_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>PHYSICAL_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>READ_AHEAD_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOB_LOGICAL_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOB_PHYSICAL_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOB_READ_AHEAD_READS<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @T<br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> T<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'°°° TOTAL °°°'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LOGICAL_READS <span style="color: #993333; font-weight: bold;">DESC</span>; &nbsp;<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>SCAN_COUNT<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> TOTAL_SCANS<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOGICAL_READS <span style="color: #66cc66;">+</span> LOB_LOGICAL_READS<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> TOTAL_LOGICAL_READS<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>PHYSICAL_READS <span style="color: #66cc66;">+</span> READ_AHEAD_READS <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LOB_PHYSICAL_READS <span style="color: #66cc66;">+</span> LOB_READ_AHEAD_READS<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> TOTAL_PHYSICAL_READS<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @T;<br />
<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>Exemple d&rsquo;utilisation :</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">EXECUTE</span> msdb<span style="color: #66cc66;">.</span>S_DBA<span style="color: #66cc66;">.</span>P_SPLIT_STATS_IO <span style="color: #ff0000;">'Table '</span><span style="color: #ff0000;">'PARAM_EAC'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 13, lectures logiques 26, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'Worktable'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 0, lectures logiques 0, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'VAL_PARAM_EAC'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 1, lectures logiques 59, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'ESP'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 31, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'TL_EXP_COOP'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 178, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'UC'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 5633, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'TL_PP_UC'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 1254, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'PG'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 85289, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'Worktable'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 0, lectures logiques 0, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'PG'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 1, lectures logiques 437, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'PP'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 7271, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'EXP'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 1996, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'VAR'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 866, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.'</span></div></div>
<p>Le résultat :<br />
<div id="attachment_782" style="width: 986px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO_GLOBAL-results.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO_GLOBAL-results.jpg" alt="Les statistiques d&#039;IO d&#039;une requête, sus forme tabulaire, totalisés et agrégés" width="976" height="337" class="size-full wp-image-782" /></a><p class="wp-caption-text">Les statistiques d&rsquo;IO d&rsquo;une requête, sous forme tabulaire, totalisés et agrégés</p></div></p>
<p><strong>QUELQUES EXPLICATIONS SUR LES MÉTRIQUES</strong></p>
<li><strong>analyses</strong> (scan count) : nombre de fois ou l&rsquo;exécuteur de requête a du pénétrer dans un index de la table ou dans la table (en cas de parallélisme, autant de fois que de threads&#8230;)</li>
<li><strong>lectures logiques</strong> (logical reads) : nombre de pages lues en RAM</li>
<li><strong>lectures physiques</strong> (physical reads) : nombre de pages lues depuis le disque (avec transfert en RAM) en mode aléatoire.</li>
<li><strong>lectures anticipées</strong> (read-ahead physical read) : nombre de pages lues depuis le disque (avec transfert en RAM) en mode continu.</li>
<li>&#8230;<strong>d&rsquo;objets volumineux</strong> (lob &#8230;) : nombre de pages &#8230; pour les &laquo;&nbsp;Large OBjects&nbsp;&raquo;.
</li>
<p><strong>NOTA </strong>: cet outil fonctionne que votre serveur soit en anglais ou français.</p>
<p><strong>LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE</strong><br />
<a href="http://sqlpro.developpez.com/_fichierSQL/SET_STATISTIS_IO_AGGREGATE.txt" title="Fichier du code" target="_blank">Le code !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Une fonction de comptage d&#8217;occurrence</title>
		<link>https://blog.developpez.com/sqlpro/p13156/ms-sql-server/une-fonction-de-comptage-doccurrence</link>
		<comments>https://blog.developpez.com/sqlpro/p13156/ms-sql-server/une-fonction-de-comptage-doccurrence#comments</comments>
		<pubDate>Thu, 16 Nov 2017 17:51:42 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2000]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[caractères]]></category>
		<category><![CDATA[comptage]]></category>
		<category><![CDATA[fonction]]></category>
		<category><![CDATA[occurrence]]></category>
		<category><![CDATA[SQL server]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=776</guid>
		<description><![CDATA[La petite fonction qui suit permet de compter le nombre de fois ou un caractère est présent dans une chaine de caractères. CREATE FUNCTION F_COUNT_CHAR&#40;@STR NVARCHAR&#40;MAX&#41;, @CHR NCHAR&#40;1&#41;&#41; RETURNS INT WITH RETURNS NULL ON NULL INPUT AS BEGIN DECLARE @NBR INT, @POS INT; SELECT @NBR = 0, @POS = CHARINDEX&#40;@CHR, @STR&#41;; WHILE @POS &#62; 0 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>La petite fonction qui suit permet de compter le nombre de fois ou un caractère est présent dans une chaine de caractères.</p>
<p><span id="more-776"></span></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span>@STR NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @CHR <span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">INT</span><br />
<span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">INPUT</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @NBR <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> @POS <span style="color: #993333; font-weight: bold;">INT</span>;<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @NBR <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> @POS <span style="color: #66cc66;">=</span> CHARINDEX<span style="color: #66cc66;">&#40;</span>@CHR<span style="color: #66cc66;">,</span> @STR<span style="color: #66cc66;">&#41;</span>;<br />
WHILE @POS <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @NBR <span style="color: #66cc66;">=</span> @NBR <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @POS <span style="color: #66cc66;">=</span> CHARINDEX<span style="color: #66cc66;">&#40;</span>@CHR<span style="color: #66cc66;">,</span> @STR<span style="color: #66cc66;">,</span> @POS <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #993333; font-weight: bold;">RETURN</span> @NBR;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>Exemples d&rsquo;utilisation :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- exemple &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- résultat</span><br />
<span style="color: #808080; font-style: italic;">------------------------------------------- ------------</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'abracadabra'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--&gt; 5</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">--&gt; 0</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">--&gt; 0</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">--&gt; 0</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">--&gt; NULL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">--&gt; NULL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">--&gt; NULL</span></div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Métadonnées de l&#8217;utilisation d&#8217;une colonne</title>
		<link>https://blog.developpez.com/sqlpro/p13155/ms-sql-server/metadonnees-de-lutilisation-dune-colonne</link>
		<comments>https://blog.developpez.com/sqlpro/p13155/ms-sql-server/metadonnees-de-lutilisation-dune-colonne#comments</comments>
		<pubDate>Mon, 23 Oct 2017 07:07:13 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[colonne]]></category>
		<category><![CDATA[contrainte]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[métadonnées]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=765</guid>
		<description><![CDATA[Certaines modifications de type de données d&#8217;une colonne nécessitent une suppression préalable des contraintes et index. Pour vous y aider, la procédure suivante indique dans quels objets (index ou contraintes) une colonne d&#8217;une table passée en argument est enrôlée. Dans le cas ou vous voudriez changer le type d&#8217;une telle colonne, il faudrait agir manuellement [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Certaines modifications de type de données d&rsquo;une colonne nécessitent une suppression préalable des contraintes et index. Pour vous y aider, la procédure suivante indique dans quels objets (index ou contraintes) une colonne d&rsquo;une table passée en argument est enrôlée.<br />
<span id="more-765"></span></p>
<p>Dans le cas ou vous voudriez changer le type d&rsquo;une telle colonne, il faudrait agir manuellement avec le scénario suivant :</p>
<p>1)	Scripter la création des contraintes par clic droit sur le nom de chacune des contraintes et les supprimer<br />
2)	Scripter la création des index par clic droit sur le nom de chacun des index et les supprimer<br />
3)	Modifier le type de données de la colonne avec la commande ALTER TABLE … ALTER COLUMN …<br />
4)	Recréer les index<br />
5)	Recréer les contraintes</p>
<p>Cette procédure à été créée pour vous y aider. Elle indique pour un couple d’information table/colonne dans quels index ou contrainte cette dernière est enrôlée&#8230;<br />
Cette procédure est créée en tant que procédure système afin d&rsquo;être exploitable dans n&rsquo;importe quelle base de l&rsquo;instance SQL Server.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">USE</span> master;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>all_objects<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'sp__COLUMN_IS_USED'</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;schema_id <span style="color: #66cc66;">=</span> SCHEMA_ID<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dbo'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ROP PROCEDURE dbo.sp__COLUMN_IS_USED;'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #808080; font-style: italic;">--===========================================================================--</span><br />
<span style="color: #808080; font-style: italic;">-- METADONNÉES D'UTILISATION D'UNE COLONNE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --</span><br />
<span style="color: #808080; font-style: italic;">--===========================================================================--</span><br />
<span style="color: #808080; font-style: italic;">-- Fredéric Brouard alias SQLpro &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://sqlpro.developpez.com --</span><br />
<span style="color: #808080; font-style: italic;">-- Société SQL SPOT - http://www.sqlspot.com &nbsp; &nbsp; &nbsp; &nbsp;2017-10-13 - version 1.0 --</span><br />
<span style="color: #808080; font-style: italic;">--===========================================================================--</span><br />
<br />
<br />
<span style="color: #808080; font-style: italic;">--▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄--</span><br />
<span style="color: #808080; font-style: italic;">-- PHASE 1 : création de la procédure &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--</span><br />
<span style="color: #808080; font-style: italic;">--▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀--</span><br />
<br />
<br />
<span style="color: #993333; font-weight: bold;">USE</span> master;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> dbo<span style="color: #66cc66;">.</span>sp__COLUMN_IS_USED @OBJ NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">261</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @COL NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #808080; font-style: italic;">/******************************************************************************<br />
* METADONNÉES D'UTILISATION D'UNE COLONNE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* Procédure listant sous forme de tables dans quels objets est utilisé une &nbsp; &nbsp;*<br />
* colonne d'une table &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
******************************************************************************* &nbsp; <br />
* Frédéric Brouard - SQLpro@SQLspot.com - Sté SQL SPOT http://www.sqlspot.com *<br />
* Plus d'info. sur http://sqlpro.developpez.com &nbsp; - &nbsp;2017-10-13 - version 1.0 * &nbsp; <br />
******************************************************************************* &nbsp; <br />
* Cette procédure prend en argument le nom d'une table avec son schéma SQL &nbsp; &nbsp;*<br />
* (sinon le schéma par défaut de l'utilisateur qui la lance) ainsi qu'un nom &nbsp;*<br />
* de colonne et renvoie un jeu de données contenant le descriptif des cas &nbsp; &nbsp; *<br />
* d'utilisation de cette colonne dans les index et les contraintes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
*******************************************************************************<br />
* ATTENTION &nbsp;: procédure système ! Exécutable depuis n'importe quelle base &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* Paramètre en entrée : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;@OBJ type NVARCHAR(261) : nom de table dont on veut connaître les index &nbsp;*<br />
* &nbsp; &nbsp;@COL nom de la colonne dont on veut connaître l'usage &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* COLONNE de la table en sortie : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;COLUMN_NAME &nbsp; nom de la colonne passée en paramètre &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;OBJECT_TYPE &nbsp; type d'objet (nature de la contrainte ou INDEX) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;OBJECT_NAME &nbsp; nom de la contrainte ou de l'index &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
<br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* EXEMPLE : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;EXEC dbo.sp__COLUMN_IS_USED 'CC', 'Idx' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;... donne la liste de tous les index et contrainte de la table CC &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; &nbsp;incluant la colonne Idx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
******************************************************************************/</span> <br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;<br />
<span style="color: #993333; font-weight: bold;">WITH</span> T <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> COLUMN_NAME<span style="color: #66cc66;">,</span> CONSTRAINT_TYPE <span style="color: #993333; font-weight: bold;">AS</span> OBJECT_TYPE<span style="color: #66cc66;">,</span> KCU<span style="color: #66cc66;">.</span>CONSTRAINT_NAME <span style="color: #993333; font-weight: bold;">AS</span> OBJECT_NAME <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>KEY_COLUMN_USAGE <span style="color: #993333; font-weight: bold;">AS</span> KCU<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>TABLE_CONSTRAINTS <span style="color: #993333; font-weight: bold;">AS</span> TC<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> KCU<span style="color: #66cc66;">.</span>TABLE_SCHEMA <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span>TABLE_SCHEMA &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> KCU<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span> <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> KCU<span style="color: #66cc66;">.</span>CONSTRAINT_NAME <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span>CONSTRAINT_NAME<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;OBJECT_ID<span style="color: #66cc66;">&#40;</span>KCU<span style="color: #66cc66;">.</span>TABLE_SCHEMA <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'.'</span> <span style="color: #66cc66;">+</span> KCU<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> OBJECT_ID<span style="color: #66cc66;">&#40;</span>@OBJ<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>RTRIM<span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>@COL<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">']'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'['</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> COLUMN_NAME<br />
<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>RTRIM<span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>@COL<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">']'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'['</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CONSTRAINT_TYPE <span style="color: #993333; font-weight: bold;">AS</span> OBJECT_TYPE<span style="color: #66cc66;">,</span> CK<span style="color: #66cc66;">.</span>CONSTRAINT_NAME <span style="color: #993333; font-weight: bold;">AS</span> OBJECT_NAME <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>CHECK_CONSTRAINTS <span style="color: #993333; font-weight: bold;">AS</span> CK<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>TABLE_CONSTRAINTS <span style="color: #993333; font-weight: bold;">AS</span> TC<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> CK<span style="color: #66cc66;">.</span>CONSTRAINT_SCHEMA <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span>CONSTRAINT_SCHEMA &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> CK<span style="color: #66cc66;">.</span>CONSTRAINT_NAME <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span>CONSTRAINT_NAME<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;OBJECT_ID<span style="color: #66cc66;">&#40;</span>TC<span style="color: #66cc66;">.</span>TABLE_SCHEMA <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'.'</span> <span style="color: #66cc66;">+</span> TC<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> OBJECT_ID<span style="color: #66cc66;">&#40;</span>@OBJ<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;CHECK_CLAUSE <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%'</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>RTRIM<span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>@COL<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">']'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'['</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'%'</span><br />
<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> c<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'INDEX'</span><span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">.</span>name <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>indexes <span style="color: #993333; font-weight: bold;">AS</span> i <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>index_columns <span style="color: #993333; font-weight: bold;">AS</span> ic <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> i<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> ic<span style="color: #66cc66;">.</span>object_id <span style="color: #993333; font-weight: bold;">AND</span> i<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> ic<span style="color: #66cc66;">.</span>index_id <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span> <span style="color: #993333; font-weight: bold;">AS</span> c <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> ic<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>object_id <span style="color: #993333; font-weight: bold;">AND</span> ic<span style="color: #66cc66;">.</span>column_id <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>column_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;i<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> OBJECT_ID<span style="color: #66cc66;">&#40;</span>@OBJ<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>RTRIM<span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>@COL<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">']'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'['</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">CASE</span> OBJECT_TYPE <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'PRIMARY KEY'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'UNIQUE'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'FOREIGN KEY'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">3</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'CHECK'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">4</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'INDEX'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">5</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">99</span> <span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">,</span> OBJECT_NAME; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<br />
<span style="color: #808080; font-style: italic;">--▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄--</span><br />
<span style="color: #808080; font-style: italic;">-- PHASE 2 : traduction en procédure système &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --</span><br />
<span style="color: #808080; font-style: italic;">--▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀--</span><br />
<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> sp_MS_marksystemobject <span style="color: #ff0000;">'sp__COLUMN_IS_USED'</span>;</div></div>
<p>Pour utiliser cette procédure, lancez la avec EXEC et passez le nom de table éventuellement préfixé par son schéma SQL ainsi que le nom de la colonne. Exemple :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">EXEC sp__COLUMN_IS_USED 'matable', 'macolonne';</div></div>
<p><a href="http://sqlpro.developpez.com/_fichierSQL/sp__COLUMN_IS_USED.txt" title="CODE SQL" rel="noopener" target="_blank">LE CODE ! LE CODE ! LE CODE ! LE CODE ! LE CODE ! LE CODE ! LE CODE ! LE CODE ! LE CODE ! LE CODE !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Audit trail générique</title>
		<link>https://blog.developpez.com/sqlpro/p13128/ms-sql-server/sql-server-2005/audit-trail-generique</link>
		<comments>https://blog.developpez.com/sqlpro/p13128/ms-sql-server/sql-server-2005/audit-trail-generique#comments</comments>
		<pubDate>Fri, 13 Jan 2017 14:53:54 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[audit]]></category>
		<category><![CDATA[audit trail]]></category>
		<category><![CDATA[pistage]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[traçabilité]]></category>
		<category><![CDATA[trace]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=741</guid>
		<description><![CDATA[L&#8217; &#171;&#160;audit trail&#160;&#187;, littéralement &#171;&#160;piste d&#8217;audit&#160;&#187; est un audit destiné à pister les événements qui se passe dans un système. Dans une base de données, il est, la plupart du temps, destiné à vérifier ce qui s&#8217;est passé, notamment sur le plan des valeurs avant ou après la modification. Il peut à la fois servir [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>L&rsquo; &laquo;&nbsp;audit trail&nbsp;&raquo;, littéralement &laquo;&nbsp;piste d&rsquo;audit&nbsp;&raquo; est un audit destiné à pister les événements qui se passe dans un système. Dans une base de données, il est, la plupart du temps, destiné à vérifier ce qui s&rsquo;est passé, notamment sur le plan des valeurs avant ou après la modification. Il peut à la fois servir pour la sécurité (qui à fait quoi ?), comme sur le plan fonctionnel (pourquoi cette valeur ?, À quelle date un tel changement ?&#8230;).<br />
En dehors des outils lourds comme CDC (Change Data Capture) et Change Tracking (tous deux destiné à savoir <strong>quoi</strong>, donc fonctionnel) ou Database Audit (destiné à savoir <strong>qui</strong>, donc sécurité), voici une méthode basée sur un déclencheur et une seule et unique table, facile et rapide à mettre en œuvre et qui permet de tracer qui et quoi&#8230;<br />
<span id="more-741"></span><br />
la principe est simple : chaque table reçoit un unique déclencheur lancé sur tous les événements de mise à jour (INSERT, UPDATE, DELETE) et concatène les images <em>avant </em>et <em>après </em>des données (tables inserted et deleted) puis les transforme en un unique document XML, quelque soit le nombre de lignes. Il est alors facile de stocker cette information das une ligne d&rsquo;une table, accompagnée de métadonnées, notamment, date et heure de survenance, application cliente, compte de connexion, utilisateur SQL, machine hôte, login windows, etc&#8230; </p>
<p>Il ne reste plus qu&rsquo;à lire la table de trace pour savoir qui à fait quoi et quand ! Comme les données sont sous forme XML, il est possible des les interroger par des requêtes SQL mêlant XQuery et XPath. Une astuce consistant à publier certaines vues pour les demandes de contrôle les plus fréquentes  (en général quelques tables).</p>
<p>Les objets sont créé dans un schéma SQL de nom S_ADT et la table à scruter est la table S_ADT.T_MAJ.</p>
<p>Voici l&rsquo;ensemble du code pour se faire&#8230;</p>
<p><strong>1 &#8211; LA TABLE</strong></p>
<p>Pour recueillir les données tracées</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> SCHEMA S_ADT<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> T_MAJ<br />
<span style="color: #66cc66;">&#40;</span>MAJ_ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span> <span style="color: #993333; font-weight: bold;">IDENTITY</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_DHU &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DATETIME2 <span style="color: #993333; font-weight: bold;">DEFAULT</span> SYSUTCDATETIME<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_DHL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DATETIME2 <span style="color: #993333; font-weight: bold;">DEFAULT</span> SYSDATETIME<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_CNX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sysname <span style="color: #993333; font-weight: bold;">DEFAULT</span> &nbsp; SYSTEM_USER<span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_USR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sysname <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">USER</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_APPLICATION &nbsp; &nbsp;NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_HOST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_WINLOGIN &nbsp; &nbsp; &nbsp; NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_WINDOMAIN &nbsp; &nbsp; &nbsp;NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_SCHEMA &nbsp; &nbsp; &nbsp; &nbsp; NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_OBJECT &nbsp; &nbsp; &nbsp; &nbsp; NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_TYPE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_DATA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XML<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>2 &#8211; LE TRIGGER</strong></p>
<p>Exemple de création de trigger sur une table pour tracer tous les événements</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> E_auditTrail_???<br />
<span style="color: #993333; font-weight: bold;">ON</span> ???<br />
<span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">INSERT</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">UPDATE</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DELETE</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @S sysname<span style="color: #66cc66;">,</span> @O sysname<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; @A NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @H NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @L NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @D NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @S <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> @O <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> t<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> t<span style="color: #66cc66;">.</span>parent_object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;t<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> @@PROCID;<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @A program_name<span style="color: #66cc66;">,</span> @H host_name<span style="color: #66cc66;">,</span> @L login_name<span style="color: #66cc66;">,</span> @D domain_name <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>dm_exec_sessions<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;session_id <span style="color: #66cc66;">=</span> @@SPID;<br />
<br />
<span style="color: #808080; font-style: italic;">-- cas d'insertion</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> deleted<span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> S_ADT<span style="color: #66cc66;">.</span>T_MAJ <span style="color: #66cc66;">&#40;</span>MAJ_SCHEMA<span style="color: #66cc66;">,</span> MAJ_OBJECT<span style="color: #66cc66;">,</span> MAJ_TYPE<span style="color: #66cc66;">,</span> MAJ_DATA<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION<span style="color: #66cc66;">,</span> MAJ_HOST<span style="color: #66cc66;">,</span> MAJ_WINLOGIN<span style="color: #66cc66;">,</span> MAJ_WINDOMAIN<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> @S<span style="color: #66cc66;">,</span> @O<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'I'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> inserted <span style="color: #993333; font-weight: bold;">FOR</span> XML AUTO<span style="color: #66cc66;">,</span> ELEMENTS<span style="color: #66cc66;">,</span> ROOT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'insert'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">BINARY</span> BASE64<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> XML<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A<span style="color: #66cc66;">,</span> @H<span style="color: #66cc66;">,</span> @L<span style="color: #66cc66;">,</span> @D;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- cas de la suppression</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> inserted<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> S_ADT<span style="color: #66cc66;">.</span>T_MAJ <span style="color: #66cc66;">&#40;</span>MAJ_SCHEMA<span style="color: #66cc66;">,</span> MAJ_OBJECT<span style="color: #66cc66;">,</span> MAJ_TYPE<span style="color: #66cc66;">,</span> MAJ_DATA<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION<span style="color: #66cc66;">,</span> MAJ_HOST<span style="color: #66cc66;">,</span> MAJ_WINLOGIN<span style="color: #66cc66;">,</span> MAJ_WINDOMAIN<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> @S<span style="color: #66cc66;">,</span> @O<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'D'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> deleted <span style="color: #993333; font-weight: bold;">FOR</span> XML AUTO<span style="color: #66cc66;">,</span> ELEMENTS<span style="color: #66cc66;">,</span> ROOT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'delete'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">BINARY</span> BASE64<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> XML<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A<span style="color: #66cc66;">,</span> @H<span style="color: #66cc66;">,</span> @L<span style="color: #66cc66;">,</span> @D;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- cas de la modification</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> S_ADT<span style="color: #66cc66;">.</span>T_MAJ <span style="color: #66cc66;">&#40;</span>MAJ_SCHEMA<span style="color: #66cc66;">,</span> MAJ_OBJECT<span style="color: #66cc66;">,</span> MAJ_TYPE<span style="color: #66cc66;">,</span> MAJ_DATA<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION<span style="color: #66cc66;">,</span> MAJ_HOST<span style="color: #66cc66;">,</span> MAJ_WINLOGIN<span style="color: #66cc66;">,</span> MAJ_WINDOMAIN<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> @S<span style="color: #66cc66;">,</span> @O<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'I'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'OLD'</span> <span style="color: #993333; font-weight: bold;">AS</span> _old_<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> deleted <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'NEW'</span> <span style="color: #993333; font-weight: bold;">AS</span> _new_<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> inserted<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> _auditTrail_<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FOR</span> XML AUTO<span style="color: #66cc66;">,</span> ELEMENTS<span style="color: #66cc66;">,</span> ROOT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'update'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">BINARY</span> BASE64<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> XML<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A<span style="color: #66cc66;">,</span> @H<span style="color: #66cc66;">,</span> @L<span style="color: #66cc66;">,</span> @D;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>Pour vous faciliter la tâche, quelques scripts complémentaires&#8230;</p>
<p><strong>3 &#8211; Un batch de création des triggers sur toutes les tables&#8230;</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <br />
<span style="color: #ff0000;">'CREATE TRIGGER E_auditTrail_~1<br />
ON ~2<br />
FOR INSERT, UPDATE, DELETE<br />
AS<br />
SET NOCOUNT ON;<br />
<br />
DECLARE @S sysname, @O sysname, <br />
&nbsp; &nbsp; &nbsp; &nbsp; @A NVARCHAR(128), @H NVARCHAR(128), @L NVARCHAR(128), @D NVARCHAR(128);<br />
<br />
SELECT @S = s.name, @O = o.name<br />
FROM &nbsp; sys.objects AS o<br />
&nbsp; &nbsp; &nbsp; &nbsp;JOIN sys.schemas AS s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON o.schema_id = s.schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;JOIN sys.objects AS t<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON t.parent_object_id = o.object_id<br />
WHERE &nbsp;t.object_id = @@PROCID;<br />
<br />
SELECT @A program_name, @H host_name, @L login_name, @D domain_name <br />
FROM &nbsp; sys.dm_exec_sessions<br />
WHERE &nbsp;session_id = @@SPID;<br />
<br />
IF NOT EXISTS(SELECT * FROM deleted) --&gt; cas d'</span><span style="color: #ff0000;">'insertion<br />
BEGIN<br />
&nbsp; &nbsp;INSERT INTO S_ADT.T_MAJ (MAJ_SCHEMA, MAJ_OBJECT, MAJ_TYPE, MAJ_DATA,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION, MAJ_HOST, MAJ_WINLOGIN, MAJ_WINDOMAIN)<br />
&nbsp; &nbsp;SELECT @S, @O, '</span><span style="color: #ff0000;">'I'</span><span style="color: #ff0000;">', <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CAST((SELECT * FROM inserted FOR XML AUTO, ELEMENTS, ROOT('</span><span style="color: #ff0000;">'insert'</span><span style="color: #ff0000;">'), BINARY BASE64) AS XML),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A, @H, @L, @D;<br />
&nbsp; &nbsp;RETURN;<br />
END;<br />
<br />
BEGIN<br />
IF NOT EXISTS(SELECT * FROM inserted) --&gt; cas de la suppression<br />
&nbsp; &nbsp;INSERT INTO S_ADT.T_MAJ (MAJ_SCHEMA, MAJ_OBJECT, MAJ_TYPE, MAJ_DATA,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION, MAJ_HOST, MAJ_WINLOGIN, MAJ_WINDOMAIN)<br />
&nbsp; &nbsp;SELECT @S, @O, '</span><span style="color: #ff0000;">'D'</span><span style="color: #ff0000;">', <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CAST((SELECT * FROM deleted FOR XML AUTO, ELEMENTS, ROOT('</span><span style="color: #ff0000;">'delete'</span><span style="color: #ff0000;">'), BINARY BASE64) AS XML),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A, @H, @L, @D;<br />
&nbsp; &nbsp;RETURN;<br />
END;<br />
<br />
-- cas de la modif<br />
&nbsp; &nbsp;INSERT INTO S_ADT.T_MAJ (MAJ_SCHEMA, MAJ_OBJECT, MAJ_TYPE, MAJ_DATA,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION, MAJ_HOST, MAJ_WINLOGIN, MAJ_WINDOMAIN)<br />
&nbsp; &nbsp;SELECT @S, @O, '</span><span style="color: #ff0000;">'I'</span><span style="color: #ff0000;">', <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CAST((SELECT * <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM &nbsp; (SELECT '</span><span style="color: #ff0000;">'OLD'</span><span style="color: #ff0000;">' AS _old_, * FROM deleted <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UNION ALL<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SELECT '</span><span style="color: #ff0000;">'NEW'</span><span style="color: #ff0000;">' AS _new_, * FROM inserted) AS _auditTrail_<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FOR XML AUTO, ELEMENTS, ROOT('</span><span style="color: #ff0000;">'update'</span><span style="color: #ff0000;">'), BINARY BASE64) AS XML),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A, @H, @L, @D;'</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @T NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">261</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @N <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @CMD NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> C CURSOR <br />
<span style="color: #993333; font-weight: bold;">LOCAL</span> FORWARD_ONLY STATIC READ_ONLY<br />
<span style="color: #993333; font-weight: bold;">FOR</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'['</span> <span style="color: #66cc66;">+</span> TABLE_SCHEMA <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].['</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">']'</span> <span style="color: #993333; font-weight: bold;">AS</span> T<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>NEWID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> N<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; INFORMATION_SCHEMA<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLES</span> <span style="color: #993333; font-weight: bold;">AS</span> T<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;TABLE_TYPE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'BASE TABLE'</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;TABLE_SCHEMA &nbsp;<span style="color: #ff0000;">'S_ADT'</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; INFORMATION_SCHEMA<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span> <span style="color: #993333; font-weight: bold;">AS</span> C<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;T<span style="color: #66cc66;">.</span>TABLE_SCHEMA <span style="color: #66cc66;">=</span> C<span style="color: #66cc66;">.</span>TABLE_SCHEMA<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;T<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span> <span style="color: #66cc66;">=</span> C<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;C<span style="color: #66cc66;">.</span>DATA_TYPE <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'text'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ntext'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'image'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'xml'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'geometry'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'geography'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">OPEN</span> C;<br />
FETCH C <span style="color: #993333; font-weight: bold;">INTO</span> @T<span style="color: #66cc66;">,</span> @N;<br />
WHILE @@FETCH_STATUS <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @CMD <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'~1'</span><span style="color: #66cc66;">,</span> @N<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'~2'</span><span style="color: #66cc66;">,</span> @T<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@CMD<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;FETCH C <span style="color: #993333; font-weight: bold;">INTO</span> @T<span style="color: #66cc66;">,</span> @N;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
CLOSE C;<br />
DEALLOCATE C;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>4 &#8211; Un batch de suppression de tous les triggers de tracabilité mis en place</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'DROP TRIGGER ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].'</span> <span style="color: #66cc66;">+</span> d<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">';'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>triggers <span style="color: #993333; font-weight: bold;">AS</span> d<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;d<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'E?_auditTrail?_%'</span> ESCAPE <span style="color: #ff0000;">'?'</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;LEN<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">49</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIKE</span> REPLICATE<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'[0-9A-F]'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>5 &#8211; Un batch de désactivation des déclencheurs de traçabilité</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'DISABLE TRIGGER ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].'</span> <span style="color: #66cc66;">+</span> d<span style="color: #66cc66;">.</span>name <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">' ON ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].['</span> <span style="color: #66cc66;">+</span> t<span style="color: #66cc66;">.</span>name &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'];'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>triggers <span style="color: #993333; font-weight: bold;">AS</span> d<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> t<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>parent_id <span style="color: #66cc66;">=</span> t<span style="color: #66cc66;">.</span>object_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;d<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'E?_auditTrail?_%'</span> ESCAPE <span style="color: #ff0000;">'?'</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;LEN<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">49</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIKE</span> REPLICATE<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'[0-9A-F]'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>6 &#8211; Un batch de réactivation des déclencheurs de tracabilité</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'ENABLE TRIGGER ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].'</span> <span style="color: #66cc66;">+</span> d<span style="color: #66cc66;">.</span>name <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">' ON ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].['</span> <span style="color: #66cc66;">+</span> t<span style="color: #66cc66;">.</span>name &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'];'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>triggers <span style="color: #993333; font-weight: bold;">AS</span> d<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> t<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>parent_id <span style="color: #66cc66;">=</span> t<span style="color: #66cc66;">.</span>object_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;d<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'E?_auditTrail?_%'</span> ESCAPE <span style="color: #ff0000;">'?'</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;LEN<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">49</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIKE</span> REPLICATE<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'[0-9A-F]'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>NOTA :</strong></p>
<p>Conseil important : si vous avez attribué des privilèges très fermés, il faut autoriser les utilisateurs à voir les DMV d&rsquo;état du serveur pour recueillir les données système, sinon les déclencheurs vont planter.pour cela vous devez attribuer le privilège &laquo;&nbsp;VIEW SERVER STATE&nbsp;&raquo; à tous les utilisateurs de la base à travers leur compte de connexion.<br />
Vous pouvez faire ceci à l&rsquo;aide du script SQL suivant :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">'USE master;'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'GRANT VIEW SERVER STATE TO ['</span> <span style="color: #66cc66;">+</span> L<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'];'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>database_principals <span style="color: #993333; font-weight: bold;">AS</span> U<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>server_principals <span style="color: #993333; font-weight: bold;">AS</span> L<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> L<span style="color: #66cc66;">.</span>sid <span style="color: #66cc66;">=</span> U<span style="color: #66cc66;">.</span>sid<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;L<span style="color: #66cc66;">.</span>name &nbsp;<span style="color: #ff0000;">'sa'</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;U<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dbo'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'INFORMATION_SCHEMA'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'sys'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> &nbsp;<span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE</strong><br />
<a href="http://sqlpro.developpez.com/_fichierSQL/AuditTrail.txt" title="Fichier du code" target="_blank">Le code !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Déblocage d&#8217;une instance SQL Server bloquée</title>
		<link>https://blog.developpez.com/sqlpro/p13127/ms-sql-server/sql-server-2005/deblocage-dune-instance-bloquee</link>
		<comments>https://blog.developpez.com/sqlpro/p13127/ms-sql-server/sql-server-2005/deblocage-dune-instance-bloquee#comments</comments>
		<pubDate>Thu, 12 Jan 2017 14:12:46 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[attente]]></category>
		<category><![CDATA[blocage]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[verrou]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=727</guid>
		<description><![CDATA[Il arrive, souvent à cause d&#8217;un mauvais développement (voir en gras italique plus loin), qu&#8217;un serveur SQL se bloque du fait des verrous. Une session en bloque autre qui en bloque plusieurs autres&#8230; et c&#8217;est l&#8217;effet boule de neige. La plupart du temps, les développeurs ou pseudo DBA réagissent mal en relaçant le service SQL [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Il arrive, souvent à cause d&rsquo;un mauvais développement (voir en <em><strong>gras italique</strong></em> plus loin), qu&rsquo;un serveur SQL se bloque du fait des verrous. Une session en bloque autre qui en bloque plusieurs autres&#8230; et c&rsquo;est l&rsquo;effet boule de neige. La plupart du temps, les développeurs ou pseudo DBA réagissent mal en relaçant le service SQL Server ce qui fait perdre une grande partie des mises à jour demandées, empêchent les utilisateurs de travailler et pour peu qu&rsquo;une longue transaction n&rsquo;ai pas encore enregistrées les données, rend indisponible la base après le redémarrage du serveur, souvent pendant de longues minutes&#8230; Tout cela pouvant être évité avec un peu d&rsquo;analyse et surtout la bonne requête que je vous présente, destinées à débloquer sans frustrer !<br />
<span id="more-727"></span><br />
Un blocage est une situation normale dans une base de données, comme dans la vie courante. Les feux rouges, l&rsquo;accès à des toilettes, les portiques de sécurité des aéroports, sont autant de points ou vous pouvez vous retrouvé bloqué pendant un certain temps, et cela est normal et fait partie de la vie. Les bases de données devant assurer la cohérence entre ceux qui lisent les données et ceux qui les écrivent, doivent poser des verrous afin de bloquer les utilisateurs concurrents dans certaines situations. Le principe est le suivant :</p>
<ul>
<li>un utilisateur qui <strong>lit</strong> des données, doit bloquer l&rsquo;accès aux utilisateurs qui veulent modifier les mêmes données, mais n&rsquo;empêchent pas d&rsquo;autres utilisateurs de lire en même temps les mêmes données. Dans ce cas la base pose un <strong>verrou partagé</strong> (shared), qui peut être pessimiste (posé préventivement) ou optimiste (actionné après coup).</li>
<li>un utilisateur qui <strong>écrit</strong> des données doit empêcher toute lecture et toute écriture concurrente. On parle alors de <strong>verrou exclusif</strong>.</li>
</ul>
<p>Si une session doit faire différentes modification au sein d&rsquo;une même transaction, les verrous vont être maintenus jusqu&rsquo;à ce qu&rsquo;intervienne la finalisation de la transaction, par un COMMIT (validation) ou un ROLLBACK (annulation). Pendant ce temps, si d&rsquo;autres utilisateurs doivent accéder à ces données, il faut qu&rsquo;ils patientent. C&rsquo;est la notion de blocage. Un blocage est donc une situation normale dans une base de données, comme c&rsquo;est le cas de madame Michu qui patiente dans sa 4L au feu rouge pour laisser passer les automobiliste de la voie latérale.<br />
Ce qui n&rsquo;est pas toujours normal, c&rsquo;est un blocage qui dure, longtemps, voire, très, très longtemps&#8230; mais jamais de manière éternelle, car la situation doit se débouquer à la fin du traitement bloquant, c&rsquo;est une certitude !<br />
Dans ce cas, il est possible que d&rsquo;autres utilisateurs se retrouvent bloqués, alors que même, ils ont déjà entamé des modification de données, susceptible d&rsquo;entrainer d&rsquo;autres blocages ! D’où l&rsquo;effet boule de neige&#8230; Là encore cette situation peut être normale, si le bloqueur de tête met à jour une très importante quantité de données alors que beaucoup d&rsquo;utilisateurs sont en train de parcourir ces mêmes données ! Mais dans bien des cas, c&rsquo;est une situation anormale, généralement due à des mauvaises pratiques de développement, tel que <em><strong>des transaction démarré côté client, des transactions avec interactions de l&rsquo;utilisateur, le forçage de verrous (les SGBDR se débrouillent généralement mieux tous seuls), des transactions non finalisées, l&rsquo;usage d&rsquo;un niveau d&rsquo;isolation inappropriée, l&rsquo;utilisation de code itératif eu lieu de code ensembliste, l&rsquo;oubli de poser les index adéquats, l&rsquo;utilisation de tables obèses, le recours systématique aux curseur, l’usage immodéré des déclencheurs</strong></em>, etc !<br />
Bref, la question est, en présence d&rsquo;un tel blocage, comment faire ?</p>
<p>LA SOLUTION</p>
<p>Elle consiste tout simplement à recherche qui bloque qui, et remonter jusqu&rsquo;à bloqueur de tête pour le forcer à abandonner son traitement. Problème, cela nécessite de parcourir récursivement l&rsquo;arbre de blocage de blocage&#8230; Second problème, il peut y avoir plusieurs chaines de blocage !<br />
La DMV sys.dm_exec_requests permet de savoir qui bloque qui dans l&rsquo;état actuel de l&rsquo;instance. Les deux colonnes qui nous intéressent pour ce faire sont :</p>
<ul>
<li>session_id : l&rsquo;identifiant de la session en cours</li>
<li>blocking_session_id : l&rsquo;identifiant de la session qui bloque</li>
</ul>
<p><div id="attachment_737" style="width: 688px" class="wp-caption aligncenter"><a href="http://blog.developpez.com/sqlpro/files/2017/01/blocage_session_chaine_arbre_sql_server1.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/01/blocage_session_chaine_arbre_sql_server1.jpg" alt="Différentes chaines de blocage de sessions dans SQL Server (en fait des arbres)" width="678" height="403" class="size-full wp-image-737" /></a><p class="wp-caption-text">Différentes chaines de blocage de sessions dans SQL Server (en fait des arbres)</p></div><br />
Dans la figure ci avant, les sessions 53 et 57 sont les leaders et bloquent respectivement :</p>
<ul>
<li>pour le 53, 11 session avec une profondeur maximale de 5</li>
<li>pour le 57, 3 sessions avec une profondeur maximale de 3</li>
</ul>
<p>Il faudrait débloquer en priorité le 53, attendre quelques instants pour voir si cela ne débloque pas le 57.</p>
<p>À partir de ces seules deux informations (session_id et blocking_session_id), nous pouvons remonter jusqu’aux bloqueurs de tête. L&rsquo;écriture de cette requête doit procéder en deux temps :<br />
rechercher les bloquers de tête, c&rsquo;est à dire ceux qui ne sont pas bloqués par d&rsquo;autres, mais qui en bloque d&rsquo;autres. Une fois ces &laquo;&nbsp;leaders&nbsp;&raquo; trouvés, il suffit de descendre dans l&rsquo;arbre de blocage pour comptabiliser le nombre de sessions bloquées et la profondeur du blocage.<br />
Il suffit alors de tuer les &laquo;&nbsp;lead blockers&nbsp;&raquo; les plus prégnants, c&rsquo;est à dire en général ceux qui bloquent le plus de session, ceux qui ont la plus grande profondeur de blocage, ceux dont le temps de traitement a été le plus long, ceux ayant consommé le plus d&rsquo;IO, ceux ayant la requête la moins critique&#8230;. À vous de choisir !</p>
<p>La requête !</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">WITH</span><br />
T_SESSION <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #808080; font-style: italic;">-- on récupère les sessions en cours des utilisateurs</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> session_id<span style="color: #66cc66;">,</span> blocking_session_id<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>dm_exec_requests <span style="color: #993333; font-weight: bold;">AS</span> tout<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;session_id <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">50</span><br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
T_LEAD <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #808080; font-style: italic;">-- on recherche les bloqueurs de tête</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> session_id<span style="color: #66cc66;">,</span> blocking_session_id<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_SESSION <span style="color: #993333; font-weight: bold;">AS</span> tout<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;session_id <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">50</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;blocking_session_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_SESSION <span style="color: #993333; font-weight: bold;">AS</span> tin<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;tin<span style="color: #66cc66;">.</span>blocking_session_id <span style="color: #66cc66;">=</span> tout<span style="color: #66cc66;">.</span>session_id<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
T_CHAIN <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #808080; font-style: italic;">-- requête récursive pour trouver les chaines de blocage</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> session_id <span style="color: #993333; font-weight: bold;">AS</span> lead_session_id<span style="color: #66cc66;">,</span> session_id<span style="color: #66cc66;">,</span> blocking_session_id<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> p<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_LEAD<br />
<span style="color: #993333; font-weight: bold;">UNION</span> &nbsp;<span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> C<span style="color: #66cc66;">.</span>lead_session_id<span style="color: #66cc66;">,</span> S<span style="color: #66cc66;">.</span>session_id<span style="color: #66cc66;">,</span> S<span style="color: #66cc66;">.</span>blocking_session_id<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_CHAIN <span style="color: #993333; font-weight: bold;">AS</span> C<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> T_SESSION <span style="color: #993333; font-weight: bold;">AS</span> S<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> C<span style="color: #66cc66;">.</span>session_id <span style="color: #66cc66;">=</span> S<span style="color: #66cc66;">.</span>blocking_session_id<br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
T_WEIGHT <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #808080; font-style: italic;">-- calculs finaux</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> lead_session_id <span style="color: #993333; font-weight: bold;">AS</span> LEAD_BLOCKER<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> BLOCKED_SESSION_COUNT<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>p<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> BLOCKED_DEEP<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'KILL '</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>lead_session_id <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">';'</span> <span style="color: #993333; font-weight: bold;">AS</span> SQL_CMD<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_CHAIN<br />
<span style="color: #993333; font-weight: bold;">GROUP</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> lead_session_id<br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> T<span style="color: #66cc66;">.*,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;DB_NAME<span style="color: #66cc66;">&#40;</span>r<span style="color: #66cc66;">.</span>database_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> database_name<span style="color: #66cc66;">,</span> host_name<span style="color: #66cc66;">,</span> program_name<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;nt_user_name<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;q<span style="color: #66cc66;">.</span>text <span style="color: #993333; font-weight: bold;">AS</span> sql_command<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;DATEDIFF<span style="color: #66cc66;">&#40;</span>ms<span style="color: #66cc66;">,</span> last_request_start_time<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span>last_request_end_time<span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> duration_ms<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;s<span style="color: #66cc66;">.</span>open_transaction_count<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;r<span style="color: #66cc66;">.</span>cpu_time<span style="color: #66cc66;">,</span> r<span style="color: #66cc66;">.</span>reads<span style="color: #66cc66;">,</span> r<span style="color: #66cc66;">.</span>writes<span style="color: #66cc66;">,</span> r<span style="color: #66cc66;">.</span>logical_reads<span style="color: #66cc66;">,</span> r<span style="color: #66cc66;">.</span>total_elapsed_time <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_WEIGHT <span style="color: #993333; font-weight: bold;">AS</span> T<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>dm_exec_sessions <span style="color: #993333; font-weight: bold;">AS</span> s <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> T<span style="color: #66cc66;">.</span>LEAD_BLOCKER <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>session_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>dm_exec_requests <span style="color: #993333; font-weight: bold;">AS</span> r <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> s<span style="color: #66cc66;">.</span>session_id <span style="color: #66cc66;">=</span> r<span style="color: #66cc66;">.</span>session_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">OUTER</span> APPLY sys<span style="color: #66cc66;">.</span>dm_exec_sql_text<span style="color: #66cc66;">&#40;</span>sql_handle<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> q<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> BLOCKED_SESSION_COUNT <span style="color: #993333; font-weight: bold;">DESC</span><span style="color: #66cc66;">,</span> BLOCKED_DEEP <span style="color: #993333; font-weight: bold;">DESC</span>;</div></div>
<p>Cette requête présente les sessions bloquant les autres sessions, en déterminant la session à la tête d&rsquo;une chaine de blocage (LEAD_BLOCKER), avec le nombre de sessions bloquées (BLOCKED_SESSION_COUNT) et la longueur maximale de la chaîne de blocage (BLOCKED_DEEP). Ceci permet de déterminer quelle session est à annuler en priorité en cas de blocage intempestif et d&rsquo;utiliser la commande KILL (SQL_CMD) qui termine la session fautive en forçant un ROLLBACK. Les autres colonnes donnent des informations sur la requête qui a bloqué (texte SQL, base, origine du code, métriques diverses&#8230;) </p>
<p><strong>ATTENTION </strong><br />
Ne pas confondre blocage et interblocage (ou étreinte fatale, ou encore verrou mortel&#8230;). Dans un interblocage, une session bloque des ressources qu&rsquo;une autre session veut obtenir, tandis que cette même session demandeuse a bloqué des objets que la première a besoin. Dans ce cas, nous pouvons atteindre éternellement, car le blocage ne peut être résolu par l&rsquo;attente. Le seul moyen consiste donc à sacrifier une des victimes de ce blocage et lui forçant une annulation de la transaction. Ceci est détecté automatiquement dans les bons SGBDR, qui procèdent automatiquement à l&rsquo;achèvement d&rsquo;une des victimes. Dans SQL Server, le message est intentionnellement significatif : <em>&laquo;&nbsp;La transaction (ID de processus %1!) a été bloquée sur les ressources &#8230; par un autre processus et a été choisie comme victime. Réexécutez la transaction.&nbsp;&raquo;</em></p>
<p><strong>LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE</strong><br />
<a href="http://sqlpro.developpez.com/_fichierSQL/LeadBlocker.txt" title="Fichier du code" target="_blank">Le code !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LIKE &#8216;%mot%&#8217; ou les index rotatifs&#8230;.</title>
		<link>https://blog.developpez.com/sqlpro/p13123/langage-sql-norme/like-mot-ou-les-index-rotatifs</link>
		<comments>https://blog.developpez.com/sqlpro/p13123/langage-sql-norme/like-mot-ou-les-index-rotatifs#comments</comments>
		<pubDate>Thu, 05 Jan 2017 20:01:44 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[Langage SQL (norme)]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[joker]]></category>
		<category><![CDATA[LIKE]]></category>
		<category><![CDATA[recherche]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=687</guid>
		<description><![CDATA[Qui n&#8217;a jamais rêvé d&#8217;obtenir des performances pour rechercher des mots partiels contenus dans d&#8217;autres mots, comme par exemple tous les mots contenant &#171;&#160;bolo&#160;&#187; ? Dans un dictionnaire de 128 918 mots, une telle recherche met moins de 50 millisecondes à l&#8217;aide des index rotatifs, contre ??? pour le LIKE &#8216;%bolo%&#8217;&#8230; Explications&#8230; diabolos, hyperboloïde, bolonaise, [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Qui n&rsquo;a jamais rêvé d&rsquo;obtenir des performances pour rechercher des mots partiels contenus dans d&rsquo;autres mots, comme par exemple tous les mots contenant &laquo;&nbsp;bolo&nbsp;&raquo; ? Dans un dictionnaire de 128 918 mots, une telle recherche met moins de 50 millisecondes à l&rsquo;aide des index rotatifs, contre ??? pour le LIKE &lsquo;%bolo%&rsquo;&#8230; Explications&#8230;<br />
<span id="more-687"></span><br />
diabolos, hyperboloïde, bolonaise, bolomètre, diabolo, bolométriques, amphibologie, bolonais, paraboloïde, amphibologique, bolométrique&#8230; Tels sont les mots du dictionnaire LEXIQUE du CNRS contenant la chaine de caractères &laquo;&nbsp;boblo&nbsp;&raquo;.</p>
<p><strong>LE PROBLÈME</strong></p>
<p>Les index des bases de données relationnelles, de type BTree (arbre équilibrés) trient les chaines de caractères par rapport à l&rsquo;ordre orthographique des chaines de caractères au regard du classement des lettres de l&rsquo;alphabet et de la collation choisie, cette dernière pouvant être sensible ou non, à la casse, aux accents&#8230;. Du fait de cet ordre, il est donc possible de rechercher par intervalle comme c&rsquo;est le cas de la recherche d&rsquo;un mot commençant par&#8230; En effet, si nous recherchons les mots commençant par &laquo;&nbsp;fass&nbsp;&raquo; ils sont regroupés les uns à la suite des autres dans l&rsquo;index, ce qui permet un accès immédiat et la solution est, par exemple la suivante :
<ol>
<ul>fasse</ul>
<ul>fassent</ul>
<ul>fasses</ul>
<ul>fassiez</ul>
<ul>fassions</ul>
</ol>
<p>Pour ce qui est des mots finissant par, l&rsquo;index ne nous est d&rsquo;aucune utilité, mais une astuce consiste à créer les mots inversés (par exemple à l&rsquo;aide d&rsquo;une colonne calculée), les indexer et rechercher par le même type de LIKE sur l&rsquo;inverse de la terminaison souhaitée. Par exemple, rechercher tous les mots se terminant par &laquo;&nbsp;aide&rsquo;, il suffit de faire la recherche suivante :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'edia%'</span></div></div>
<p>et de renvoyer le mot correspondant !<br />
Bien entendu pour rechercher un mot commençant par&#8230; et finissant par&#8230;, il suffit de combiner les deux recherches !<br />
Un exemple est apporté par l&rsquo;extrait de requête suivant :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">WHERE</span> MOT_MOT <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'enta%'</span> <span style="color: #993333; font-weight: bold;">AND</span> MOT_INVERSE <span style="color: #993333; font-weight: bold;">LIKE</span> CONCAT<span style="color: #66cc66;">&#40;</span>REVERSE<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'aient'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'%'</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>Mais comment être efficace lorsque l&rsquo;on recherche une chaine à l&rsquo;intérieur d&rsquo;un mot ? C&rsquo;est là qu&rsquo;intervient la notion d&rsquo;index &laquo;&nbsp;rotatif&nbsp;&raquo;&#8230;</p>
<p><strong>LE CONCEPT</strong></p>
<p><strong>Un index &laquo;&nbsp;rotatif&nbsp;&raquo;</strong> est en fait une liste de mots pour laquelle on supprime successivement la lettre du début à chaque tour, d&rsquo;où le nom d&rsquo;index &laquo;&nbsp;rotatif&nbsp;&raquo;.<br />
Par exemple le mot &laquo;&nbsp;locomotive&nbsp;&raquo; sera ainsi décliné :
<ol>
<ul>locomotive</ul>
<ul>ocomotive</ul>
<ul>comotive</ul>
<ul>omotive</ul>
<ul>motive</ul>
<ul>otive</ul>
<ul>tive</ul>
<ul>ive</ul>
<ul>ve</ul>
<ul>e</ul>
</ol>
<p>On numérote alors chaque rotation, y compris le mot racine dont par convention on attribuera l&rsquo;indice 0 comme niveau de rotation. Par exemple le &laquo;&nbsp;sous-mot&nbsp;&raquo; &laquo;&nbsp;motive&nbsp;&raquo; aura un indice de rotation de 4 parce qu&rsquo;on lui aura retiré les 4 premières lettres.<br />
Une fois ces mots Ajoutés dans une table de mots, il suffit de les indexer et d&rsquo;ajouter une table des références croisées entre les mots racine et les rotations. La recherche peut désormais se faire via un LIKE &lsquo;mot%&rsquo; qui permet d&rsquo;exploiter l&rsquo;index ! Mais il faut ensuite remonter au mot racine par une jointure à l&rsquo;aide de la table des références croisées.</p>
<p><strong>LA MÉCANIQUE</strong></p>
<p>Afin de bien distinguer ces objets techniques de votre base, objets qui n&rsquo;ont rien à voir avec les objets fonctionnels de votre application, je vous conseille de créer un schéma SQL spécifique pour les y stocker logiquement</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> SCHEMA S_XRT;</div></div>
<p><strong>Les tables pour ce faire.</strong></p>
<p>Deux tables suffisent : la table des mots, et la table des références croisées.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> S_XRT<span style="color: #66cc66;">.</span>T_MOT<br />
<span style="color: #66cc66;">&#40;</span>MOT_ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">IDENTITY</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span><br />
<span style="color: #808080; font-style: italic;">--&gt; doit être sensible aux accents mais pas à la casse ! Exemple maïs et </span><br />
<span style="color: #808080; font-style: italic;">-- &nbsp;mais, sur et sûr, retraite et retraité, congres et congrès !</span><br />
&nbsp;MOT_MOT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">COLLATE</span> French_BIN <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> S_XRT<span style="color: #66cc66;">.</span>T_MOT_ROTATION_MRT<br />
<span style="color: #66cc66;">&#40;</span>MRT_ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">IDENTITY</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span><br />
<span style="color: #808080; font-style: italic;">-- pointe vers la rotation du mot</span><br />
&nbsp;MOT_ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> S_XRT<span style="color: #66cc66;">.</span>T_MOT <span style="color: #66cc66;">&#40;</span>MOT_ID<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
<span style="color: #808080; font-style: italic;">-- pointe vers la racine du mot</span><br />
&nbsp;MOT_ID_RACINE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> S_XRT<span style="color: #66cc66;">.</span>T_MOT <span style="color: #66cc66;">&#40;</span>MOT_ID<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
<span style="color: #808080; font-style: italic;">-- indice de rotation</span><br />
&nbsp;MRT_ROTATION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TINYINT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><br />
&nbsp;<span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #66cc66;">&#40;</span>MOT_ID<span style="color: #66cc66;">,</span> MOT_ID_RACINE<span style="color: #66cc66;">,</span> MRT_ROTATION<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p><strong>Les routines</strong></p>
<p>Une procédure va permettre d&rsquo;indexer un mot. En voici le code en Transact SQL :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> S_XRT<span style="color: #66cc66;">.</span>P_INDEXATION_ROTATIVE @MOT <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> @MOT <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">LOWER</span><span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>RTRIM<span style="color: #66cc66;">&#40;</span>@MOT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @L TINYINT <span style="color: #66cc66;">=</span> LEN<span style="color: #66cc66;">&#40;</span>@MOT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @ID_MOT <span style="color: #993333; font-weight: bold;">INT</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @ROTATIONS <span style="color: #993333; font-weight: bold;">TABLE</span> <br />
<span style="color: #66cc66;">&#40;</span>MOT &nbsp; <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">COLLATE</span> French_BIN<span style="color: #66cc66;">,</span><br />
&nbsp;ROT &nbsp; TINYINT<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- calcul des rotations, racine comprise (0)</span><br />
<span style="color: #993333; font-weight: bold;">WITH</span> T <span style="color: #993333; font-weight: bold;">AS</span> <br />
<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> @MOT <span style="color: #993333; font-weight: bold;">AS</span> M<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">AS</span> I<br />
&nbsp;<span style="color: #993333; font-weight: bold;">UNION</span> &nbsp;<span style="color: #993333; font-weight: bold;">ALL</span><br />
&nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>M<span style="color: #66cc66;">,</span> @L<span style="color: #66cc66;">-</span>I <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> I <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T<br />
&nbsp;<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;I <span style="color: #66cc66;">&lt;</span> @L <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @ROTATIONS<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> T;<br />
<br />
<span style="color: #808080; font-style: italic;">-- insertions des mots manquants</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> S_XRT<span style="color: #66cc66;">.</span>T_MOT<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> MOT<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @ROTATIONS<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;MOT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> MOT_MOT<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; S_XRT<span style="color: #66cc66;">.</span>T_MOT<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- récupération de l'ID du mot racine</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @ID_MOT <span style="color: #66cc66;">=</span> MOT_ID<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; S_XRT<span style="color: #66cc66;">.</span>T_MOT<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;MOT_MOT <span style="color: #66cc66;">=</span> @MOT;<br />
<br />
<span style="color: #808080; font-style: italic;">-- insertions des rotations manquantes</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> S_XRT<span style="color: #66cc66;">.</span>T_MOT_ROTATION_MRT<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> M<span style="color: #66cc66;">.</span>MOT_ID<span style="color: #66cc66;">,</span> @ID_MOT<span style="color: #66cc66;">,</span> R<span style="color: #66cc66;">.</span>ROT<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @ROTATIONS <span style="color: #993333; font-weight: bold;">AS</span> R<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> S_XRT<span style="color: #66cc66;">.</span>T_MOT <span style="color: #993333; font-weight: bold;">AS</span> M<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> R<span style="color: #66cc66;">.</span>MOT <span style="color: #66cc66;">=</span> M<span style="color: #66cc66;">.</span>MOT_MOT<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;<span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; S_XRT<span style="color: #66cc66;">.</span>T_MOT_ROTATION_MRT <span style="color: #993333; font-weight: bold;">AS</span> MR<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;MR<span style="color: #66cc66;">.</span>MOT_ID <span style="color: #66cc66;">=</span> M<span style="color: #66cc66;">.</span>MOT_ID<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;MR<span style="color: #66cc66;">.</span>MOT_ID_RACINE <span style="color: #66cc66;">=</span> @ID_MOT<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;MR<span style="color: #66cc66;">.</span>MRT_ROTATION <span style="color: #66cc66;">=</span> R<span style="color: #66cc66;">.</span>ROT<span style="color: #66cc66;">&#41;</span>;</div></div>
<p>Enfin, une fonction table constituée d&rsquo;une simple requête paramétrée, va permettre de retrouver les mots racine à partir des partiels :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> S_XRT<span style="color: #66cc66;">.</span>F_SUPER_LIKE <span style="color: #66cc66;">&#40;</span>@MOT <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">TABLE</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> MR<span style="color: #66cc66;">.</span>MOT_ID<span style="color: #66cc66;">,</span> MR<span style="color: #66cc66;">.</span>MOT_MOT<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; S_XRT<span style="color: #66cc66;">.</span>T_MOT <span style="color: #993333; font-weight: bold;">AS</span> M<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color: #66cc66;">&#91;</span>S_XRT<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>T_MOT_ROTATION_MRT<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> R<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> M<span style="color: #66cc66;">.</span>MOT_ID <span style="color: #66cc66;">=</span> R<span style="color: #66cc66;">.</span>MOT_ID<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> S_XRT<span style="color: #66cc66;">.</span>T_MOT <span style="color: #993333; font-weight: bold;">AS</span> MR<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> R<span style="color: #66cc66;">.</span>MOT_ID_RACINE <span style="color: #66cc66;">=</span> MR<span style="color: #66cc66;">.</span>MOT_ID<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;M<span style="color: #66cc66;">.</span>MOT_MOT <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #993333; font-weight: bold;">LOWER</span><span style="color: #66cc66;">&#40;</span>RTRIM<span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>@MOT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'%'</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p><strong>QUELQUES TESTS</strong></p>
<p>Nous sommes partis d&rsquo;une base contenant le dictionnaire LEXIQUE du CNRS, comportant 128 918 mots.<br />
Notre méthode pour les indexer tous a été d&rsquo;utiliser un curseur pour connaitre le temps moyen mis par la procédure d&rsquo;indexation. Ce traitement a été effectué sur une machine ayant les caractéristiques suivantes : 2 processeurs XEON, 48 cœurs , 128 Go de RAM, 8 disques SAS en RAID 10 avec SQL Server 2016.<br />
Le temps de traitement a été de 26 minutes et 21 secondes, soit, pour 128 918 mots, 12 ms par mot.<br />
Le traitement a généré  :<br />
*   376 567 entrées de mots dans la table S_XRT.T_MOT (représentant 24 Mo)<br />
* 1 168 547 lignes dans la table S_XRT.T_MOT_ROTATION_MRT (représentant 62 Mo)<br />
À noter qu&rsquo;après réindexation, on observe une diminution du volume des données : 22 et 57 Ko respectivement.</p>
<p>Voici le batch que nous avons utilisé (la table du dictionnaire LEXIQUE du CNRS est dbo.TS_MOT_MOT :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @T DATETIME <span style="color: #66cc66;">=</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> C CURSOR <br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">LOCAL</span> FORWARD_ONLY STATIC READ_ONLY<br />
<span style="color: #993333; font-weight: bold;">FOR</span> <br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> MOT_MOT <br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; dbo<span style="color: #66cc66;">.</span>TS_MOT_MOT<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;MOT_LONGUEUR <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">1</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @MOT <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">OPEN</span> C;<br />
<br />
FETCH C <span style="color: #993333; font-weight: bold;">INTO</span> @MOT;<br />
<br />
WHILE @@FETCH_STATUS <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">EXEC</span> S_XRT<span style="color: #66cc66;">.</span>P_INDEXATION_ROTATIVE @MOT;<br />
&nbsp; &nbsp;FETCH C <span style="color: #993333; font-weight: bold;">INTO</span> @MOT;<br />
<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
CLOSE C;<br />
<br />
DEALLOCATE C;<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>ms<span style="color: #66cc66;">,</span> @T<span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>L&rsquo;utilisation de la fonction table en ligne S_XRT.F_SUPER_LIKE, met en moyenne 50 ms quelques soit la racine cherchée si elle est constituée d&rsquo;au moins 3 caractères. Exemple :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> S_XRT<span style="color: #66cc66;">.</span>F_SUPER_LIKE<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'moti'</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>Pour la pire des recherches, le système met moins d&rsquo;une seconde à trouver les 130 162 mots contenant la lettre e !</p>
<p>Pour comparaison, la recherche des mots comprenant les caractères &laquo;&nbsp;eta&nbsp;&raquo;, d&rsquo;une part directement dans le dictionnaire à l&rsquo;aide d&rsquo;un LIKE &lsquo;%eta%&rsquo; et d&rsquo;autres part dans l&rsquo;index rotatif donne les métriques suivantes :<br />
* Dictionnaire, LIKE&rsquo;%eta%&rsquo; : temps UC = 652 ms, temps écoulé = 430 ms.<br />
* Index rotatif, S_XRT.F_SUPER_LIKE (&lsquo;eta&rsquo;) : Temps UC = 265 ms, temps écoulé = 50 ms.</p>
<p>Paradoxalement les plans de requête ne reflètent pas vraiment cette différence. les couts des plans apparaissent ainsi :<br />
* Index rotatif, S_XRT.F_SUPER_LIKE (&lsquo;eta&rsquo;) : 3,79 (soit 86 % de l&rsquo;ensemble<br />
* Dictionnaire, LIKE&rsquo;%eta%&rsquo; : 0,61 (soit 14% de l&rsquo;ensemble)</p>
<div id="attachment_692" style="width: 635px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2017/01/Fonction-SUPER-LIKE.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/01/Fonction-SUPER-LIKE-1024x132.jpg" alt="Plan de requête SQL Server utilisant une fonction table en ligne et l&#039;index rotatif" width="800" height="104" class="size-large wp-image-692" /></a><p class="wp-caption-text">Plan de requête SQL Server utilisant une fonction table en ligne et l&rsquo;index rotatif</p></div>
<div id="attachment_693" style="width: 223px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2017/01/LIKE-direct.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/01/LIKE-direct.jpg" alt="Utilisation directe du LIKE &#039;%toto%&#039;" width="213" height="68" class="size-full wp-image-693" /></a><p class="wp-caption-text">Utilisation directe du LIKE &lsquo;%toto%&rsquo;</p></div>
<p><strong>UN PEU D&rsquo;ASTUCE</strong></p>
<p>Au fur et à mesure de son utilisation, les index rotatifs grandissent de moins en moins, car ils contiennent déjà des rotations utilisables pour d&rsquo;autres mots. On peut donc prévoir certaines manœuvres pour les rendre plus efficaces encore, comme le précalcul de certaines données, la compression des index ou encore l&rsquo;utilisation de vues matérialisées (ou indexées sur SQL Server).</p>
<p>À titre d&rsquo;exemple, nous avons obtenus la métrique suivante : temps UC = 63 ms, temps écoulé = 67 ms, à  l&rsquo;aide de l&rsquo;une de ces techniques !</p>
<p><strong>LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE</strong><br />
<a href="http://sqlpro.developpez.com/_fichierSQL/IndexRotatifs.txt" title="Fichier du code" target="_blank">Le code SQL</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Qui est leader dans les bases de données en 2016 ?</title>
		<link>https://blog.developpez.com/sqlpro/p13115/ms-sql-server/qui-est-leader-dans-les-bases-de-donnees-en-2016</link>
		<comments>https://blog.developpez.com/sqlpro/p13115/ms-sql-server/qui-est-leader-dans-les-bases-de-donnees-en-2016#comments</comments>
		<pubDate>Thu, 27 Oct 2016 14:43:33 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[bases de données]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2000]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[étude]]></category>
		<category><![CDATA[Gartner]]></category>
		<category><![CDATA[leader]]></category>
		<category><![CDATA[SGBD]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=675</guid>
		<description><![CDATA[Selon l&#8217;étude 2016 du groupe Gartner au sujet des bases de données opérationnelles d&#8217;entreprise (OPDBMS), Microsoft est le n°1 sur les deux axes de mesure : facilité d&#8217;utilisation et visionnaire. Microsoft détient l&#8217;un des systèmes de bases de données relationnelles les mieux conçu, les plus utilisés et le plus fiable avec SQL Server, incorporant un [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Selon l&rsquo;<a href="https://www.gartner.com/doc/reprints?id=1-3JD7HF0&amp;ct=161005&amp;st=sb" title="Magic Quadrant for Operational Database Management Systems 2016" target="_blank">étude 2016 du groupe Gartner au sujet des bases de données opérationnelles d&rsquo;entreprise (OPDBMS)</a>, Microsoft est le n°1 sur les deux axes de mesure : facilité d&rsquo;utilisation et visionnaire.</p>
<div id="attachment_677" style="width: 635px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2016/10/Gartner-Magic-Quadrant_SGBD_2016.png"><img src="http://blog.developpez.com/sqlpro/files/2016/10/Gartner-Magic-Quadrant_SGBD_2016-1024x1024.png" alt="Source : Gartner - Magic Quadrant for Operational Database Management Systems 2016" width="625" height="625" class="size-large wp-image-677" /></a><p class="wp-caption-text">Source : Gartner &#8211; Magic Quadrant for Operational Database Management Systems 2016</p></div>
<p>Microsoft détient l&rsquo;un des systèmes de bases de données relationnelles les mieux conçu, les plus utilisés et le plus fiable avec <a href="https://www.microsoft.com/fr-fr/server-cloud/products/sql-server/" target="_blank">SQL Server</a>, incorporant un moteur <a href="https://msdn.microsoft.com/fr-fr/library/dn133186.aspx" title="SQL Server 2016 in memory" target="_blank">&laquo;&nbsp;in memory&nbsp;&raquo;</a> (IMDBMS).<br />
Mais il se trouve que Microsoft est aussi le numéro deux du cloud derrière Amazon (<a href="https://azure.microsoft.com/fr-fr/services/sql-database/" title="Azure Database, la solution &quot;cloud&quot; de SQL Server" target="_blank">Azure SQL Database</a>).<br />
Il offre aussi différentes technologies complémentaire de type NoSQL avec <a href="https://azure.microsoft.com/fr-fr/services/documentdb/" title="NoSQL avec Azure DocumentDB" target="_blank">Azure DocumentDB</a> (un SGBD de type &laquo;&nbsp;document&nbsp;&raquo;) et <a href="https://azure.microsoft.com/fr-fr/documentation/articles/storage-table-design-guide/" title="Base de données NoSQL sous forme de paires clé/valeur avec Azure Table" target="_blank">Azure Tables</a> (un SGBDR de type paire clef/valeur).<br />
Très présent dans le cloud, y compris de manière hybride (une base SQL Serveur locale peut avoir une partie de ses données dans le cloud via le concept de &laquo;&nbsp;<a href="https://msdn.microsoft.com/fr-fr/library/dn935011.aspx" title="SQL Server : concept de &quot;stretch tables&quot;" target="_blank">stretch tables</a>&laquo;&nbsp;), Microsoft offre aussi de nombreuses possibilité analytiques ou mobile.</p>
<p>Pour la troisième année consécutive Mictrosoft est numéro 2 en part de marché sur le plan financier, derrière Oracle dont le Gartner Group a remonté comme point négatif de l&rsquo;étude, les problèmes de coûts, de complexité de licences et noté un rapport qualité prix jugé négatif par les clients.</p>
<p>Mais ce qui fait la force de Microsoft depuis toujours, et celle en particulier de ses solutions de bases de données, c&rsquo;est la facilité d&rsquo;utilisation (l&rsquo;un des meilleurs scores de l&rsquo;étude) et de migration de version (80% des clients ont trouvés que la migration des bases était plus facile que prévue ou autant que prévu)&#8230;</p>
<p>Les seuls problèmes pointés du doigt de manière négatives par le Gartner Group sont exclusivement celles liées au marketing, canaux de vente et distribution des solutions Microsoft ou l&rsquo;éditeur reste encore perçut par certains comme un acteurs pour de petites solutions&#8230;<br />
Finalement aucune critique technique ni financière ne vient ternir l&rsquo;image d&rsquo;excellence de Microsoft dans le domaines des bases de données d&rsquo;entreprise ce qui tend à prouver que ce classement de leader est largement mérité !</p>
<p>***</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
