]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3755 Fix big performance issue on selectChildrenComponentIds query
authorJulien Lancelot <julien.lancelot@gmail.com>
Wed, 12 Jun 2013 10:33:03 +0000 (12:33 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Wed, 12 Jun 2013 10:33:03 +0000 (12:33 +0200)
sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml

index 11cd702ab845329fbacda03dceea3a431adc70ab..bfaff59ad83d67906775324d54bc29405ca26b98 100644 (file)
     select children.project_id
     from snapshots children
     inner join (
-      select rootSnapshot.id, rootSnapshot.root_snapshot_id, rootSnapshot.path from snapshots rootSnapshot
-      <where>
-        rootSnapshot.project_id in (
-        select p.id from projects p where p.enabled=${_true}
-        and p.kee in <foreach item="componentKey" index="index" collection="componentRootKeys" open="(" separator="," close=")">#{componentKey}</foreach>
-        )
-        and rootSnapshot.islast=${_true}
-        and rootSnapshot.root_snapshot_id is not null
-      </where>
-      ) rootSnapshot on children.root_snapshot_id = rootSnapshot.root_snapshot_id
-      and
-      <choose>
-        <when test="_databaseId == 'mssql'">
-          children.path LIKE rootSnapshot.path + CAST(rootSnapshot.id AS varchar(15)) + '.%'
-        </when>
-        <when test="_databaseId == 'mysql'">
-          children.path LIKE concat(rootSnapshot.path, rootSnapshot.id, '.%')
-        </when>
-        <otherwise>
-          children.path LIKE rootSnapshot.path || rootSnapshot.id || '.%'
-        </otherwise>
-      </choose>
+      select rootSnapshot.id, rootSnapshot.root_snapshot_id, rootSnapshot.path
+      from snapshots rootSnapshot
+      inner join (
+        <include refid="org.sonar.core.resource.ResourceMapper.selectProjectIdsFromComponentKeysQuery" />
+      ) rootProject on rootProject.id = rootSnapshot.project_id
+      and rootSnapshot.islast=${_true}
+      and rootSnapshot.root_snapshot_id is not null
+    ) rootSnapshot on children.root_snapshot_id = rootSnapshot.root_snapshot_id
+    and
+    <choose>
+      <when test="_databaseId == 'mssql'">
+        children.path LIKE rootSnapshot.path + CAST(rootSnapshot.id AS varchar(15)) + '.%'
+      </when>
+      <when test="_databaseId == 'mysql'">
+        children.path LIKE concat(rootSnapshot.path, rootSnapshot.id, '.%')
+      </when>
+      <otherwise>
+        children.path LIKE rootSnapshot.path || rootSnapshot.id || '.%'
+      </otherwise>
+    </choose>
     union
     -- project ids of the children of a PRJ/TRK component
-    select children.project_id from snapshots children
-    inner join  (
-      select rootSnapshot.id from snapshots rootSnapshot
-      <where>
-        rootSnapshot.project_id in (
-        select p.id from projects p where p.enabled=${_true}
-        and p.kee in <foreach item="componentKey" index="index" collection="componentRootKeys" open="(" separator="," close=")">#{componentKey}</foreach>
-        )
-        and rootSnapshot.islast=${_true}
-        and rootSnapshot.root_snapshot_id is null
-      </where>
-      ) rootSnapshot on children.root_snapshot_id = rootSnapshot.id
+    select children.project_id
+    from snapshots children
+    inner join (
+      select rootSnapshot.id
+      from snapshots rootSnapshot
+      inner join (
+        <include refid="org.sonar.core.resource.ResourceMapper.selectProjectIdsFromComponentKeysQuery" />
+      ) rootProject on rootProject.id = rootSnapshot.project_id
+      and rootSnapshot.islast=${_true}
+      and rootSnapshot.root_snapshot_id is null
+    ) rootSnapshot on children.root_snapshot_id = rootSnapshot.id
     union
     -- project id of the component itself
-    select p.id as project_id from projects p where p.enabled=true
+    <include refid="org.sonar.core.resource.ResourceMapper.selectProjectIdsFromComponentKeysQuery" />
+  </sql>
+
+  <sql id="selectProjectIdsFromComponentKeysQuery">
+    select p.id from projects p
+    <where>
+      p.enabled=${_true}
       and p.kee in <foreach item="componentKey" index="index" collection="componentRootKeys" open="(" separator="," close=")">#{componentKey}</foreach>
+    </where>
   </sql>
 
   <insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="id">