<?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; LIKE</title>
	<atom:link href="https://blog.developpez.com/sqlpro/ptag/like/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>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>
	</channel>
</rss>
